SolverTest.php 29 KB

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