SolverTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\DependencyResolver;
  12. use Composer\IO\NullIO;
  13. use Composer\Repository\ArrayRepository;
  14. use Composer\Repository\LockArrayRepository;
  15. use Composer\DependencyResolver\DefaultPolicy;
  16. use Composer\DependencyResolver\Pool;
  17. use Composer\DependencyResolver\Request;
  18. use Composer\DependencyResolver\Solver;
  19. use Composer\DependencyResolver\SolverProblemsException;
  20. use Composer\Package\Link;
  21. use Composer\Repository\InstalledArrayRepository;
  22. use Composer\Repository\RepositorySet;
  23. use Composer\Test\TestCase;
  24. use Composer\Semver\Constraint\MultiConstraint;
  25. use Composer\Semver\Constraint\EmptyConstraint;
  26. class SolverTest extends TestCase
  27. {
  28. protected $repoSet;
  29. protected $repo;
  30. protected $repoLocked;
  31. protected $request;
  32. protected $policy;
  33. protected $solver;
  34. protected $pool;
  35. public function setUp()
  36. {
  37. $this->repoSet = new RepositorySet();
  38. $this->repo = new ArrayRepository;
  39. $this->repoLocked = new LockArrayRepository;
  40. $this->request = new Request($this->repoLocked);
  41. $this->policy = new DefaultPolicy;
  42. }
  43. public function testSolverInstallSingle()
  44. {
  45. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  46. $this->reposComplete();
  47. $this->request->requireName('A');
  48. $this->checkSolverResult(array(
  49. array('job' => 'install', 'package' => $packageA),
  50. ));
  51. }
  52. public function testSolverRemoveIfNotRequested()
  53. {
  54. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  55. $this->reposComplete();
  56. $this->checkSolverResult(array(
  57. array('job' => 'remove', 'package' => $packageA),
  58. ));
  59. }
  60. public function testInstallNonExistingPackageFails()
  61. {
  62. $this->repo->addPackage($this->getPackage('A', '1.0'));
  63. $this->reposComplete();
  64. $this->request->requireName('B', $this->getVersionConstraint('==', '1'));
  65. $this->createSolver();
  66. try {
  67. $transaction = $this->solver->solve($this->request);
  68. $this->fail('Unsolvable conflict did not result in exception.');
  69. } catch (SolverProblemsException $e) {
  70. $problems = $e->getProblems();
  71. $this->assertCount(1, $problems);
  72. $this->assertEquals(2, $e->getCode());
  73. $this->assertEquals("\n - Root composer.json requires b, it could not be found in any version, there may be a typo in the package name.", $problems[0]->getPrettyString($this->repoSet, $this->request, $this->pool, false));
  74. }
  75. }
  76. public function testSolverInstallSamePackageFromDifferentRepositories()
  77. {
  78. $repo1 = new ArrayRepository;
  79. $repo2 = new ArrayRepository;
  80. $repo1->addPackage($foo1 = $this->getPackage('foo', '1'));
  81. $repo2->addPackage($foo2 = $this->getPackage('foo', '1'));
  82. $this->repoSet->addRepository($repo1);
  83. $this->repoSet->addRepository($repo2);
  84. $this->request->requireName('foo');
  85. $this->checkSolverResult(array(
  86. array('job' => 'install', 'package' => $foo1),
  87. ));
  88. }
  89. public function testSolverInstallWithDeps()
  90. {
  91. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  92. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  93. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  94. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  95. $this->reposComplete();
  96. $this->request->requireName('A');
  97. $this->checkSolverResult(array(
  98. array('job' => 'install', 'package' => $packageB),
  99. array('job' => 'install', 'package' => $packageA),
  100. ));
  101. }
  102. public function testSolverInstallHonoursNotEqualOperator()
  103. {
  104. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  105. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  106. $this->repo->addPackage($newPackageB11 = $this->getPackage('B', '1.1'));
  107. $this->repo->addPackage($newPackageB12 = $this->getPackage('B', '1.2'));
  108. $this->repo->addPackage($newPackageB13 = $this->getPackage('B', '1.3'));
  109. $packageA->setRequires(array(
  110. 'b' => new Link('A', 'B', new MultiConstraint(array(
  111. $this->getVersionConstraint('<=', '1.3'),
  112. $this->getVersionConstraint('<>', '1.3'),
  113. $this->getVersionConstraint('!=', '1.2'),
  114. )), 'requires'),
  115. ));
  116. $this->reposComplete();
  117. $this->request->requireName('A');
  118. $this->checkSolverResult(array(
  119. array('job' => 'install', 'package' => $newPackageB11),
  120. array('job' => 'install', 'package' => $packageA),
  121. ));
  122. }
  123. public function testSolverInstallWithDepsInOrder()
  124. {
  125. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  126. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  127. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  128. $packageB->setRequires(array(
  129. 'a' => new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  130. 'c' => new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  131. ));
  132. $packageC->setRequires(array(
  133. 'a' => new Link('C', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  134. ));
  135. $this->reposComplete();
  136. $this->request->requireName('A');
  137. $this->request->requireName('B');
  138. $this->request->requireName('C');
  139. $this->checkSolverResult(array(
  140. array('job' => 'install', 'package' => $packageA),
  141. array('job' => 'install', 'package' => $packageC),
  142. array('job' => 'install', 'package' => $packageB),
  143. ));
  144. }
  145. public function testSolverFixLocked()
  146. {
  147. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  148. $this->reposComplete();
  149. $this->request->fixPackage($packageA);
  150. $this->checkSolverResult(array());
  151. }
  152. public function testSolverFixLockedWithAlternative()
  153. {
  154. $this->repo->addPackage($this->getPackage('A', '1.0'));
  155. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  156. $this->reposComplete();
  157. $this->request->fixPackage($packageA);
  158. $this->checkSolverResult(array());
  159. }
  160. public function testSolverUpdateDoesOnlyUpdate()
  161. {
  162. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  163. $this->repoLocked->addPackage($packageB = $this->getPackage('B', '1.0'));
  164. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  165. $this->reposComplete();
  166. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0.0.0'), 'requires')));
  167. $this->request->fixPackage($packageA);
  168. $this->request->requireName('B', $this->getVersionConstraint('=', '1.1.0.0'));
  169. $this->checkSolverResult(array(
  170. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  171. ));
  172. }
  173. public function testSolverUpdateSingle()
  174. {
  175. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  176. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  177. $this->reposComplete();
  178. $this->request->requireName('A');
  179. $this->checkSolverResult(array(
  180. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  181. ));
  182. }
  183. public function testSolverUpdateAll()
  184. {
  185. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  186. $this->repoLocked->addPackage($packageB = $this->getPackage('B', '1.0'));
  187. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  188. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  189. $packageA->setRequires(array('b' => new Link('A', 'B', null, 'requires')));
  190. $newPackageA->setRequires(array('b' => new Link('A', 'B', null, 'requires')));
  191. $this->reposComplete();
  192. $this->request->requireName('A');
  193. $this->checkSolverResult(array(
  194. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  195. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  196. ));
  197. }
  198. public function testSolverUpdateCurrent()
  199. {
  200. $this->repoLocked->addPackage($this->getPackage('A', '1.0'));
  201. $this->repo->addPackage($this->getPackage('A', '1.0'));
  202. $this->reposComplete();
  203. $this->request->requireName('A');
  204. $this->checkSolverResult(array());
  205. }
  206. public function testSolverUpdateOnlyUpdatesSelectedPackage()
  207. {
  208. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  209. $this->repoLocked->addPackage($packageB = $this->getPackage('B', '1.0'));
  210. $this->repo->addPackage($packageAnewer = $this->getPackage('A', '1.1'));
  211. $this->repo->addPackage($packageBnewer = $this->getPackage('B', '1.1'));
  212. $this->reposComplete();
  213. $this->request->requireName('A');
  214. $this->request->fixPackage($packageB);
  215. $this->checkSolverResult(array(
  216. array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
  217. ));
  218. }
  219. public function testSolverUpdateConstrained()
  220. {
  221. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  222. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  223. $this->repo->addPackage($this->getPackage('A', '2.0'));
  224. $this->reposComplete();
  225. $this->request->requireName('A', $this->getVersionConstraint('<', '2.0.0.0'));
  226. $this->checkSolverResult(array(array(
  227. 'job' => 'update',
  228. 'from' => $packageA,
  229. 'to' => $newPackageA,
  230. )));
  231. }
  232. public function testSolverUpdateFullyConstrained()
  233. {
  234. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  235. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  236. $this->repo->addPackage($this->getPackage('A', '2.0'));
  237. $this->reposComplete();
  238. $this->request->requireName('A', $this->getVersionConstraint('<', '2.0.0.0'));
  239. $this->checkSolverResult(array(array(
  240. 'job' => 'update',
  241. 'from' => $packageA,
  242. 'to' => $newPackageA,
  243. )));
  244. }
  245. public function testSolverUpdateFullyConstrainedPrunesInstalledPackages()
  246. {
  247. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  248. $this->repoLocked->addPackage($packageB = $this->getPackage('B', '1.0'));
  249. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  250. $this->repo->addPackage($this->getPackage('A', '2.0'));
  251. $this->reposComplete();
  252. $this->request->requireName('A', $this->getVersionConstraint('<', '2.0.0.0'));
  253. $this->checkSolverResult(array(
  254. array(
  255. 'job' => 'remove',
  256. 'package' => $packageB,
  257. ),
  258. array(
  259. 'job' => 'update',
  260. 'from' => $packageA,
  261. 'to' => $newPackageA,
  262. ),
  263. ));
  264. }
  265. public function testSolverAllJobs()
  266. {
  267. $this->repoLocked->addPackage($packageD = $this->getPackage('D', '1.0'));
  268. $this->repoLocked->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  269. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  270. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  271. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  272. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  273. $this->repo->addPackage($this->getPackage('D', '1.0'));
  274. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  275. $this->reposComplete();
  276. $this->request->requireName('A');
  277. $this->request->requireName('C');
  278. $this->checkSolverResult(array(
  279. array('job' => 'remove', 'package' => $packageD),
  280. array('job' => 'install', 'package' => $packageB),
  281. array('job' => 'install', 'package' => $packageA),
  282. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  283. ));
  284. }
  285. public function testSolverThreeAlternativeRequireAndConflict()
  286. {
  287. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  288. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  289. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  290. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  291. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  292. $packageA->setConflicts(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.0'), 'conflicts')));
  293. $this->reposComplete();
  294. $this->request->requireName('A');
  295. $this->checkSolverResult(array(
  296. array('job' => 'install', 'package' => $middlePackageB),
  297. array('job' => 'install', 'package' => $packageA),
  298. ));
  299. }
  300. public function testSolverObsolete()
  301. {
  302. $this->repoLocked->addPackage($packageA = $this->getPackage('A', '1.0'));
  303. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  304. $packageB->setReplaces(array('a' => new Link('B', 'A', new EmptyConstraint())));
  305. $this->reposComplete();
  306. $this->request->requireName('B');
  307. $this->checkSolverResult(array(
  308. array('job' => 'remove', 'package' => $packageA),
  309. array('job' => 'install', 'package' => $packageB),
  310. ));
  311. }
  312. public function testInstallOneOfTwoAlternatives()
  313. {
  314. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  315. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  316. $this->reposComplete();
  317. $this->request->requireName('A');
  318. $this->checkSolverResult(array(
  319. array('job' => 'install', 'package' => $packageA),
  320. ));
  321. }
  322. public function testInstallProvider()
  323. {
  324. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  325. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  326. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  327. $packageQ->setProvides(array('b' => new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
  328. $this->reposComplete();
  329. $this->request->requireName('A');
  330. // must explicitly pick the provider, so error in this case
  331. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  332. $this->createSolver();
  333. $this->solver->solve($this->request);
  334. }
  335. public function testSkipReplacerOfExistingPackage()
  336. {
  337. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  338. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  339. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  340. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  341. $packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  342. $this->reposComplete();
  343. $this->request->requireName('A');
  344. $this->checkSolverResult(array(
  345. array('job' => 'install', 'package' => $packageB),
  346. array('job' => 'install', 'package' => $packageA),
  347. ));
  348. }
  349. public function testNoInstallReplacerOfMissingPackage()
  350. {
  351. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  352. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  353. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  354. $packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  355. $this->reposComplete();
  356. $this->request->requireName('A');
  357. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  358. $this->createSolver();
  359. $this->solver->solve($this->request);
  360. }
  361. public function testSkipReplacedPackageIfReplacerIsSelected()
  362. {
  363. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  364. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  365. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  366. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  367. $packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  368. $this->reposComplete();
  369. $this->request->requireName('A');
  370. $this->request->requireName('Q');
  371. $this->checkSolverResult(array(
  372. array('job' => 'install', 'package' => $packageQ),
  373. array('job' => 'install', 'package' => $packageA),
  374. ));
  375. }
  376. public function testPickOlderIfNewerConflicts()
  377. {
  378. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  379. $packageX->setRequires(array(
  380. 'a' => new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  381. 'b' => new Link('X', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  382. ));
  383. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  384. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  385. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  386. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  387. // new package A depends on version of package B that does not exist
  388. // => new package A is not installable
  389. $newPackageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
  390. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  391. // but an alternative option for A and B both exists
  392. // this creates a more difficult so solve conflict
  393. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  394. $packageS->setReplaces(array(
  395. 'a' => new Link('S', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'),
  396. 'b' => new Link('S', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'),
  397. ));
  398. $this->reposComplete();
  399. $this->request->requireName('X');
  400. $this->checkSolverResult(array(
  401. array('job' => 'install', 'package' => $newPackageB),
  402. array('job' => 'install', 'package' => $packageA),
  403. array('job' => 'install', 'package' => $packageX),
  404. ));
  405. }
  406. public function testInstallCircularRequire()
  407. {
  408. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  409. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  410. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  411. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  412. $packageB2->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  413. $this->reposComplete();
  414. $this->request->requireName('A');
  415. $this->checkSolverResult(array(
  416. array('job' => 'install', 'package' => $packageB2),
  417. array('job' => 'install', 'package' => $packageA),
  418. ));
  419. }
  420. public function testInstallAlternativeWithCircularRequire()
  421. {
  422. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  423. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  424. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  425. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  426. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  427. $packageB->setRequires(array('virtual' => new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  428. $packageC->setProvides(array('virtual' => new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  429. $packageD->setProvides(array('virtual' => new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  430. $packageC->setRequires(array('a' => new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  431. $packageD->setRequires(array('a' => new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  432. $this->reposComplete();
  433. $this->request->requireName('A');
  434. $this->request->requireName('C');
  435. $this->checkSolverResult(array(
  436. array('job' => 'install', 'package' => $packageB),
  437. array('job' => 'install', 'package' => $packageA),
  438. array('job' => 'install', 'package' => $packageC),
  439. ));
  440. }
  441. /**
  442. * If a replacer D replaces B and C with C not otherwise available,
  443. * D must be installed instead of the original B.
  444. */
  445. public function testUseReplacerIfNecessary()
  446. {
  447. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  448. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  449. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  450. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  451. $packageA->setRequires(array(
  452. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  453. 'c' => new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  454. ));
  455. $packageD->setReplaces(array(
  456. 'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  457. 'c' => new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  458. ));
  459. $packageD2->setReplaces(array(
  460. 'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  461. 'c' => new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  462. ));
  463. $this->reposComplete();
  464. $this->request->requireName('A');
  465. $this->request->requireName('D');
  466. $this->checkSolverResult(array(
  467. array('job' => 'install', 'package' => $packageD2),
  468. array('job' => 'install', 'package' => $packageA),
  469. ));
  470. }
  471. public function testIssue265()
  472. {
  473. $this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
  474. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
  475. $this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
  476. $this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
  477. $this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
  478. $this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
  479. $this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
  480. $packageC->setRequires(array(
  481. 'a' => new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  482. 'd' => new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  483. ));
  484. $packageD->setRequires(array(
  485. 'a' => new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
  486. 'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
  487. ));
  488. $packageB1->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  489. $packageB2->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  490. $packageB2->setReplaces(array('d' => new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
  491. $this->reposComplete();
  492. $this->request->requireName('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
  493. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  494. $this->createSolver();
  495. $this->solver->solve($this->request);
  496. }
  497. public function testConflictResultEmpty()
  498. {
  499. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  500. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  501. $packageA->setConflicts(array(
  502. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'conflicts'),
  503. ));
  504. $this->reposComplete();
  505. $this->request->requireName('A');
  506. $this->request->requireName('B');
  507. $this->createSolver();
  508. try {
  509. $transaction = $this->solver->solve($this->request);
  510. $this->fail('Unsolvable conflict did not result in exception.');
  511. } catch (SolverProblemsException $e) {
  512. $problems = $e->getProblems();
  513. $this->assertCount(1, $problems);
  514. $msg = "\n";
  515. $msg .= " Problem 1\n";
  516. $msg .= " - Root composer.json requires a -> satisfiable by A[1.0].\n";
  517. $msg .= " - A 1.0 conflicts with B 1.0.\n";
  518. $msg .= " - Root composer.json requires b -> satisfiable by B[1.0].\n";
  519. $this->assertEquals($msg, $e->getPrettyString($this->repoSet, $this->request, $this->pool, false));
  520. }
  521. }
  522. public function testUnsatisfiableRequires()
  523. {
  524. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  525. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  526. $packageA->setRequires(array(
  527. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  528. ));
  529. $this->reposComplete();
  530. $this->request->requireName('A');
  531. $this->createSolver();
  532. try {
  533. $transaction = $this->solver->solve($this->request);
  534. $this->fail('Unsolvable conflict did not result in exception.');
  535. } catch (SolverProblemsException $e) {
  536. $problems = $e->getProblems();
  537. $this->assertCount(1, $problems);
  538. // TODO assert problem properties
  539. $msg = "\n";
  540. $msg .= " Problem 1\n";
  541. $msg .= " - Root composer.json requires a -> satisfiable by A[1.0].\n";
  542. $msg .= " - A 1.0 requires b >= 2.0 -> found B[1.0] but it does not match the constraint.\n";
  543. $this->assertEquals($msg, $e->getPrettyString($this->repoSet, $this->request, $this->pool, false));
  544. }
  545. }
  546. public function testRequireMismatchException()
  547. {
  548. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  549. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  550. $this->repo->addPackage($packageB2 = $this->getPackage('B', '0.9'));
  551. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  552. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  553. $packageA->setRequires(array(
  554. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  555. ));
  556. $packageB->setRequires(array(
  557. 'c' => new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  558. ));
  559. $packageC->setRequires(array(
  560. 'd' => new Link('C', 'D', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  561. ));
  562. $packageD->setRequires(array(
  563. 'b' => new Link('D', 'B', $this->getVersionConstraint('<', '1.0'), 'requires'),
  564. ));
  565. $this->reposComplete();
  566. $this->request->requireName('A');
  567. $this->createSolver();
  568. try {
  569. $transaction = $this->solver->solve($this->request);
  570. $this->fail('Unsolvable conflict did not result in exception.');
  571. } catch (SolverProblemsException $e) {
  572. $problems = $e->getProblems();
  573. $this->assertCount(1, $problems);
  574. $msg = "\n";
  575. $msg .= " Problem 1\n";
  576. $msg .= " - C 1.0 requires d >= 1.0 -> satisfiable by D[1.0].\n";
  577. $msg .= " - D 1.0 requires b < 1.0 -> satisfiable by B[0.9].\n";
  578. $msg .= " - B 1.0 requires c >= 1.0 -> satisfiable by C[1.0].\n";
  579. $msg .= " - You can only install one version of a package, so only one of these can be installed: B[0.9, 1.0].\n";
  580. $msg .= " - A 1.0 requires b >= 1.0 -> satisfiable by B[1.0].\n";
  581. $msg .= " - Root composer.json requires a -> satisfiable by A[1.0].\n";
  582. $this->assertEquals($msg, $e->getPrettyString($this->repoSet, $this->request, $this->pool, false));
  583. }
  584. }
  585. public function testLearnLiteralsWithSortedRuleLiterals()
  586. {
  587. $this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
  588. $this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
  589. $this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
  590. $this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
  591. $this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
  592. $packageTwigBridge->setRequires(array(
  593. 'twig/twig' => new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
  594. ));
  595. $packageSymfony->setReplaces(array(
  596. 'symfony/twig-bridge' => new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
  597. ));
  598. $this->reposComplete();
  599. $this->request->requireName('symfony/twig-bridge');
  600. $this->request->requireName('twig/twig');
  601. $this->checkSolverResult(array(
  602. array('job' => 'install', 'package' => $packageTwig16),
  603. array('job' => 'install', 'package' => $packageTwigBridge),
  604. ));
  605. }
  606. public function testInstallRecursiveAliasDependencies()
  607. {
  608. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  609. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  610. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  611. $packageA2->setRequires(array(
  612. 'b' => new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
  613. ));
  614. $packageB->setRequires(array(
  615. 'a' => new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  616. ));
  617. $this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
  618. $this->reposComplete();
  619. $this->request->requireName('A', $this->getVersionConstraint('==', '1.1.0.0'));
  620. $this->checkSolverResult(array(
  621. array('job' => 'install', 'package' => $packageB),
  622. array('job' => 'install', 'package' => $packageA2),
  623. array('job' => 'install', 'package' => $packageA2Alias),
  624. ));
  625. }
  626. public function testInstallDevAlias()
  627. {
  628. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  629. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  630. $packageB->setRequires(array(
  631. 'a' => new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
  632. ));
  633. $this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
  634. $this->reposComplete();
  635. $this->request->requireName('A', $this->getVersionConstraint('==', '2.0'));
  636. $this->request->requireName('B');
  637. $this->checkSolverResult(array(
  638. array('job' => 'install', 'package' => $packageA),
  639. array('job' => 'install', 'package' => $packageAAlias),
  640. array('job' => 'install', 'package' => $packageB),
  641. ));
  642. }
  643. /**
  644. * Tests for a bug introduced in commit 451bab1c2cd58e05af6e21639b829408ad023463 Solver.php line 554/523
  645. *
  646. * Every package and link in this test matters, only a combination this complex will run into the situation in which
  647. * a negatively decided literal will need to be learned inverted as a positive assertion.
  648. *
  649. * In particular in this case the goal is to first have the solver decide X 2.0 should not be installed to later
  650. * decide to learn that X 2.0 must be installed and revert decisions to retry solving with this new assumption.
  651. */
  652. public function testLearnPositiveLiteral()
  653. {
  654. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  655. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  656. $this->repo->addPackage($packageC1 = $this->getPackage('C', '1.0'));
  657. $this->repo->addPackage($packageC2 = $this->getPackage('C', '2.0'));
  658. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  659. $this->repo->addPackage($packageE = $this->getPackage('E', '1.0'));
  660. $this->repo->addPackage($packageF1 = $this->getPackage('F', '1.0'));
  661. $this->repo->addPackage($packageF2 = $this->getPackage('F', '2.0'));
  662. $this->repo->addPackage($packageG1 = $this->getPackage('G', '1.0'));
  663. $this->repo->addPackage($packageG2 = $this->getPackage('G', '2.0'));
  664. $this->repo->addPackage($packageG3 = $this->getPackage('G', '3.0'));
  665. $packageA->setRequires(array(
  666. 'b' => new Link('A', 'B', $this->getVersionConstraint('==', '1.0'), 'requires'),
  667. 'c' => new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  668. 'd' => new Link('A', 'D', $this->getVersionConstraint('==', '1.0'), 'requires'),
  669. ));
  670. $packageB->setRequires(array(
  671. 'e' => new Link('B', 'E', $this->getVersionConstraint('==', '1.0'), 'requires'),
  672. ));
  673. $packageC1->setRequires(array(
  674. 'f' => new Link('C', 'F', $this->getVersionConstraint('==', '1.0'), 'requires'),
  675. ));
  676. $packageC2->setRequires(array(
  677. 'f' => new Link('C', 'F', $this->getVersionConstraint('==', '1.0'), 'requires'),
  678. 'g' => new Link('C', 'G', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  679. ));
  680. $packageD->setRequires(array(
  681. 'f' => new Link('D', 'F', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  682. ));
  683. $packageE->setRequires(array(
  684. 'g' => new Link('E', 'G', $this->getVersionConstraint('<=', '2.0'), 'requires'),
  685. ));
  686. $this->reposComplete();
  687. $this->request->requireName('A');
  688. $this->createSolver();
  689. // check correct setup for assertion later
  690. $this->assertFalse($this->solver->testFlagLearnedPositiveLiteral);
  691. $this->checkSolverResult(array(
  692. array('job' => 'install', 'package' => $packageF1),
  693. array('job' => 'install', 'package' => $packageD),
  694. array('job' => 'install', 'package' => $packageG2),
  695. array('job' => 'install', 'package' => $packageC2),
  696. array('job' => 'install', 'package' => $packageE),
  697. array('job' => 'install', 'package' => $packageB),
  698. array('job' => 'install', 'package' => $packageA),
  699. ));
  700. // verify that the code path leading to a negative literal resulting in a positive learned literal is actually
  701. // executed
  702. $this->assertTrue($this->solver->testFlagLearnedPositiveLiteral);
  703. }
  704. protected function reposComplete()
  705. {
  706. $this->repoSet->addRepository($this->repo);
  707. $this->repoSet->addRepository($this->repoLocked);
  708. }
  709. protected function createSolver()
  710. {
  711. $io = new NullIO();
  712. $this->pool = $this->repoSet->createPool($this->request, $io);
  713. $this->solver = new Solver($this->policy, $this->pool, $io);
  714. }
  715. protected function checkSolverResult(array $expected)
  716. {
  717. $this->createSolver();
  718. $transaction = $this->solver->solve($this->request);
  719. $result = array();
  720. foreach ($transaction->getOperations() as $operation) {
  721. if ('update' === $operation->getOperationType()) {
  722. $result[] = array(
  723. 'job' => 'update',
  724. 'from' => $operation->getInitialPackage(),
  725. 'to' => $operation->getTargetPackage(),
  726. );
  727. } else {
  728. $job = ('uninstall' === $operation->getOperationType() ? 'remove' : 'install');
  729. $result[] = array(
  730. 'job' => $job,
  731. 'package' => $operation->getPackage(),
  732. );
  733. }
  734. }
  735. $this->assertEquals($expected, $result);
  736. }
  737. }