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\DependencyResolver\Literal;
|
|
|
|
use Composer\Package\Link;
|
|
|
|
use Composer\Package\LinkConstraint\VersionConstraint;
|
2011-11-20 14:06:12 +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
|
|
|
{
|
|
|
|
protected $pool;
|
|
|
|
protected $repo;
|
|
|
|
protected $repoInstalled;
|
|
|
|
protected $request;
|
|
|
|
protected $policy;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->pool = new Pool;
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$literals = array(new Literal($packageA, true));
|
|
|
|
$expected = array(new Literal($packageA, true));
|
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$literals = array(new Literal($packageA1, true), new Literal($packageA2, true));
|
|
|
|
$expected = array(new Literal($packageA2, true));
|
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
$literals = array(new Literal($packageA, true), new Literal($packageAInstalled, true));
|
2011-10-22 14:44:10 +00:00
|
|
|
$expected = array(new Literal($packageA, true));
|
2011-10-21 12:44:24 +00:00
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$selected = $this->policy->selectPreferedPackages($this->pool, $this->mapFromRepo($this->repoInstalled), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectLastRepo()
|
|
|
|
{
|
|
|
|
$this->repoImportant = new ArrayRepository;
|
|
|
|
|
2011-11-20 14:06:12 +00:00
|
|
|
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
|
|
|
|
$this->repoImportant->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);
|
2011-10-21 12:44:24 +00:00
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
$this->pool->addRepository($this->repoImportant);
|
|
|
|
|
|
|
|
$literals = array(new Literal($packageA, true), new Literal($packageAImportant, true));
|
|
|
|
$expected = array(new Literal($packageAImportant, true));
|
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
|
2011-10-21 12:44:24 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$packageA->setProvides(array(new Link('A', 'X', new VersionConstraint('==', '1.0'), 'provides')));
|
|
|
|
$packageB->setProvides(array(new Link('B', 'X', new VersionConstraint('==', '1.0'), 'provides')));
|
|
|
|
|
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
|
|
|
$literals = array(new Literal($packageA, true), new Literal($packageB, true));
|
|
|
|
$expected = $literals;
|
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
|
2011-10-21 12:58:31 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
$packageB->setReplaces(array(new Link('B', 'A', new VersionConstraint('==', '1.0'), 'replaces')));
|
|
|
|
|
|
|
|
$this->pool->addRepository($this->repo);
|
|
|
|
|
|
|
|
$literals = array(new Literal($packageA, true), new Literal($packageB, true));
|
2011-10-22 15:20:45 +00:00
|
|
|
$expected = array(new Literal($packageA, true), new Literal($packageB, true));
|
2011-10-21 12:58:31 +00:00
|
|
|
|
2011-11-18 23:27:35 +00:00
|
|
|
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
|
2011-10-21 12:58:31 +00:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $selected);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2011-10-21 12:44:24 +00:00
|
|
|
}
|