123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?php
- namespace Composer\Test\DependencyResolver;
- use Composer\Repository\ArrayRepository;
- use Composer\Repository\PlatformRepository;
- use Composer\Repository\ComposerRepository;
- use Composer\DependencyResolver\DefaultPolicy;
- use Composer\DependencyResolver\Pool;
- use Composer\DependencyResolver\Request;
- use Composer\DependencyResolver\Solver;
- use Composer\DependencyResolver\SolverProblemsException;
- use Composer\Package\Link;
- use Composer\Package\LinkConstraint\VersionConstraint;
- use Composer\Test\TestCase;
- class SolverTest extends TestCase
- {
- 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->request = new Request($this->pool);
- $this->policy = new DefaultPolicy;
- $this->solver = new Solver($this->policy, $this->pool, $this->repoInstalled);
- }
- public function testSolverInstallSingle()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testSolverInstallWithDeps()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageB),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testSolverInstallInstalled()
- {
- $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array());
- }
- public function testSolverInstallInstalledWithAlternative()
- {
- $this->repo->addPackage($this->getPackage('A', '1.0'));
- $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array());
- }
- public function testSolverRemoveSingle()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->remove('A');
- $this->checkSolverResult(array(
- array('job' => 'remove', 'package' => $packageA),
- ));
- }
- public function testSolverRemoveUninstalled()
- {
- $this->repo->addPackage($this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->remove('A');
- $this->checkSolverResult(array());
- }
- public function testSolverUpdateDoesOnlyUpdate()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
- $this->reposComplete();
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0.0.0'), 'requires')));
- $this->request->install('A', new VersionConstraint('=', '1.0.0.0'));
- $this->request->install('B', new VersionConstraint('=', '1.1.0.0'));
- $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
- $this->request->update('B', new VersionConstraint('=', '1.0.0.0'));
- $this->checkSolverResult(array(
- array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
- ));
- }
- public function testSolverUpdateSingle()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
- $this->reposComplete();
- $this->request->update('A');
- $this->checkSolverResult(array(
- array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
- ));
- }
- public function testSolverUpdateCurrent()
- {
- $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
- $this->repo->addPackage($this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->update('A');
- $this->checkSolverResult(array());
- }
- public function testSolverUpdateOnlyUpdatesSelectedPackage()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($packageAnewer = $this->getPackage('A', '1.1'));
- $this->repo->addPackage($packageBnewer = $this->getPackage('B', '1.1'));
- $this->reposComplete();
- $this->request->update('A');
- $this->checkSolverResult(array(
- array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
- ));
- }
- public function testSolverUpdateConstrained()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
- $this->repo->addPackage($this->getPackage('A', '2.0'));
- $this->reposComplete();
- $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
- $this->request->update('A');
- $this->checkSolverResult(array(array(
- 'job' => 'update',
- 'from' => $packageA,
- 'to' => $newPackageA,
- )));
- }
- public function testSolverUpdateFullyConstrained()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
- $this->repo->addPackage($this->getPackage('A', '2.0'));
- $this->reposComplete();
- $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
- $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
- $this->checkSolverResult(array(array(
- 'job' => 'update',
- 'from' => $packageA,
- 'to' => $newPackageA,
- )));
- }
- public function testSolverUpdateFullyConstrainedPrunesInstalledPackages()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repoInstalled->addPackage($this->getPackage('B', '1.0'));
- $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
- $this->repo->addPackage($this->getPackage('A', '2.0'));
- $this->reposComplete();
- $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
- $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
- $this->checkSolverResult(array(array(
- 'job' => 'update',
- 'from' => $packageA,
- 'to' => $newPackageA,
- )));
- }
- public function testSolverAllJobs()
- {
- $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
- $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
- $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
- $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
- $this->repo->addPackage($this->getPackage('D', '1.0'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
- $this->reposComplete();
- $this->request->install('A');
- $this->request->update('C');
- $this->request->remove('D');
- $this->checkSolverResult(array(
- array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
- array('job' => 'install', 'package' => $packageB),
- array('job' => 'remove', 'package' => $packageD),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testSolverThreeAlternativeRequireAndConflict()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
- $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
- $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
- $packageA->setConflicts(array(new Link('A', 'B', new VersionConstraint('<', '1.0'), 'conflicts')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $middlePackageB),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testSolverObsolete()
- {
- $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $packageB->setReplaces(array(new Link('B', 'A', null)));
- $this->reposComplete();
- $this->request->install('B');
- $this->checkSolverResult(array(
- array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
- ));
- }
- public function testInstallOneOfTwoAlternatives()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testInstallProvider()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '0.8'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageQ->setProvides(array(new Link('Q', 'B', new VersionConstraint('=', '1.0'), 'provides')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageQ),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testSkipReplacerOfExistingPackage()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageB),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testInstallReplacerOfMissingPackage()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageQ),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testSkipReplacedPackageIfReplacerIsSelected()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
- $this->reposComplete();
- $this->request->install('A');
- $this->request->install('Q');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageQ),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testPickOlderIfNewerConflicts()
- {
- $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
- $packageX->setRequires(array(
- new Link('X', 'A', new VersionConstraint('>=', '2.0.0.0'), 'requires'),
- new Link('X', 'B', new VersionConstraint('>=', '2.0.0.0'), 'requires')));
- $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
- $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
- $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '2.0.0.0'), 'requires')));
-
-
- $newPackageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '2.2.0.0'), 'requires')));
-
-
-
- $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
- $packageS->setReplaces(array(new Link('S', 'A', new VersionConstraint('>=', '2.0.0.0'), 'replaces'), new Link('S', 'B', new VersionConstraint('>=', '2.0.0.0'), 'replaces')));
- $this->reposComplete();
- $this->request->install('X');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageA),
- array('job' => 'install', 'package' => $newPackageB),
- array('job' => 'install', 'package' => $packageX),
- ));
- }
- public function testInstallCircularRequire()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
- $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageB2->setRequires(array(new Link('B', 'A', new VersionConstraint('>=', '1.0'), 'requires')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageB2),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testInstallAlternativeWithCircularRequire()
- {
- $this->markTestIncomplete();
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
- $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
- $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageB->setRequires(array(new Link('B', 'Virtual', new VersionConstraint('>=', '1.0'), 'requires')));
- $packageC->setRequires(array(new Link('C', 'Virtual', new VersionConstraint('==', '1.0'), 'provides')));
- $packageD->setRequires(array(new Link('D', 'Virtual', new VersionConstraint('==', '1.0'), 'provides')));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageC),
- array('job' => 'install', 'package' => $packageB),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
-
- public function testUseReplacerIfNecessary()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
- $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
- $packageA->setRequires(array(
- new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires'),
- new Link('A', 'C', new VersionConstraint('>=', '1.0'), 'requires'),
- ));
- $packageD->setReplaces(array(
- new Link('D', 'B', new VersionConstraint('>=', '1.0'), 'replaces'),
- new Link('D', 'C', new VersionConstraint('>=', '1.0'), 'replaces'),
- ));
- $packageD2->setReplaces(array(
- new Link('D', 'B', new VersionConstraint('>=', '1.0'), 'replaces'),
- new Link('D', 'C', new VersionConstraint('>=', '1.0'), 'replaces'),
- ));
- $this->reposComplete();
- $this->request->install('A');
- $this->checkSolverResult(array(
- array('job' => 'install', 'package' => $packageD2),
- array('job' => 'install', 'package' => $packageA),
- ));
- }
- public function testConflictResultEmpty()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));;
- $packageA->setConflicts(array(
- new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'conflicts'),
- ));
- $this->reposComplete();
- $this->request->install('A');
- $this->request->install('B');
- try {
- $transaction = $this->solver->solve($this->request);
- $this->fail('Unsolvable conflict did not resolve in exception.');
- } catch (SolverProblemsException $e) {
- }
- }
- public function testUnsatisfiableRequires()
- {
- $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
- $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
- $packageA->setRequires(array(
- new Link('A', 'B', new VersionConstraint('>=', '2.0'), 'requires'),
- ));
- $this->reposComplete();
- $this->request->install('A');
- try {
- $transaction = $this->solver->solve($this->request);
- $this->fail('Unsolvable conflict did not resolve in exception.');
- } catch (SolverProblemsException $e) {
- }
- }
- protected function reposComplete()
- {
- $this->pool->addRepository($this->repoInstalled);
- $this->pool->addRepository($this->repo);
- }
- protected function checkSolverResult(array $expected)
- {
- $transaction = $this->solver->solve($this->request);
- $result = array();
- foreach ($transaction as $operation) {
- if ('update' === $operation->getJobType()) {
- $result[] = array(
- 'job' => 'update',
- 'from' => $operation->getInitialPackage(),
- 'to' => $operation->getTargetPackage()
- );
- } else {
- $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
- $result[] = array(
- 'job' => $job,
- 'package' => $operation->getPackage()
- );
- }
- }
- $this->assertEquals($expected, $result);
- }
- }
|