SolverTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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\Repository\ArrayRepository;
  13. use Composer\DependencyResolver\DefaultPolicy;
  14. use Composer\DependencyResolver\Pool;
  15. use Composer\DependencyResolver\Request;
  16. use Composer\DependencyResolver\Solver;
  17. use Composer\DependencyResolver\SolverProblemsException;
  18. use Composer\Package\Link;
  19. use Composer\Test\TestCase;
  20. class SolverTest extends TestCase
  21. {
  22. protected $pool;
  23. protected $repo;
  24. protected $repoInstalled;
  25. protected $request;
  26. protected $policy;
  27. public function setUp()
  28. {
  29. $this->pool = new Pool;
  30. $this->repo = new ArrayRepository;
  31. $this->repoInstalled = new ArrayRepository;
  32. $this->request = new Request($this->pool);
  33. $this->policy = new DefaultPolicy;
  34. $this->solver = new Solver($this->policy, $this->pool, $this->repoInstalled);
  35. }
  36. public function testSolverInstallSingle()
  37. {
  38. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  39. $this->reposComplete();
  40. $this->request->install('A');
  41. $this->checkSolverResult(array(
  42. array('job' => 'install', 'package' => $packageA),
  43. ));
  44. }
  45. public function testSolverRemoveIfNotInstalled()
  46. {
  47. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  48. $this->reposComplete();
  49. $this->checkSolverResult(array(
  50. array('job' => 'remove', 'package' => $packageA),
  51. ));
  52. }
  53. public function testInstallNonExistingPackageFails()
  54. {
  55. $this->repo->addPackage($this->getPackage('A', '1.0'));
  56. $this->reposComplete();
  57. $this->request->install('B', $this->getVersionConstraint('==', '1'));
  58. try {
  59. $transaction = $this->solver->solve($this->request);
  60. $this->fail('Unsolvable conflict did not result in exception.');
  61. } catch (SolverProblemsException $e) {
  62. $problems = $e->getProblems();
  63. $this->assertEquals(1, count($problems));
  64. $this->assertEquals("\n - The requested package b == 1 could not be found.", $problems[0]->getPrettyString());
  65. }
  66. }
  67. public function testSolverInstallSamePackageFromDifferentRepositories()
  68. {
  69. $repo1 = new ArrayRepository;
  70. $repo2 = new ArrayRepository;
  71. $repo1->addPackage($foo1 = $this->getPackage('foo', '1'));
  72. $repo2->addPackage($foo2 = $this->getPackage('foo', '1'));
  73. $this->pool->addRepository($this->repoInstalled);
  74. $this->pool->addRepository($repo1);
  75. $this->pool->addRepository($repo2);
  76. $this->request->install('foo');
  77. $this->checkSolverResult(array(
  78. array('job' => 'install', 'package' => $foo1),
  79. ));
  80. }
  81. public function testSolverInstallWithDeps()
  82. {
  83. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  84. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  85. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  86. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  87. $this->reposComplete();
  88. $this->request->install('A');
  89. $this->checkSolverResult(array(
  90. array('job' => 'install', 'package' => $packageB),
  91. array('job' => 'install', 'package' => $packageA),
  92. ));
  93. }
  94. public function testSolverInstallHonoursNotEqualOperator()
  95. {
  96. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  97. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  98. $this->repo->addPackage($newPackageB11 = $this->getPackage('B', '1.1'));
  99. $this->repo->addPackage($newPackageB12 = $this->getPackage('B', '1.2'));
  100. $this->repo->addPackage($newPackageB13 = $this->getPackage('B', '1.3'));
  101. $packageA->setRequires(array(
  102. new Link('A', 'B', $this->getVersionConstraint('<=', '1.3'), 'requires'),
  103. new Link('A', 'B', $this->getVersionConstraint('<>', '1.3'), 'requires'),
  104. new Link('A', 'B', $this->getVersionConstraint('!=', '1.2'), 'requires'),
  105. ));
  106. $this->reposComplete();
  107. $this->request->install('A');
  108. $this->checkSolverResult(array(
  109. array('job' => 'install', 'package' => $newPackageB11),
  110. array('job' => 'install', 'package' => $packageA),
  111. ));
  112. }
  113. public function testSolverInstallWithDepsInOrder()
  114. {
  115. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  116. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  117. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  118. $packageB->setRequires(array(
  119. new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  120. new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  121. ));
  122. $packageC->setRequires(array(
  123. new Link('C', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  124. ));
  125. $this->reposComplete();
  126. $this->request->install('A');
  127. $this->request->install('B');
  128. $this->request->install('C');
  129. $this->checkSolverResult(array(
  130. array('job' => 'install', 'package' => $packageA),
  131. array('job' => 'install', 'package' => $packageC),
  132. array('job' => 'install', 'package' => $packageB),
  133. ));
  134. }
  135. public function testSolverInstallInstalled()
  136. {
  137. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  138. $this->reposComplete();
  139. $this->request->install('A');
  140. $this->checkSolverResult(array());
  141. }
  142. public function testSolverInstallInstalledWithAlternative()
  143. {
  144. $this->repo->addPackage($this->getPackage('A', '1.0'));
  145. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  146. $this->reposComplete();
  147. $this->request->install('A');
  148. $this->checkSolverResult(array());
  149. }
  150. public function testSolverRemoveSingle()
  151. {
  152. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  153. $this->reposComplete();
  154. $this->request->remove('A');
  155. $this->checkSolverResult(array(
  156. array('job' => 'remove', 'package' => $packageA),
  157. ));
  158. }
  159. public function testSolverRemoveUninstalled()
  160. {
  161. $this->repo->addPackage($this->getPackage('A', '1.0'));
  162. $this->reposComplete();
  163. $this->request->remove('A');
  164. $this->checkSolverResult(array());
  165. }
  166. public function testSolverUpdateDoesOnlyUpdate()
  167. {
  168. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  169. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  170. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  171. $this->reposComplete();
  172. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0.0.0'), 'requires')));
  173. $this->request->install('A', $this->getVersionConstraint('=', '1.0.0.0'));
  174. $this->request->install('B', $this->getVersionConstraint('=', '1.1.0.0'));
  175. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  176. $this->request->update('B', $this->getVersionConstraint('=', '1.0.0.0'));
  177. $this->checkSolverResult(array(
  178. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  179. ));
  180. }
  181. public function testSolverUpdateSingle()
  182. {
  183. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  184. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  185. $this->reposComplete();
  186. $this->request->install('A');
  187. $this->request->update('A');
  188. $this->checkSolverResult(array(
  189. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  190. ));
  191. }
  192. public function testSolverUpdateAll()
  193. {
  194. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  195. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  196. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  197. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  198. $packageA->setRequires(array(new Link('A', 'B', null, 'requires')));
  199. $newPackageA->setRequires(array(new Link('A', 'B', null, 'requires')));
  200. $this->reposComplete();
  201. $this->request->install('A');
  202. $this->request->updateAll();
  203. $this->checkSolverResult(array(
  204. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  205. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  206. ));
  207. }
  208. public function testSolverUpdateCurrent()
  209. {
  210. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  211. $this->repo->addPackage($this->getPackage('A', '1.0'));
  212. $this->reposComplete();
  213. $this->request->install('A');
  214. $this->request->update('A');
  215. $this->checkSolverResult(array());
  216. }
  217. public function testSolverUpdateOnlyUpdatesSelectedPackage()
  218. {
  219. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  220. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  221. $this->repo->addPackage($packageAnewer = $this->getPackage('A', '1.1'));
  222. $this->repo->addPackage($packageBnewer = $this->getPackage('B', '1.1'));
  223. $this->reposComplete();
  224. $this->request->install('A');
  225. $this->request->install('B');
  226. $this->request->update('A');
  227. $this->checkSolverResult(array(
  228. array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
  229. ));
  230. }
  231. public function testSolverUpdateConstrained()
  232. {
  233. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  234. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  235. $this->repo->addPackage($this->getPackage('A', '2.0'));
  236. $this->reposComplete();
  237. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  238. $this->request->update('A');
  239. $this->checkSolverResult(array(array(
  240. 'job' => 'update',
  241. 'from' => $packageA,
  242. 'to' => $newPackageA,
  243. )));
  244. }
  245. public function testSolverUpdateFullyConstrained()
  246. {
  247. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  248. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  249. $this->repo->addPackage($this->getPackage('A', '2.0'));
  250. $this->reposComplete();
  251. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  252. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  253. $this->checkSolverResult(array(array(
  254. 'job' => 'update',
  255. 'from' => $packageA,
  256. 'to' => $newPackageA,
  257. )));
  258. }
  259. public function testSolverUpdateFullyConstrainedPrunesInstalledPackages()
  260. {
  261. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  262. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  263. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  264. $this->repo->addPackage($this->getPackage('A', '2.0'));
  265. $this->reposComplete();
  266. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  267. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  268. $this->checkSolverResult(array(
  269. array(
  270. 'job' => 'update',
  271. 'from' => $packageA,
  272. 'to' => $newPackageA,
  273. ),
  274. array(
  275. 'job' => 'remove',
  276. 'package' => $packageB,
  277. ),
  278. ));
  279. }
  280. public function testSolverAllJobs()
  281. {
  282. $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
  283. $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  284. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  285. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  286. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  287. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  288. $this->repo->addPackage($this->getPackage('D', '1.0'));
  289. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  290. $this->reposComplete();
  291. $this->request->install('A');
  292. $this->request->install('C');
  293. $this->request->update('C');
  294. $this->request->remove('D');
  295. $this->checkSolverResult(array(
  296. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  297. array('job' => 'install', 'package' => $packageB),
  298. array('job' => 'install', 'package' => $packageA),
  299. array('job' => 'remove', 'package' => $packageD),
  300. ));
  301. }
  302. public function testSolverThreeAlternativeRequireAndConflict()
  303. {
  304. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  305. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  306. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  307. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  308. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  309. $packageA->setConflicts(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.0'), 'conflicts')));
  310. $this->reposComplete();
  311. $this->request->install('A');
  312. $this->checkSolverResult(array(
  313. array('job' => 'install', 'package' => $middlePackageB),
  314. array('job' => 'install', 'package' => $packageA),
  315. ));
  316. }
  317. public function testSolverObsolete()
  318. {
  319. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  320. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  321. $packageB->setReplaces(array(new Link('B', 'A', null)));
  322. $this->reposComplete();
  323. $this->request->install('B');
  324. $this->checkSolverResult(array(
  325. array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
  326. ));
  327. }
  328. public function testInstallOneOfTwoAlternatives()
  329. {
  330. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  331. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  332. $this->reposComplete();
  333. $this->request->install('A');
  334. $this->checkSolverResult(array(
  335. array('job' => 'install', 'package' => $packageA),
  336. ));
  337. }
  338. public function testInstallProvider()
  339. {
  340. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  341. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  342. $this->repo->addPackage($packageB = $this->getPackage('B', '0.8'));
  343. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  344. $packageQ->setProvides(array(new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
  345. $this->reposComplete();
  346. $this->request->install('A');
  347. $this->checkSolverResult(array(
  348. array('job' => 'install', 'package' => $packageQ),
  349. array('job' => 'install', 'package' => $packageA),
  350. ));
  351. }
  352. public function testSkipReplacerOfExistingPackage()
  353. {
  354. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  355. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  356. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  357. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  358. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  359. $this->reposComplete();
  360. $this->request->install('A');
  361. $this->checkSolverResult(array(
  362. array('job' => 'install', 'package' => $packageB),
  363. array('job' => 'install', 'package' => $packageA),
  364. ));
  365. }
  366. public function testInstallReplacerOfMissingPackage()
  367. {
  368. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  369. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  370. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  371. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  372. $this->reposComplete();
  373. $this->request->install('A');
  374. $this->checkSolverResult(array(
  375. array('job' => 'install', 'package' => $packageQ),
  376. array('job' => 'install', 'package' => $packageA),
  377. ));
  378. }
  379. public function testSkipReplacedPackageIfReplacerIsSelected()
  380. {
  381. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  382. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  383. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  384. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  385. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  386. $this->reposComplete();
  387. $this->request->install('A');
  388. $this->request->install('Q');
  389. $this->checkSolverResult(array(
  390. array('job' => 'install', 'package' => $packageQ),
  391. array('job' => 'install', 'package' => $packageA),
  392. ));
  393. }
  394. public function testPickOlderIfNewerConflicts()
  395. {
  396. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  397. $packageX->setRequires(array(
  398. new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  399. new Link('X', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  400. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  401. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  402. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  403. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  404. // new package A depends on version of package B that does not exist
  405. // => new package A is not installable
  406. $newPackageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
  407. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  408. // but an alternative option for A and B both exists
  409. // this creates a more difficult so solve conflict
  410. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  411. $packageS->setReplaces(array(new Link('S', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'), new Link('S', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces')));
  412. $this->reposComplete();
  413. $this->request->install('X');
  414. $this->checkSolverResult(array(
  415. array('job' => 'install', 'package' => $newPackageB),
  416. array('job' => 'install', 'package' => $packageA),
  417. array('job' => 'install', 'package' => $packageX),
  418. ));
  419. }
  420. public function testInstallCircularRequire()
  421. {
  422. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  423. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  424. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  425. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  426. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  427. $this->reposComplete();
  428. $this->request->install('A');
  429. $this->checkSolverResult(array(
  430. array('job' => 'install', 'package' => $packageB2),
  431. array('job' => 'install', 'package' => $packageA),
  432. ));
  433. }
  434. public function testInstallAlternativeWithCircularRequire()
  435. {
  436. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  437. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  438. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  439. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  440. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  441. $packageB->setRequires(array(new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  442. $packageC->setProvides(array(new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  443. $packageD->setProvides(array(new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  444. $packageC->setRequires(array(new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  445. $packageD->setRequires(array(new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  446. $this->reposComplete();
  447. $this->request->install('A');
  448. $this->checkSolverResult(array(
  449. array('job' => 'install', 'package' => $packageB),
  450. array('job' => 'install', 'package' => $packageA),
  451. array('job' => 'install', 'package' => $packageC),
  452. ));
  453. }
  454. /**
  455. * If a replacer D replaces B and C with C not otherwise available,
  456. * D must be installed instead of the original B.
  457. */
  458. public function testUseReplacerIfNecessary()
  459. {
  460. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  461. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  462. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  463. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  464. $packageA->setRequires(array(
  465. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  466. new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  467. ));
  468. $packageD->setReplaces(array(
  469. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  470. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  471. ));
  472. $packageD2->setReplaces(array(
  473. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  474. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  475. ));
  476. $this->reposComplete();
  477. $this->request->install('A');
  478. $this->checkSolverResult(array(
  479. array('job' => 'install', 'package' => $packageD2),
  480. array('job' => 'install', 'package' => $packageA),
  481. ));
  482. }
  483. public function testIssue265()
  484. {
  485. $this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
  486. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
  487. $this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
  488. $this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
  489. $this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
  490. $this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
  491. $this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
  492. $packageC->setRequires(array(
  493. new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  494. new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  495. ));
  496. $packageD->setRequires(array(
  497. new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
  498. new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
  499. ));
  500. $packageB1->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  501. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  502. $packageB2->setReplaces(array(new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
  503. $this->reposComplete();
  504. $this->request->install('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
  505. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  506. $this->solver->solve($this->request);
  507. }
  508. public function testConflictResultEmpty()
  509. {
  510. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  511. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));;
  512. $packageA->setConflicts(array(
  513. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'conflicts'),
  514. ));
  515. $this->reposComplete();
  516. $this->request->install('A');
  517. $this->request->install('B');
  518. try {
  519. $transaction = $this->solver->solve($this->request);
  520. $this->fail('Unsolvable conflict did not result in exception.');
  521. } catch (SolverProblemsException $e) {
  522. $problems = $e->getProblems();
  523. $this->assertEquals(1, count($problems));
  524. $msg = "\n";
  525. $msg .= " Problem 1\n";
  526. $msg .= " - Installation request for a -> satisfiable by A 1.0.\n";
  527. $msg .= " - B 1.0 conflicts with A 1.0.\n";
  528. $msg .= " - Installation request for b -> satisfiable by B 1.0.\n";
  529. $this->assertEquals($msg, $e->getMessage());
  530. }
  531. }
  532. public function testUnsatisfiableRequires()
  533. {
  534. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  535. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  536. $packageA->setRequires(array(
  537. new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  538. ));
  539. $this->reposComplete();
  540. $this->request->install('A');
  541. try {
  542. $transaction = $this->solver->solve($this->request);
  543. $this->fail('Unsolvable conflict did not result in exception.');
  544. } catch (SolverProblemsException $e) {
  545. $problems = $e->getProblems();
  546. $this->assertEquals(1, count($problems));
  547. // TODO assert problem properties
  548. $msg = "\n";
  549. $msg .= " Problem 1\n";
  550. $msg .= " - Installation request for a -> satisfiable by A 1.0.\n";
  551. $msg .= " - A 1.0 requires b >= 2.0 -> no matching package found.\n";
  552. $this->assertEquals($msg, $e->getMessage());
  553. }
  554. }
  555. public function testRequireMismatchException()
  556. {
  557. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  558. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  559. $this->repo->addPackage($packageB2 = $this->getPackage('B', '0.9'));
  560. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  561. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  562. $packageA->setRequires(array(
  563. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  564. ));
  565. $packageB->setRequires(array(
  566. new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  567. ));
  568. $packageC->setRequires(array(
  569. new Link('C', 'D', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  570. ));
  571. $packageD->setRequires(array(
  572. new Link('D', 'B', $this->getVersionConstraint('<', '1.0'), 'requires'),
  573. ));
  574. $this->reposComplete();
  575. $this->request->install('A');
  576. try {
  577. $transaction = $this->solver->solve($this->request);
  578. $this->fail('Unsolvable conflict did not result in exception.');
  579. } catch (SolverProblemsException $e) {
  580. $problems = $e->getProblems();
  581. $this->assertEquals(1, count($problems));
  582. $msg = "\n";
  583. $msg .= " Problem 1\n";
  584. $msg .= " - C 1.0 requires d >= 1.0 -> satisfiable by D 1.0.\n";
  585. $msg .= " - D 1.0 requires b < 1.0 -> satisfiable by B 0.9.\n";
  586. $msg .= " - B 1.0 requires c >= 1.0 -> satisfiable by C 1.0.\n";
  587. $msg .= " - Can only install one of: B 0.9, B 1.0.\n";
  588. $msg .= " - A 1.0 requires b >= 1.0 -> satisfiable by B 1.0.\n";
  589. $msg .= " - Installation request for a -> satisfiable by A 1.0.\n";
  590. $this->assertEquals($msg, $e->getMessage());
  591. }
  592. }
  593. public function testLearnLiteralsWithSortedRuleLiterals()
  594. {
  595. $this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
  596. $this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
  597. $this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
  598. $this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
  599. $this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
  600. $packageTwigBridge->setRequires(array(
  601. new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
  602. ));
  603. $packageSymfony->setReplaces(array(
  604. new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
  605. ));
  606. $this->reposComplete();
  607. $this->request->install('symfony/twig-bridge');
  608. $this->request->install('twig/twig');
  609. $this->checkSolverResult(array(
  610. array('job' => 'install', 'package' => $packageTwig16),
  611. array('job' => 'install', 'package' => $packageTwigBridge),
  612. ));
  613. }
  614. public function testInstallRecursiveAliasDependencies()
  615. {
  616. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  617. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  618. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  619. $packageA2->setRequires(array(
  620. new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
  621. ));
  622. $packageB->setRequires(array(
  623. new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  624. ));
  625. $this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
  626. $this->reposComplete();
  627. $this->request->install('A', $this->getVersionConstraint('==', '1.1.0.0'));
  628. $this->checkSolverResult(array(
  629. array('job' => 'install', 'package' => $packageA2),
  630. array('job' => 'install', 'package' => $packageB),
  631. array('job' => 'install', 'package' => $packageA2Alias),
  632. ));
  633. }
  634. public function testInstallDevAlias()
  635. {
  636. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  637. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  638. $packageB->setRequires(array(
  639. new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
  640. ));
  641. $this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
  642. $this->reposComplete();
  643. $this->request->install('A', $this->getVersionConstraint('==', '2.0'));
  644. $this->request->install('B');
  645. $this->checkSolverResult(array(
  646. array('job' => 'install', 'package' => $packageA),
  647. array('job' => 'install', 'package' => $packageAAlias),
  648. array('job' => 'install', 'package' => $packageB),
  649. ));
  650. }
  651. protected function reposComplete()
  652. {
  653. $this->pool->addRepository($this->repoInstalled);
  654. $this->pool->addRepository($this->repo);
  655. }
  656. protected function checkSolverResult(array $expected)
  657. {
  658. $transaction = $this->solver->solve($this->request);
  659. $result = array();
  660. foreach ($transaction as $operation) {
  661. if ('update' === $operation->getJobType()) {
  662. $result[] = array(
  663. 'job' => 'update',
  664. 'from' => $operation->getInitialPackage(),
  665. 'to' => $operation->getTargetPackage()
  666. );
  667. } else {
  668. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  669. $result[] = array(
  670. 'job' => $job,
  671. 'package' => $operation->getPackage()
  672. );
  673. }
  674. }
  675. $this->assertEquals($expected, $result);
  676. }
  677. }