public function testSelectNewestPicksLatestStableWithPreferStable()
 {
     $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0.0'));
     $this->repo->addPackage($packageA2 = $this->getPackage('A', '1.0.1-alpha'));
     $this->pool->addRepository($this->repo);
     $literals = array($packageA1->getId(), $packageA2->getId());
     $expected = array($packageA1->getId());
     $policy = new DefaultPolicy(true);
     $selected = $policy->selectPreferedPackages($this->pool, array(), $literals);
     $this->assertEquals($expected, $selected);
 }
 public function testSelectLowest()
 {
     $policy = new DefaultPolicy(false, true);
     $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0'));
     $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
     $this->pool->addRepository($this->repo);
     $literals = array($packageA1->getId(), $packageA2->getId());
     $expected = array($packageA1->getId());
     $selected = $policy->selectPreferredPackages($this->pool, array(), $literals);
     $this->assertEquals($expected, $selected);
 }
示例#3
0
 protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null)
 {
     $name = strtolower($name);
     $constraint = null;
     if ($version) {
         $constraint = $this->versionParser->parseConstraints($version);
     }
     $policy = new DefaultPolicy();
     $pool = new Pool('dev');
     $pool->addRepository($repos);
     $matchedPackage = null;
     $versions = array();
     $matches = $pool->whatProvides($name, $constraint);
     foreach ($matches as $index => $package) {
         // skip providers/replacers
         if ($package->getName() !== $name) {
             unset($matches[$index]);
             continue;
         }
         // select an exact match if it is in the installed repo and no specific version was required
         if (null === $version && $installedRepo->hasPackage($package)) {
             $matchedPackage = $package;
         }
         $versions[$package->getPrettyVersion()] = $package->getVersion();
         $matches[$index] = $package->getId();
     }
     // select prefered package according to policy rules
     if (!$matchedPackage && $matches && ($prefered = $policy->selectPreferedPackages($pool, array(), $matches))) {
         $matchedPackage = $pool->literalToPackage($prefered[0]);
     }
     return array($matchedPackage, $versions);
 }
示例#4
0
 /**
  * Finds a package by name and version if provided.
  *
  * @param RepositoryInterface $installedRepo
  * @param RepositoryInterface $repos
  * @param string              $name
  * @param string|null         $version
  *
  * @throws \InvalidArgumentException
  *
  * @return array [CompletePackageInterface, array of versions]
  */
 protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null)
 {
     $name = strtolower($name);
     $constraint = null;
     if ($version !== null) {
         $constraint = $this->versionParser->parseConstraints($version);
     }
     $policy = new DefaultPolicy();
     $pool = new Pool('dev');
     $pool->addRepository($repos);
     $matchedPackage = null;
     $versions = [];
     $matches = $pool->whatProvides($name, $constraint);
     foreach ($matches as $index => $package) {
         // Skip providers/replacers.
         if ($package->getName() !== $name) {
             unset($matches[$index]);
             continue;
         }
         // Select an exact match if it is in the installed repo and no specific version was required.
         if ($version === null && $installedRepo->hasPackage($package)) {
             $matchedPackage = $package;
         }
         $versions[$package->getPrettyVersion()] = $package->getVersion();
         $matches[$index] = $package->getId();
     }
     // Select prefered package according to policy rules.
     if (!$matchedPackage && !empty($matches) && ($prefered = $policy->selectPreferredPackages($pool, [], $matches))) {
         $matchedPackage = $pool->literalToPackage($prefered[0]);
     }
     // If we have package result, return them.
     if ($matchedPackage) {
         return [$matchedPackage->getName() => ['package' => $matchedPackage, 'versions' => $versions]];
     }
     return null;
 }