SolverTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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('The requested package b == 1.0.0.0 could not be found.', (string) $problems[0]);
  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 testSolverInstallWithDepsInOrder()
  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($packageC = $this->getPackage('C', '1.0'));
  99. $packageB->setRequires(array(
  100. new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  101. new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  102. ));
  103. $packageC->setRequires(array(
  104. new Link('C', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  105. ));
  106. $this->reposComplete();
  107. $this->request->install('A');
  108. $this->request->install('B');
  109. $this->request->install('C');
  110. $this->checkSolverResult(array(
  111. array('job' => 'install', 'package' => $packageA),
  112. array('job' => 'install', 'package' => $packageC),
  113. array('job' => 'install', 'package' => $packageB),
  114. ));
  115. }
  116. public function testSolverInstallInstalled()
  117. {
  118. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  119. $this->reposComplete();
  120. $this->request->install('A');
  121. $this->checkSolverResult(array());
  122. }
  123. public function testSolverInstallInstalledWithAlternative()
  124. {
  125. $this->repo->addPackage($this->getPackage('A', '1.0'));
  126. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  127. $this->reposComplete();
  128. $this->request->install('A');
  129. $this->checkSolverResult(array());
  130. }
  131. public function testSolverRemoveSingle()
  132. {
  133. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  134. $this->reposComplete();
  135. $this->request->remove('A');
  136. $this->checkSolverResult(array(
  137. array('job' => 'remove', 'package' => $packageA),
  138. ));
  139. }
  140. public function testSolverRemoveUninstalled()
  141. {
  142. $this->repo->addPackage($this->getPackage('A', '1.0'));
  143. $this->reposComplete();
  144. $this->request->remove('A');
  145. $this->checkSolverResult(array());
  146. }
  147. public function testSolverUpdateDoesOnlyUpdate()
  148. {
  149. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  150. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  151. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  152. $this->reposComplete();
  153. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0.0.0'), 'requires')));
  154. $this->request->install('A', $this->getVersionConstraint('=', '1.0.0.0'));
  155. $this->request->install('B', $this->getVersionConstraint('=', '1.1.0.0'));
  156. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  157. $this->request->update('B', $this->getVersionConstraint('=', '1.0.0.0'));
  158. $this->checkSolverResult(array(
  159. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  160. ));
  161. }
  162. public function testSolverUpdateSingle()
  163. {
  164. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  165. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  166. $this->reposComplete();
  167. $this->request->install('A');
  168. $this->request->update('A');
  169. $this->checkSolverResult(array(
  170. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  171. ));
  172. }
  173. public function testSolverUpdateAll()
  174. {
  175. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  176. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  177. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  178. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  179. $packageA->setRequires(array(new Link('A', 'B', null, 'requires')));
  180. $newPackageA->setRequires(array(new Link('A', 'B', null, 'requires')));
  181. $this->reposComplete();
  182. $this->request->install('A');
  183. $this->request->updateAll();
  184. $this->checkSolverResult(array(
  185. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  186. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  187. ));
  188. }
  189. public function testSolverUpdateCurrent()
  190. {
  191. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  192. $this->repo->addPackage($this->getPackage('A', '1.0'));
  193. $this->reposComplete();
  194. $this->request->install('A');
  195. $this->request->update('A');
  196. $this->checkSolverResult(array());
  197. }
  198. public function testSolverUpdateOnlyUpdatesSelectedPackage()
  199. {
  200. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  201. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  202. $this->repo->addPackage($packageAnewer = $this->getPackage('A', '1.1'));
  203. $this->repo->addPackage($packageBnewer = $this->getPackage('B', '1.1'));
  204. $this->reposComplete();
  205. $this->request->install('A');
  206. $this->request->install('B');
  207. $this->request->update('A');
  208. $this->checkSolverResult(array(
  209. array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
  210. ));
  211. }
  212. public function testSolverUpdateConstrained()
  213. {
  214. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  215. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  216. $this->repo->addPackage($this->getPackage('A', '2.0'));
  217. $this->reposComplete();
  218. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  219. $this->request->update('A');
  220. $this->checkSolverResult(array(array(
  221. 'job' => 'update',
  222. 'from' => $packageA,
  223. 'to' => $newPackageA,
  224. )));
  225. }
  226. public function testSolverUpdateFullyConstrained()
  227. {
  228. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  229. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  230. $this->repo->addPackage($this->getPackage('A', '2.0'));
  231. $this->reposComplete();
  232. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  233. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  234. $this->checkSolverResult(array(array(
  235. 'job' => 'update',
  236. 'from' => $packageA,
  237. 'to' => $newPackageA,
  238. )));
  239. }
  240. public function testSolverUpdateFullyConstrainedPrunesInstalledPackages()
  241. {
  242. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  243. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  244. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  245. $this->repo->addPackage($this->getPackage('A', '2.0'));
  246. $this->reposComplete();
  247. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  248. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  249. $this->checkSolverResult(array(
  250. array(
  251. 'job' => 'update',
  252. 'from' => $packageA,
  253. 'to' => $newPackageA,
  254. ),
  255. array(
  256. 'job' => 'remove',
  257. 'package' => $packageB,
  258. ),
  259. ));
  260. }
  261. public function testSolverAllJobs()
  262. {
  263. $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
  264. $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  265. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  266. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  267. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  268. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  269. $this->repo->addPackage($this->getPackage('D', '1.0'));
  270. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  271. $this->reposComplete();
  272. $this->request->install('A');
  273. $this->request->install('C');
  274. $this->request->update('C');
  275. $this->request->remove('D');
  276. $this->checkSolverResult(array(
  277. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  278. array('job' => 'install', 'package' => $packageB),
  279. array('job' => 'install', 'package' => $packageA),
  280. array('job' => 'remove', 'package' => $packageD),
  281. ));
  282. }
  283. public function testSolverThreeAlternativeRequireAndConflict()
  284. {
  285. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  286. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  287. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  288. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  289. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  290. $packageA->setConflicts(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.0'), 'conflicts')));
  291. $this->reposComplete();
  292. $this->request->install('A');
  293. $this->checkSolverResult(array(
  294. array('job' => 'install', 'package' => $middlePackageB),
  295. array('job' => 'install', 'package' => $packageA),
  296. ));
  297. }
  298. public function testSolverObsolete()
  299. {
  300. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  301. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  302. $packageB->setReplaces(array(new Link('B', 'A', null)));
  303. $this->reposComplete();
  304. $this->request->install('B');
  305. $this->checkSolverResult(array(
  306. array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
  307. ));
  308. }
  309. public function testInstallOneOfTwoAlternatives()
  310. {
  311. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  312. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  313. $this->reposComplete();
  314. $this->request->install('A');
  315. $this->checkSolverResult(array(
  316. array('job' => 'install', 'package' => $packageA),
  317. ));
  318. }
  319. public function testInstallProvider()
  320. {
  321. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  322. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  323. $this->repo->addPackage($packageB = $this->getPackage('B', '0.8'));
  324. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  325. $packageQ->setProvides(array(new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
  326. $this->reposComplete();
  327. $this->request->install('A');
  328. $this->checkSolverResult(array(
  329. array('job' => 'install', 'package' => $packageQ),
  330. array('job' => 'install', 'package' => $packageA),
  331. ));
  332. }
  333. public function testSkipReplacerOfExistingPackage()
  334. {
  335. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  336. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  337. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  338. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  339. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  340. $this->reposComplete();
  341. $this->request->install('A');
  342. $this->checkSolverResult(array(
  343. array('job' => 'install', 'package' => $packageB),
  344. array('job' => 'install', 'package' => $packageA),
  345. ));
  346. }
  347. public function testInstallReplacerOfMissingPackage()
  348. {
  349. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  350. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  351. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  352. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  353. $this->reposComplete();
  354. $this->request->install('A');
  355. $this->checkSolverResult(array(
  356. array('job' => 'install', 'package' => $packageQ),
  357. array('job' => 'install', 'package' => $packageA),
  358. ));
  359. }
  360. public function testSkipReplacedPackageIfReplacerIsSelected()
  361. {
  362. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  363. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  364. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  365. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  366. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  367. $this->reposComplete();
  368. $this->request->install('A');
  369. $this->request->install('Q');
  370. $this->checkSolverResult(array(
  371. array('job' => 'install', 'package' => $packageQ),
  372. array('job' => 'install', 'package' => $packageA),
  373. ));
  374. }
  375. public function testPickOlderIfNewerConflicts()
  376. {
  377. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  378. $packageX->setRequires(array(
  379. new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  380. new Link('X', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  381. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  382. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  383. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  384. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  385. // new package A depends on version of package B that does not exist
  386. // => new package A is not installable
  387. $newPackageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
  388. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  389. // but an alternative option for A and B both exists
  390. // this creates a more difficult so solve conflict
  391. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  392. $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')));
  393. $this->reposComplete();
  394. $this->request->install('X');
  395. $this->checkSolverResult(array(
  396. array('job' => 'install', 'package' => $newPackageB),
  397. array('job' => 'install', 'package' => $packageA),
  398. array('job' => 'install', 'package' => $packageX),
  399. ));
  400. }
  401. public function testInstallCircularRequire()
  402. {
  403. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  404. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  405. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  406. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  407. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  408. $this->reposComplete();
  409. $this->request->install('A');
  410. $this->checkSolverResult(array(
  411. array('job' => 'install', 'package' => $packageB2),
  412. array('job' => 'install', 'package' => $packageA),
  413. ));
  414. }
  415. public function testInstallAlternativeWithCircularRequire()
  416. {
  417. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  418. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  419. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  420. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  421. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  422. $packageB->setRequires(array(new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  423. $packageC->setProvides(array(new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  424. $packageD->setProvides(array(new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  425. $packageC->setRequires(array(new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  426. $packageD->setRequires(array(new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  427. $this->reposComplete();
  428. $this->request->install('A');
  429. $this->checkSolverResult(array(
  430. array('job' => 'install', 'package' => $packageB),
  431. array('job' => 'install', 'package' => $packageA),
  432. array('job' => 'install', 'package' => $packageC),
  433. ));
  434. }
  435. /**
  436. * If a replacer D replaces B and C with C not otherwise available,
  437. * D must be installed instead of the original B.
  438. */
  439. public function testUseReplacerIfNecessary()
  440. {
  441. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  442. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  443. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  444. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  445. $packageA->setRequires(array(
  446. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  447. new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  448. ));
  449. $packageD->setReplaces(array(
  450. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  451. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  452. ));
  453. $packageD2->setReplaces(array(
  454. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  455. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  456. ));
  457. $this->reposComplete();
  458. $this->request->install('A');
  459. $this->checkSolverResult(array(
  460. array('job' => 'install', 'package' => $packageD2),
  461. array('job' => 'install', 'package' => $packageA),
  462. ));
  463. }
  464. public function testIssue265()
  465. {
  466. $this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
  467. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
  468. $this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
  469. $this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
  470. $this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
  471. $this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
  472. $this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
  473. $packageC->setRequires(array(
  474. new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  475. new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  476. ));
  477. $packageD->setRequires(array(
  478. new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
  479. new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
  480. ));
  481. $packageB1->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  482. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  483. $packageB2->setReplaces(array(new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
  484. $this->reposComplete();
  485. $this->request->install('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
  486. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  487. $this->solver->solve($this->request);
  488. }
  489. public function testConflictResultEmpty()
  490. {
  491. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  492. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));;
  493. $packageA->setConflicts(array(
  494. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'conflicts'),
  495. ));
  496. $this->reposComplete();
  497. $this->request->install('A');
  498. $this->request->install('B');
  499. try {
  500. $transaction = $this->solver->solve($this->request);
  501. $this->fail('Unsolvable conflict did not result in exception.');
  502. } catch (SolverProblemsException $e) {
  503. $problems = $e->getProblems();
  504. $this->assertEquals(1, count($problems));
  505. // TODO assert problem properties
  506. }
  507. }
  508. public function testUnsatisfiableRequires()
  509. {
  510. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  511. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  512. $packageA->setRequires(array(
  513. new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  514. ));
  515. $this->reposComplete();
  516. $this->request->install('A');
  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. // TODO assert problem properties
  524. }
  525. }
  526. public function testLearnLiteralsWithSortedRuleLiterals()
  527. {
  528. $this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
  529. $this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
  530. $this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
  531. $this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
  532. $this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
  533. $packageTwigBridge->setRequires(array(
  534. new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
  535. ));
  536. $packageSymfony->setReplaces(array(
  537. new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
  538. ));
  539. $this->reposComplete();
  540. $this->request->install('symfony/twig-bridge');
  541. $this->request->install('twig/twig');
  542. $this->checkSolverResult(array(
  543. array('job' => 'install', 'package' => $packageTwig16),
  544. array('job' => 'install', 'package' => $packageTwigBridge),
  545. ));
  546. }
  547. public function testInstallRecursiveAliasDependencies()
  548. {
  549. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  550. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  551. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  552. $packageA2->setRequires(array(
  553. new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
  554. ));
  555. $packageB->setRequires(array(
  556. new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  557. ));
  558. $this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
  559. $this->reposComplete();
  560. $this->request->install('A', $this->getVersionConstraint('==', '1.1.0.0'));
  561. $this->checkSolverResult(array(
  562. array('job' => 'install', 'package' => $packageA2),
  563. array('job' => 'install', 'package' => $packageB),
  564. array('job' => 'install', 'package' => $packageA2Alias),
  565. ));
  566. }
  567. public function testInstallDevAlias()
  568. {
  569. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  570. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  571. $packageB->setRequires(array(
  572. new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
  573. ));
  574. $this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
  575. $this->reposComplete();
  576. $this->request->install('A', $this->getVersionConstraint('==', '2.0'));
  577. $this->request->install('B');
  578. $this->checkSolverResult(array(
  579. array('job' => 'install', 'package' => $packageA),
  580. array('job' => 'install', 'package' => $packageAAlias),
  581. array('job' => 'install', 'package' => $packageB),
  582. ));
  583. }
  584. protected function reposComplete()
  585. {
  586. $this->pool->addRepository($this->repoInstalled);
  587. $this->pool->addRepository($this->repo);
  588. }
  589. protected function checkSolverResult(array $expected)
  590. {
  591. $transaction = $this->solver->solve($this->request);
  592. $result = array();
  593. foreach ($transaction as $operation) {
  594. if ('update' === $operation->getJobType()) {
  595. $result[] = array(
  596. 'job' => 'update',
  597. 'from' => $operation->getInitialPackage(),
  598. 'to' => $operation->getTargetPackage()
  599. );
  600. } else {
  601. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  602. $result[] = array(
  603. 'job' => $job,
  604. 'package' => $operation->getPackage()
  605. );
  606. }
  607. }
  608. $this->assertEquals($expected, $result);
  609. }
  610. }