2011-10-21 12:44:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\DependencyResolver;
|
|
|
|
|
|
|
|
use Composer\Repository\ArrayRepository;
|
2011-11-18 23:27:35 +00:00
|
|
|
use Composer\Repository\RepositoryInterface;
|
2011-10-21 12:44:24 +00:00
|
|
|
use Composer\DependencyResolver\DefaultPolicy;
|
|
|
|
use Composer\DependencyResolver\Pool;
|
|
|
|
use Composer\Package\Link;
|
2012-04-20 20:10:58 +00:00
|
|
|
use Composer\Package\AliasPackage;
|
2015-09-24 14:32:36 +00:00
|
|
|
use Composer\Semver\Constraint\Constraint;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2011-11-20 14:06:12 +00:00
|
|
|
class DefaultPolicyTest extends TestCase
|
2011-10-21 12:44:24 +00:00
|
|
|
{
|
2016-09-27 16:03:14 +00:00
|
|
|
/** @var Pool */
|
2011-10-21 12:44:24 +00:00
|
|
|
protected $pool;
|
2016-09-27 16:03:14 +00:00
|
|
|
/** @var ArrayRepository */
|
2011-10-21 12:44:24 +00:00
|
|
|
protected $repo;
|
2016-09-27 16:03:14 +00:00
|
|
|
/** @var ArrayRepository */
|
2011-10-21 12:44:24 +00:00
|
|
|
protected $repoInstalled;
|
2016-09-27 16:03:14 +00:00
|
|
|
/** @var DefaultPolicy */
|
2011-10-21 12:44:24 +00:00
|
|
|
protected $policy;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2012-07-04 12:22:09 +00:00
|
|
|
$this->pool = new Pool('dev');
|
2011-10-21 12:44:24 +00:00
|
|
|
$this->repo = new ArrayRepository;
|
|
|
|
$this->repoInstalled = new ArrayRepository;
|
|
|
|
|
|
|
|
$this->policy = new DefaultPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectSingle()
|
|
|
|
{
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
|
2011-10-21 12:44:24 +00:00
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals = array($packageA->getId());
|
|
|
|
$expected = array($packageA->getId());
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2011-10-21 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectNewest()
|
|
|
|
{
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0'));
|
|
|
|
$this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
|
2011-10-21 12:44:24 +00:00
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals = array($packageA1->getId(), $packageA2->getId());
|
|
|
|
$expected = array($packageA2->getId());
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2011-10-21 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 20:02:55 +00:00
|
|
|
public function testSelectNewestPicksLatest()
|
|
|
|
{
|
|
|
|
$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($packageA2->getId());
|
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2013-03-28 20:02:55 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2013-03-28 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $policy->selectPreferredPackages($this->pool, array(), $literals);
|
2013-03-28 20:02:55 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2013-03-28 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 16:12:54 +00:00
|
|
|
public function testSelectNewestWithDevPicksNonDev()
|
|
|
|
{
|
|
|
|
$this->repo->addPackage($packageA1 = $this->getPackage('A', 'dev-foo'));
|
|
|
|
$this->repo->addPackage($packageA2 = $this->getPackage('A', '1.0.0'));
|
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
|
|
|
$literals = array($packageA1->getId(), $packageA2->getId());
|
|
|
|
$expected = array($packageA2->getId());
|
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2013-05-23 16:12:54 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2013-05-23 16:12:54 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 14:44:10 +00:00
|
|
|
public function testSelectNewestOverInstalled()
|
2011-10-21 12:44:24 +00:00
|
|
|
{
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
|
|
|
|
$this->repoInstalled->addPackage($packageAInstalled = $this->getPackage('A', '1.0'));
|
2011-10-21 12:44:24 +00:00
|
|
|
$this->pool->addRepository($this->repoInstalled);
|
2011-11-18 23:27:35 +00:00
|
|
|
$this->pool->addRepository($this->repo);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals = array($packageA->getId(), $packageAInstalled->getId());
|
|
|
|
$expected = array($packageA->getId());
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, $this->mapFromRepo($this->repoInstalled), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2011-10-21 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 09:11:45 +00:00
|
|
|
public function testSelectFirstRepo()
|
2011-10-21 12:44:24 +00:00
|
|
|
{
|
2016-09-27 16:03:14 +00:00
|
|
|
$otherRepository = new ArrayRepository;
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
|
2016-09-27 16:03:14 +00:00
|
|
|
$otherRepository->addPackage($packageAImportant = $this->getPackage('A', '1.0'));
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$this->pool->addRepository($this->repoInstalled);
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->pool->addRepository($otherRepository);
|
2012-03-06 09:11:45 +00:00
|
|
|
$this->pool->addRepository($this->repo);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals = array($packageA->getId(), $packageAImportant->getId());
|
|
|
|
$expected = array($packageAImportant->getId());
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRepositoryOrderingAffectsPriority()
|
|
|
|
{
|
|
|
|
$repo1 = new ArrayRepository;
|
|
|
|
$repo2 = new ArrayRepository;
|
|
|
|
|
|
|
|
$repo1->addPackage($package1 = $this->getPackage('A', '1.0'));
|
|
|
|
$repo1->addPackage($package2 = $this->getPackage('A', '1.1'));
|
|
|
|
$repo2->addPackage($package3 = $this->getPackage('A', '1.1'));
|
|
|
|
$repo2->addPackage($package4 = $this->getPackage('A', '1.2'));
|
|
|
|
|
|
|
|
$this->pool->addRepository($repo1);
|
|
|
|
$this->pool->addRepository($repo2);
|
|
|
|
|
|
|
|
$literals = array($package1->getId(), $package2->getId(), $package3->getId(), $package4->getId());
|
|
|
|
$expected = array($package2->getId());
|
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $selected);
|
|
|
|
|
|
|
|
$this->pool = new Pool('dev');
|
|
|
|
$this->pool->addRepository($repo2);
|
|
|
|
$this->pool->addRepository($repo1);
|
|
|
|
|
|
|
|
$expected = array($package4->getId());
|
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $selected);
|
2011-10-21 12:44:24 +00:00
|
|
|
}
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2012-04-20 20:10:58 +00:00
|
|
|
public function testSelectLocalReposFirst()
|
|
|
|
{
|
2016-09-27 16:03:14 +00:00
|
|
|
$repoImportant = new ArrayRepository;
|
2012-04-20 20:10:58 +00:00
|
|
|
|
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', 'dev-master'));
|
|
|
|
$this->repo->addPackage($packageAAlias = new AliasPackage($packageA, '2.1.9999999.9999999-dev', '2.1.x-dev'));
|
2016-09-27 16:03:14 +00:00
|
|
|
$repoImportant->addPackage($packageAImportant = $this->getPackage('A', 'dev-feature-a'));
|
|
|
|
$repoImportant->addPackage($packageAAliasImportant = new AliasPackage($packageAImportant, '2.1.9999999.9999999-dev', '2.1.x-dev'));
|
|
|
|
$repoImportant->addPackage($packageA2Important = $this->getPackage('A', 'dev-master'));
|
|
|
|
$repoImportant->addPackage($packageA2AliasImportant = new AliasPackage($packageA2Important, '2.1.9999999.9999999-dev', '2.1.x-dev'));
|
2012-04-20 20:10:58 +00:00
|
|
|
$packageAAliasImportant->setRootPackageAlias(true);
|
|
|
|
|
|
|
|
$this->pool->addRepository($this->repoInstalled);
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->pool->addRepository($repoImportant);
|
2012-04-20 20:10:58 +00:00
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
2015-09-24 14:32:36 +00:00
|
|
|
$packages = $this->pool->whatProvides('a', new Constraint('=', '2.1.9999999.9999999-dev'));
|
2012-04-20 20:10:58 +00:00
|
|
|
$literals = array();
|
|
|
|
foreach ($packages as $package) {
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals[] = $package->getId();
|
2012-04-20 20:10:58 +00:00
|
|
|
}
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$expected = array($packageAAliasImportant->getId());
|
2012-04-20 20:10:58 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2012-04-20 20:10:58 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2012-04-20 20:10:58 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 12:58:31 +00:00
|
|
|
public function testSelectAllProviders()
|
|
|
|
{
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
|
|
|
|
$this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2015-09-24 14:32:36 +00:00
|
|
|
$packageA->setProvides(array(new Link('A', 'X', new Constraint('==', '1.0'), 'provides')));
|
|
|
|
$packageB->setProvides(array(new Link('B', 'X', new Constraint('==', '1.0'), 'provides')));
|
2011-10-21 12:58:31 +00:00
|
|
|
|
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals = array($packageA->getId(), $packageB->getId());
|
2011-10-21 12:58:31 +00:00
|
|
|
$expected = $literals;
|
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2011-10-21 12:58:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:20:45 +00:00
|
|
|
public function testPreferNonReplacingFromSameRepo()
|
2011-10-21 12:58:31 +00:00
|
|
|
{
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
|
|
|
|
$this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2015-09-24 14:32:36 +00:00
|
|
|
$packageB->setReplaces(array(new Link('B', 'A', new Constraint('==', '1.0'), 'replaces')));
|
2011-10-21 12:58:31 +00:00
|
|
|
|
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
2012-05-19 18:38:56 +00:00
|
|
|
$literals = array($packageA->getId(), $packageB->getId());
|
|
|
|
$expected = $literals;
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2011-10-21 12:58:31 +00:00
|
|
|
}
|
2011-11-18 23:27:35 +00:00
|
|
|
|
2013-04-17 13:39:42 +00:00
|
|
|
public function testPreferReplacingPackageFromSameVendor()
|
|
|
|
{
|
2013-04-17 15:37:22 +00:00
|
|
|
// test with default order
|
2013-04-17 13:39:42 +00:00
|
|
|
$this->repo->addPackage($packageB = $this->getPackage('vendor-b/replacer', '1.0'));
|
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('vendor-a/replacer', '1.0'));
|
|
|
|
|
2015-09-24 14:32:36 +00:00
|
|
|
$packageA->setReplaces(array(new Link('vendor-a/replacer', 'vendor-a/package', new Constraint('==', '1.0'), 'replaces')));
|
|
|
|
$packageB->setReplaces(array(new Link('vendor-b/replacer', 'vendor-a/package', new Constraint('==', '1.0'), 'replaces')));
|
2013-04-17 13:39:42 +00:00
|
|
|
|
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
|
|
|
$literals = array($packageA->getId(), $packageB->getId());
|
|
|
|
$expected = $literals;
|
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals, 'vendor-a/package');
|
2013-04-17 13:39:42 +00:00
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
|
2013-04-17 15:37:22 +00:00
|
|
|
// test with reversed order in repo
|
|
|
|
$repo = new ArrayRepository;
|
|
|
|
$repo->addPackage($packageA = clone $packageA);
|
|
|
|
$repo->addPackage($packageB = clone $packageB);
|
|
|
|
|
|
|
|
$pool = new Pool('dev');
|
|
|
|
$pool->addRepository($this->repo);
|
|
|
|
|
|
|
|
$literals = array($packageA->getId(), $packageB->getId());
|
|
|
|
$expected = $literals;
|
2013-04-17 13:39:42 +00:00
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals, 'vendor-a/package');
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2013-04-17 13:39:42 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
protected function mapFromRepo(RepositoryInterface $repo)
|
|
|
|
{
|
|
|
|
$map = array();
|
|
|
|
foreach ($repo->getPackages() as $package) {
|
|
|
|
$map[$package->getId()] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
2014-11-21 13:01:01 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
2015-03-20 14:23:24 +00:00
|
|
|
$selected = $policy->selectPreferredPackages($this->pool, array(), $literals);
|
2014-11-21 13:01:01 +00:00
|
|
|
|
2016-09-27 16:03:14 +00:00
|
|
|
$this->assertSame($expected, $selected);
|
2014-11-21 13:01:01 +00:00
|
|
|
}
|
2011-10-21 12:44:24 +00:00
|
|
|
}
|