SolverTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  343. $packageQ->setProvides(array(new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
  344. $this->reposComplete();
  345. $this->request->install('A');
  346. $this->checkSolverResult(array(
  347. array('job' => 'install', 'package' => $packageQ),
  348. array('job' => 'install', 'package' => $packageA),
  349. ));
  350. }
  351. public function testSkipReplacerOfExistingPackage()
  352. {
  353. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  354. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  355. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  356. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  357. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  358. $this->reposComplete();
  359. $this->request->install('A');
  360. $this->checkSolverResult(array(
  361. array('job' => 'install', 'package' => $packageB),
  362. array('job' => 'install', 'package' => $packageA),
  363. ));
  364. }
  365. public function testInstallReplacerOfMissingPackage()
  366. {
  367. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  368. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  369. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  370. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  371. $this->reposComplete();
  372. $this->request->install('A');
  373. $this->checkSolverResult(array(
  374. array('job' => 'install', 'package' => $packageQ),
  375. array('job' => 'install', 'package' => $packageA),
  376. ));
  377. }
  378. public function testSkipReplacedPackageIfReplacerIsSelected()
  379. {
  380. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  381. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  382. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  383. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  384. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  385. $this->reposComplete();
  386. $this->request->install('A');
  387. $this->request->install('Q');
  388. $this->checkSolverResult(array(
  389. array('job' => 'install', 'package' => $packageQ),
  390. array('job' => 'install', 'package' => $packageA),
  391. ));
  392. }
  393. public function testPickOlderIfNewerConflicts()
  394. {
  395. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  396. $packageX->setRequires(array(
  397. new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  398. new Link('X', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  399. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  400. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  401. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  402. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  403. // new package A depends on version of package B that does not exist
  404. // => new package A is not installable
  405. $newPackageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
  406. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  407. // but an alternative option for A and B both exists
  408. // this creates a more difficult so solve conflict
  409. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  410. $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')));
  411. $this->reposComplete();
  412. $this->request->install('X');
  413. $this->checkSolverResult(array(
  414. array('job' => 'install', 'package' => $newPackageB),
  415. array('job' => 'install', 'package' => $packageA),
  416. array('job' => 'install', 'package' => $packageX),
  417. ));
  418. }
  419. public function testInstallCircularRequire()
  420. {
  421. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  422. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  423. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  424. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  425. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  426. $this->reposComplete();
  427. $this->request->install('A');
  428. $this->checkSolverResult(array(
  429. array('job' => 'install', 'package' => $packageB2),
  430. array('job' => 'install', 'package' => $packageA),
  431. ));
  432. }
  433. public function testInstallAlternativeWithCircularRequire()
  434. {
  435. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  436. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  437. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  438. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  439. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  440. $packageB->setRequires(array(new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  441. $packageC->setProvides(array(new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  442. $packageD->setProvides(array(new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  443. $packageC->setRequires(array(new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  444. $packageD->setRequires(array(new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  445. $this->reposComplete();
  446. $this->request->install('A');
  447. $this->checkSolverResult(array(
  448. array('job' => 'install', 'package' => $packageB),
  449. array('job' => 'install', 'package' => $packageA),
  450. array('job' => 'install', 'package' => $packageC),
  451. ));
  452. }
  453. /**
  454. * If a replacer D replaces B and C with C not otherwise available,
  455. * D must be installed instead of the original B.
  456. */
  457. public function testUseReplacerIfNecessary()
  458. {
  459. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  460. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  461. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  462. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  463. $packageA->setRequires(array(
  464. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  465. new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  466. ));
  467. $packageD->setReplaces(array(
  468. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  469. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  470. ));
  471. $packageD2->setReplaces(array(
  472. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  473. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  474. ));
  475. $this->reposComplete();
  476. $this->request->install('A');
  477. $this->checkSolverResult(array(
  478. array('job' => 'install', 'package' => $packageD2),
  479. array('job' => 'install', 'package' => $packageA),
  480. ));
  481. }
  482. public function testIssue265()
  483. {
  484. $this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
  485. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
  486. $this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
  487. $this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
  488. $this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
  489. $this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
  490. $this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
  491. $packageC->setRequires(array(
  492. new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  493. new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  494. ));
  495. $packageD->setRequires(array(
  496. new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
  497. new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
  498. ));
  499. $packageB1->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  500. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  501. $packageB2->setReplaces(array(new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
  502. $this->reposComplete();
  503. $this->request->install('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
  504. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  505. $this->solver->solve($this->request);
  506. }
  507. public function testConflictResultEmpty()
  508. {
  509. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  510. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));;
  511. $packageA->setConflicts(array(
  512. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'conflicts'),
  513. ));
  514. $this->reposComplete();
  515. $this->request->install('A');
  516. $this->request->install('B');
  517. try {
  518. $transaction = $this->solver->solve($this->request);
  519. $this->fail('Unsolvable conflict did not result in exception.');
  520. } catch (SolverProblemsException $e) {
  521. $problems = $e->getProblems();
  522. $this->assertEquals(1, count($problems));
  523. $msg = "\n";
  524. $msg .= " Problem 1\n";
  525. $msg .= " - Installation request for a -> satisfiable by A 1.0.\n";
  526. $msg .= " - B 1.0 conflicts with A 1.0.\n";
  527. $msg .= " - Installation request for b -> satisfiable by B 1.0.\n";
  528. $this->assertEquals($msg, $e->getMessage());
  529. }
  530. }
  531. public function testUnsatisfiableRequires()
  532. {
  533. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  534. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  535. $packageA->setRequires(array(
  536. new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  537. ));
  538. $this->reposComplete();
  539. $this->request->install('A');
  540. try {
  541. $transaction = $this->solver->solve($this->request);
  542. $this->fail('Unsolvable conflict did not result in exception.');
  543. } catch (SolverProblemsException $e) {
  544. $problems = $e->getProblems();
  545. $this->assertEquals(1, count($problems));
  546. // TODO assert problem properties
  547. $msg = "\n";
  548. $msg .= " Problem 1\n";
  549. $msg .= " - Installation request for a -> satisfiable by A 1.0.\n";
  550. $msg .= " - A 1.0 requires b >= 2.0 -> no matching package found.\n\n";
  551. $msg .= "Potential causes:\n";
  552. $msg .= " - A typo in the package name\n";
  553. $msg .= " - The package is not available in a stable-enough version according to your minimum-stability setting\n";
  554. $msg .= " see https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion for more details.\n";
  555. $this->assertEquals($msg, $e->getMessage());
  556. }
  557. }
  558. public function testRequireMismatchException()
  559. {
  560. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  561. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  562. $this->repo->addPackage($packageB2 = $this->getPackage('B', '0.9'));
  563. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  564. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  565. $packageA->setRequires(array(
  566. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  567. ));
  568. $packageB->setRequires(array(
  569. new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  570. ));
  571. $packageC->setRequires(array(
  572. new Link('C', 'D', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  573. ));
  574. $packageD->setRequires(array(
  575. new Link('D', 'B', $this->getVersionConstraint('<', '1.0'), 'requires'),
  576. ));
  577. $this->reposComplete();
  578. $this->request->install('A');
  579. try {
  580. $transaction = $this->solver->solve($this->request);
  581. $this->fail('Unsolvable conflict did not result in exception.');
  582. } catch (SolverProblemsException $e) {
  583. $problems = $e->getProblems();
  584. $this->assertEquals(1, count($problems));
  585. $msg = "\n";
  586. $msg .= " Problem 1\n";
  587. $msg .= " - C 1.0 requires d >= 1.0 -> satisfiable by D 1.0.\n";
  588. $msg .= " - D 1.0 requires b < 1.0 -> satisfiable by B 0.9.\n";
  589. $msg .= " - B 1.0 requires c >= 1.0 -> satisfiable by C 1.0.\n";
  590. $msg .= " - Can only install one of: B 0.9, B 1.0.\n";
  591. $msg .= " - A 1.0 requires b >= 1.0 -> satisfiable by B 1.0.\n";
  592. $msg .= " - Installation request for a -> satisfiable by A 1.0.\n";
  593. $this->assertEquals($msg, $e->getMessage());
  594. }
  595. }
  596. public function testLearnLiteralsWithSortedRuleLiterals()
  597. {
  598. $this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
  599. $this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
  600. $this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
  601. $this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
  602. $this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
  603. $packageTwigBridge->setRequires(array(
  604. new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
  605. ));
  606. $packageSymfony->setReplaces(array(
  607. new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
  608. ));
  609. $this->reposComplete();
  610. $this->request->install('symfony/twig-bridge');
  611. $this->request->install('twig/twig');
  612. $this->checkSolverResult(array(
  613. array('job' => 'install', 'package' => $packageTwig16),
  614. array('job' => 'install', 'package' => $packageTwigBridge),
  615. ));
  616. }
  617. public function testInstallRecursiveAliasDependencies()
  618. {
  619. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  620. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  621. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  622. $packageA2->setRequires(array(
  623. new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
  624. ));
  625. $packageB->setRequires(array(
  626. new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  627. ));
  628. $this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
  629. $this->reposComplete();
  630. $this->request->install('A', $this->getVersionConstraint('==', '1.1.0.0'));
  631. $this->checkSolverResult(array(
  632. array('job' => 'install', 'package' => $packageA2),
  633. array('job' => 'install', 'package' => $packageB),
  634. array('job' => 'install', 'package' => $packageA2Alias),
  635. ));
  636. }
  637. public function testInstallDevAlias()
  638. {
  639. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  640. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  641. $packageB->setRequires(array(
  642. new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
  643. ));
  644. $this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
  645. $this->reposComplete();
  646. $this->request->install('A', $this->getVersionConstraint('==', '2.0'));
  647. $this->request->install('B');
  648. $this->checkSolverResult(array(
  649. array('job' => 'install', 'package' => $packageA),
  650. array('job' => 'install', 'package' => $packageAAlias),
  651. array('job' => 'install', 'package' => $packageB),
  652. ));
  653. }
  654. protected function reposComplete()
  655. {
  656. $this->pool->addRepository($this->repoInstalled);
  657. $this->pool->addRepository($this->repo);
  658. }
  659. protected function checkSolverResult(array $expected)
  660. {
  661. $transaction = $this->solver->solve($this->request);
  662. $result = array();
  663. foreach ($transaction as $operation) {
  664. if ('update' === $operation->getJobType()) {
  665. $result[] = array(
  666. 'job' => 'update',
  667. 'from' => $operation->getInitialPackage(),
  668. 'to' => $operation->getTargetPackage()
  669. );
  670. } else {
  671. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  672. $result[] = array(
  673. 'job' => $job,
  674. 'package' => $operation->getPackage()
  675. );
  676. }
  677. }
  678. $this->assertEquals($expected, $result);
  679. }
  680. }