SolverTest.php 38 KB

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