1
0
Fork 0
composer/tests/Composer/Test/DependencyResolver/SolverTest.php

943 lines
38 KiB
PHP
Raw Normal View History

<?php
/*
2011-04-16 12:42:35 +00:00
* This file is part of Composer.
*
2011-04-16 12:42:35 +00:00
* (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.
*/
2015-10-13 09:34:02 +00:00
namespace Composer\Test\DependencyResolver;
use Composer\IO\NullIO;
2011-05-23 00:18:11 +00:00
use Composer\Repository\ArrayRepository;
use Composer\DependencyResolver\DefaultPolicy;
use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Request;
use Composer\DependencyResolver\Solver;
use Composer\DependencyResolver\SolverProblemsException;
2011-05-23 00:18:11 +00:00
use Composer\Package\Link;
use Composer\Test\TestCase;
use Composer\Semver\Constraint\MultiConstraint;
class SolverTest extends TestCase
{
protected $pool;
protected $repo;
protected $repoInstalled;
protected $request;
protected $policy;
protected $solver;
public function setUp()
{
$this->pool = new Pool;
$this->repo = new ArrayRepository;
$this->repoInstalled = new ArrayRepository;
$this->request = new Request();
$this->policy = new DefaultPolicy;
$this->solver = new Solver($this->policy, $this->pool, $this->repoInstalled, new NullIO());
}
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 testSolverRemoveIfNotInstalled()
{
$this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->reposComplete();
$this->checkSolverResult(array(
array('job' => 'remove', 'package' => $packageA),
));
}
public function testInstallNonExistingPackageFails()
{
$this->repo->addPackage($this->getPackage('A', '1.0'));
$this->reposComplete();
$this->request->install('B', $this->getVersionConstraint('==', '1'));
try {
$transaction = $this->solver->solve($this->request);
$this->fail('Unsolvable conflict did not result in exception.');
} catch (SolverProblemsException $e) {
$problems = $e->getProblems();
2017-11-30 14:58:10 +00:00
$this->assertCount(1, $problems);
$this->assertEquals(2, $e->getCode());
2012-10-31 17:34:27 +00:00
$this->assertEquals("\n - The requested package b could not be found in any version, there may be a typo in the package name.", $problems[0]->getPrettyString());
}
}
public function testSolverInstallSamePackageFromDifferentRepositories()
{
$repo1 = new ArrayRepository;
$repo2 = new ArrayRepository;
$repo1->addPackage($foo1 = $this->getPackage('foo', '1'));
$repo2->addPackage($foo2 = $this->getPackage('foo', '1'));
$this->pool->addRepository($this->repoInstalled);
$this->pool->addRepository($repo1);
$this->pool->addRepository($repo2);
$this->request->install('foo');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $foo1),
));
}
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('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
$this->reposComplete();
$this->request->install('A');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageB),
array('job' => 'install', 'package' => $packageA),
));
}
2012-06-19 01:08:36 +00:00
public function testSolverInstallHonoursNotEqualOperator()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
$this->repo->addPackage($newPackageB11 = $this->getPackage('B', '1.1'));
$this->repo->addPackage($newPackageB12 = $this->getPackage('B', '1.2'));
$this->repo->addPackage($newPackageB13 = $this->getPackage('B', '1.3'));
$packageA->setRequires(array(
'b' => new Link('A', 'B', new MultiConstraint(array(
$this->getVersionConstraint('<=', '1.3'),
$this->getVersionConstraint('<>', '1.3'),
$this->getVersionConstraint('!=', '1.2'),
)), 'requires'),
2012-06-19 01:08:36 +00:00
));
$this->reposComplete();
$this->request->install('A');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $newPackageB11),
array('job' => 'install', 'package' => $packageA),
));
}
public function testSolverInstallWithDepsInOrder()
{
$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'));
$packageB->setRequires(array(
'a' => new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
'c' => new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageC->setRequires(array(
'a' => new Link('C', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$this->reposComplete();
$this->request->install('A');
$this->request->install('B');
$this->request->install('C');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageA),
array('job' => 'install', 'package' => $packageC),
array('job' => 'install', 'package' => $packageB),
));
}
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());
}
2012-01-15 13:15:53 +00:00
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('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0.0.0'), 'requires')));
2012-01-15 13:15:53 +00:00
$this->request->install('A', $this->getVersionConstraint('=', '1.0.0.0'));
$this->request->install('B', $this->getVersionConstraint('=', '1.1.0.0'));
$this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
$this->request->update('B', $this->getVersionConstraint('=', '1.0.0.0'));
2012-01-15 13:15:53 +00:00
$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->install('A');
$this->request->update('A');
$this->checkSolverResult(array(
array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
));
}
public function testSolverUpdateAll()
{
$this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
$this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
$this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
$packageA->setRequires(array('b' => new Link('A', 'B', null, 'requires')));
$newPackageA->setRequires(array('b' => new Link('A', 'B', null, 'requires')));
$this->reposComplete();
$this->request->install('A');
$this->request->updateAll();
$this->checkSolverResult(array(
array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
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->install('A');
$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->install('A');
$this->request->install('B');
$this->request->update('A');
$this->checkSolverResult(array(
array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
));
}
2011-12-24 13:15:10 +00:00
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', $this->getVersionConstraint('<', '2.0.0.0'));
2011-12-24 13:15:10 +00:00
$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', $this->getVersionConstraint('<', '2.0.0.0'));
$this->request->update('A', $this->getVersionConstraint('=', '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($packageB = $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', $this->getVersionConstraint('<', '2.0.0.0'));
$this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
2011-12-24 13:15:10 +00:00
$this->checkSolverResult(array(
array(
'job' => 'update',
'from' => $packageA,
'to' => $newPackageA,
),
array(
'job' => 'remove',
'package' => $packageB,
),
));
2011-12-24 13:15:10 +00:00
}
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('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
$this->reposComplete();
$this->request->install('A');
$this->request->install('C');
$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' => 'install', 'package' => $packageA),
array('job' => 'remove', 'package' => $packageD),
));
}
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('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
$packageA->setConflicts(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '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('a' => new Link('B', 'A', new MultiConstraint(array()))));
$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'));
$packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageQ->setProvides(array('b' => new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
$this->reposComplete();
$this->request->install('A');
// must explicitly pick the provider, so error in this case
$this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
$this->solver->solve($this->request);
}
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('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
$this->reposComplete();
$this->request->install('A');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageB),
array('job' => 'install', 'package' => $packageA),
));
}
public function testNoInstallReplacerOfMissingPackage()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
$packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
$this->reposComplete();
$this->request->install('A');
$this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
$this->solver->solve($this->request);
}
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('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '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(
'a' => new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
2015-09-28 09:51:14 +00:00
'b' => new Link('X', 'B', $this->getVersionConstraint('>=', '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('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
// new package A depends on version of package B that does not exist
// => new package A is not installable
$newPackageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
// add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
// but an alternative option for A and B both exists
// this creates a more difficult so solve conflict
$this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
$packageS->setReplaces(array(
'a' => new Link('S', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'),
2015-09-28 09:51:14 +00:00
'b' => new Link('S', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'),
));
$this->reposComplete();
$this->request->install('X');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $newPackageB),
array('job' => 'install', 'package' => $packageA),
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('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageB2->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('>=', '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->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('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageB->setRequires(array('virtual' => new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
$packageC->setProvides(array('virtual' => new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
$packageD->setProvides(array('virtual' => new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
$packageC->setRequires(array('a' => new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
$packageD->setRequires(array('a' => new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
$this->reposComplete();
$this->request->install('A');
$this->request->install('C');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageA),
array('job' => 'install', 'package' => $packageC),
array('job' => 'install', 'package' => $packageB),
));
}
/**
2012-02-18 11:37:45 +00:00
* If a replacer D replaces B and C with C not otherwise available,
* D must be installed instead of the original B.
*/
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(
'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
'c' => new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageD->setReplaces(array(
'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
'c' => new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
));
$packageD2->setReplaces(array(
'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
'c' => new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
));
$this->reposComplete();
$this->request->install('A');
$this->request->install('D');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageD2),
array('job' => 'install', 'package' => $packageA),
));
}
public function testIssue265()
{
$this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
$this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
$this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
$this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
$this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
$this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
$this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
$packageC->setRequires(array(
'a' => new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
'd' => new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
));
$packageD->setRequires(array(
'a' => new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
));
$packageB1->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
$packageB2->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
$packageB2->setReplaces(array('d' => new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
$this->reposComplete();
$this->request->install('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
$this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
$this->solver->solve($this->request);
}
public function testConflictResultEmpty()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
2014-10-17 17:46:01 +00:00
$this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
$packageA->setConflicts(array(
'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '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 result in exception.');
} catch (SolverProblemsException $e) {
$problems = $e->getProblems();
2017-11-30 14:58:10 +00:00
$this->assertCount(1, $problems);
$msg = "\n";
$msg .= " Problem 1\n";
$msg .= " - Installation request for a -> satisfiable by A[1.0].\n";
$msg .= " - B 1.0 conflicts with A[1.0].\n";
$msg .= " - Installation request for b -> satisfiable by B[1.0].\n";
$this->assertEquals($msg, $e->getMessage());
}
}
public function testUnsatisfiableRequires()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
$packageA->setRequires(array(
'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
));
$this->reposComplete();
$this->request->install('A');
try {
$transaction = $this->solver->solve($this->request);
$this->fail('Unsolvable conflict did not result in exception.');
} catch (SolverProblemsException $e) {
$problems = $e->getProblems();
2017-11-30 14:58:10 +00:00
$this->assertCount(1, $problems);
// TODO assert problem properties
$msg = "\n";
$msg .= " Problem 1\n";
$msg .= " - Installation request for a -> satisfiable by A[1.0].\n";
$msg .= " - A 1.0 requires b >= 2.0 -> no matching package found.\n\n";
$msg .= "Potential causes:\n";
$msg .= " - This package is not installable via Composer 1.x, see <https://blog.packagist.com/deprecating-composer-1-support/>\n";
$msg .= " - A typo in the package name\n";
$msg .= " - The package is not available in a stable-enough version according to your minimum-stability setting\n";
$msg .= " see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.\n";
$msg .= " - It's a private package and you forgot to add a custom repository to find it\n\n";
2015-05-04 17:37:57 +00:00
$msg .= "Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.";
$this->assertEquals($msg, $e->getMessage());
}
}
public function testRequireMismatchException()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
$this->repo->addPackage($packageB2 = $this->getPackage('B', '0.9'));
$this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
$this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
$packageA->setRequires(array(
'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageB->setRequires(array(
'c' => new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageC->setRequires(array(
'd' => new Link('C', 'D', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageD->setRequires(array(
'b' => new Link('D', 'B', $this->getVersionConstraint('<', '1.0'), 'requires'),
));
$this->reposComplete();
$this->request->install('A');
try {
$transaction = $this->solver->solve($this->request);
$this->fail('Unsolvable conflict did not result in exception.');
} catch (SolverProblemsException $e) {
$problems = $e->getProblems();
2017-11-30 14:58:10 +00:00
$this->assertCount(1, $problems);
$msg = "\n";
$msg .= " Problem 1\n";
$msg .= " - C 1.0 requires d >= 1.0 -> satisfiable by D[1.0].\n";
$msg .= " - D 1.0 requires b < 1.0 -> satisfiable by B[0.9].\n";
$msg .= " - B 1.0 requires c >= 1.0 -> satisfiable by C[1.0].\n";
$msg .= " - Can only install one of: B[0.9, 1.0].\n";
$msg .= " - A 1.0 requires b >= 1.0 -> satisfiable by B[1.0].\n";
$msg .= " - Installation request for a -> satisfiable by A[1.0].\n";
$this->assertEquals($msg, $e->getMessage());
}
}
public function testLearnLiteralsWithSortedRuleLiterals()
{
$this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
$this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
$this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
$this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
$this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
$packageTwigBridge->setRequires(array(
'twig/twig' => new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
));
$packageSymfony->setReplaces(array(
'symfony/twig-bridge' => new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
));
$this->reposComplete();
$this->request->install('symfony/twig-bridge');
$this->request->install('twig/twig');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageTwig16),
array('job' => 'install', 'package' => $packageTwigBridge),
));
}
public function testInstallRecursiveAliasDependencies()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
$this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
$packageA2->setRequires(array(
'b' => new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
));
$packageB->setRequires(array(
'a' => new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
));
$this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
$this->reposComplete();
$this->request->install('A', $this->getVersionConstraint('==', '1.1.0.0'));
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageA2),
array('job' => 'install', 'package' => $packageB),
array('job' => 'install', 'package' => $packageA2Alias),
));
}
public function testInstallDevAlias()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
$this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
$packageB->setRequires(array(
'a' => new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
));
$this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
$this->reposComplete();
$this->request->install('A', $this->getVersionConstraint('==', '2.0'));
$this->request->install('B');
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageA),
array('job' => 'install', 'package' => $packageAAlias),
array('job' => 'install', 'package' => $packageB),
));
}
Fix solver problem exceptions with unexpected contradictory "Conclusions" This 5 character fix comes with a solver test as well as a functional installer test essentially verifying the same thing. The solver test is more useful when working on the solver. But the functional test is less likely to be accidentally modified incorrectly during refactoring, as every single package, version and link in the rather complex test scenario is essential, and a modified version of the test may very well still result in a successful installation but no longer verify the bug described below. Background: In commit 451bab1c2cd58e05af6e21639b829408ad023463 from May 19, 2012 I refactored literals from complex objects into pure integers to reduce memory consumption. The absolute value of an integer literal is the id of the package it refers to in the package pool. The sign indicates whether the package should be installed (positive) or removed (negative), So a major part of the refactoring was swapping this call: $literal->getPackageId() For this: abs($literal) Unintentionally in line 554/523 I incorrectly applied this change to the line: $this->literalFromId(-$literal->getPackageId()); It was converted to: -abs($literal); The function literalFromId used to create a new literal object. By using the abs() function this change essentially forces the resulting literal to be negative, while the minus sign previously inverted the literal, so positive into negative and vice versa. This particular line is in a function meant to analyze a conflicting decision during dependency resolution and to draw a conclusion from it, then revert the state of the solver to an earlier position, and attempt to solve the rest of the rules again with this new "learned" conclusion. Because of this bug these conclusions could only ever occur in the negative, e.g. "don't install package X". This is by far the most likely scenario when the solver reaches this particular line, but there are exceptions. If you experienced a solver problem description that contained a statement like "Conclusion: don't install vendor/package 1.2.3" which directly contradicted other statements listed as part of the problem, this could likely have been the cause.
2019-02-03 00:53:17 +00:00
/**
* Tests for a bug introduced in commit 451bab1c2cd58e05af6e21639b829408ad023463 Solver.php line 554/523
*
* Every package and link in this test matters, only a combination this complex will run into the situation in which
* a negatively decided literal will need to be learned inverted as a positive assertion.
*
* In particular in this case the goal is to first have the solver decide X 2.0 should not be installed to later
* decide to learn that X 2.0 must be installed and revert decisions to retry solving with this new assumption.
*/
public function testLearnPositiveLiteral()
{
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
$this->repo->addPackage($packageC1 = $this->getPackage('C', '1.0'));
$this->repo->addPackage($packageC2 = $this->getPackage('C', '2.0'));
$this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
$this->repo->addPackage($packageE = $this->getPackage('E', '1.0'));
$this->repo->addPackage($packageF1 = $this->getPackage('F', '1.0'));
$this->repo->addPackage($packageF2 = $this->getPackage('F', '2.0'));
$this->repo->addPackage($packageG1 = $this->getPackage('G', '1.0'));
$this->repo->addPackage($packageG2 = $this->getPackage('G', '2.0'));
$this->repo->addPackage($packageG3 = $this->getPackage('G', '3.0'));
$packageA->setRequires(array(
'b' => new Link('A', 'B', $this->getVersionConstraint('==', '1.0'), 'requires'),
'c' => new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
'd' => new Link('A', 'D', $this->getVersionConstraint('==', '1.0'), 'requires'),
));
$packageB->setRequires(array(
'e' => new Link('B', 'E', $this->getVersionConstraint('==', '1.0'), 'requires'),
));
$packageC1->setRequires(array(
'f' => new Link('C', 'F', $this->getVersionConstraint('==', '1.0'), 'requires'),
));
$packageC2->setRequires(array(
'f' => new Link('C', 'F', $this->getVersionConstraint('==', '1.0'), 'requires'),
'g' => new Link('C', 'G', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageD->setRequires(array(
'f' => new Link('D', 'F', $this->getVersionConstraint('>=', '1.0'), 'requires'),
));
$packageE->setRequires(array(
'g' => new Link('E', 'G', $this->getVersionConstraint('<=', '2.0'), 'requires'),
));
$this->reposComplete();
$this->request->install('A');
// check correct setup for assertion later
$this->assertFalse($this->solver->testFlagLearnedPositiveLiteral);
Fix solver problem exceptions with unexpected contradictory "Conclusions" This 5 character fix comes with a solver test as well as a functional installer test essentially verifying the same thing. The solver test is more useful when working on the solver. But the functional test is less likely to be accidentally modified incorrectly during refactoring, as every single package, version and link in the rather complex test scenario is essential, and a modified version of the test may very well still result in a successful installation but no longer verify the bug described below. Background: In commit 451bab1c2cd58e05af6e21639b829408ad023463 from May 19, 2012 I refactored literals from complex objects into pure integers to reduce memory consumption. The absolute value of an integer literal is the id of the package it refers to in the package pool. The sign indicates whether the package should be installed (positive) or removed (negative), So a major part of the refactoring was swapping this call: $literal->getPackageId() For this: abs($literal) Unintentionally in line 554/523 I incorrectly applied this change to the line: $this->literalFromId(-$literal->getPackageId()); It was converted to: -abs($literal); The function literalFromId used to create a new literal object. By using the abs() function this change essentially forces the resulting literal to be negative, while the minus sign previously inverted the literal, so positive into negative and vice versa. This particular line is in a function meant to analyze a conflicting decision during dependency resolution and to draw a conclusion from it, then revert the state of the solver to an earlier position, and attempt to solve the rest of the rules again with this new "learned" conclusion. Because of this bug these conclusions could only ever occur in the negative, e.g. "don't install package X". This is by far the most likely scenario when the solver reaches this particular line, but there are exceptions. If you experienced a solver problem description that contained a statement like "Conclusion: don't install vendor/package 1.2.3" which directly contradicted other statements listed as part of the problem, this could likely have been the cause.
2019-02-03 00:53:17 +00:00
$this->checkSolverResult(array(
array('job' => 'install', 'package' => $packageF1),
array('job' => 'install', 'package' => $packageD),
array('job' => 'install', 'package' => $packageG2),
array('job' => 'install', 'package' => $packageC2),
array('job' => 'install', 'package' => $packageE),
array('job' => 'install', 'package' => $packageB),
array('job' => 'install', 'package' => $packageA),
));
// verify that the code path leading to a negative literal resulting in a positive learned literal is actually
// executed
$this->assertTrue($this->solver->testFlagLearnedPositiveLiteral);
Fix solver problem exceptions with unexpected contradictory "Conclusions" This 5 character fix comes with a solver test as well as a functional installer test essentially verifying the same thing. The solver test is more useful when working on the solver. But the functional test is less likely to be accidentally modified incorrectly during refactoring, as every single package, version and link in the rather complex test scenario is essential, and a modified version of the test may very well still result in a successful installation but no longer verify the bug described below. Background: In commit 451bab1c2cd58e05af6e21639b829408ad023463 from May 19, 2012 I refactored literals from complex objects into pure integers to reduce memory consumption. The absolute value of an integer literal is the id of the package it refers to in the package pool. The sign indicates whether the package should be installed (positive) or removed (negative), So a major part of the refactoring was swapping this call: $literal->getPackageId() For this: abs($literal) Unintentionally in line 554/523 I incorrectly applied this change to the line: $this->literalFromId(-$literal->getPackageId()); It was converted to: -abs($literal); The function literalFromId used to create a new literal object. By using the abs() function this change essentially forces the resulting literal to be negative, while the minus sign previously inverted the literal, so positive into negative and vice versa. This particular line is in a function meant to analyze a conflicting decision during dependency resolution and to draw a conclusion from it, then revert the state of the solver to an earlier position, and attempt to solve the rest of the rules again with this new "learned" conclusion. Because of this bug these conclusions could only ever occur in the negative, e.g. "don't install package X". This is by far the most likely scenario when the solver reaches this particular line, but there are exceptions. If you experienced a solver problem description that contained a statement like "Conclusion: don't install vendor/package 1.2.3" which directly contradicted other statements listed as part of the problem, this could likely have been the cause.
2019-02-03 00:53:17 +00:00
}
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(
2017-03-08 14:07:29 +00:00
'job' => 'update',
'from' => $operation->getInitialPackage(),
2017-03-08 14:07:29 +00:00
'to' => $operation->getTargetPackage(),
);
} else {
$job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
$result[] = array(
2017-03-08 14:07:29 +00:00
'job' => $job,
2015-09-28 09:51:14 +00:00
'package' => $operation->getPackage(),
);
}
}
2011-05-23 00:23:21 +00:00
$this->assertEquals($expected, $result);
}
}