SolverTest.php 30 KB

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