SolverTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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' => 'remove',
  233. 'package' => $packageB,
  234. ),
  235. array(
  236. 'job' => 'update',
  237. 'from' => $packageA,
  238. 'to' => $newPackageA,
  239. )));
  240. }
  241. public function testSolverAllJobs()
  242. {
  243. $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
  244. $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  245. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  246. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  247. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  248. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  249. $this->repo->addPackage($this->getPackage('D', '1.0'));
  250. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  251. $this->reposComplete();
  252. $this->request->install('A');
  253. $this->request->install('C');
  254. $this->request->update('C');
  255. $this->request->remove('D');
  256. $this->checkSolverResult(array(
  257. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  258. array('job' => 'install', 'package' => $packageB),
  259. array('job' => 'remove', 'package' => $packageD),
  260. array('job' => 'install', 'package' => $packageA),
  261. ));
  262. }
  263. public function testSolverThreeAlternativeRequireAndConflict()
  264. {
  265. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  266. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  267. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  268. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  269. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  270. $packageA->setConflicts(array(new Link('A', 'B', $this->getVersionConstraint('<', '1.0'), 'conflicts')));
  271. $this->reposComplete();
  272. $this->request->install('A');
  273. $this->checkSolverResult(array(
  274. array('job' => 'install', 'package' => $middlePackageB),
  275. array('job' => 'install', 'package' => $packageA),
  276. ));
  277. }
  278. public function testSolverObsolete()
  279. {
  280. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  281. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  282. $packageB->setReplaces(array(new Link('B', 'A', null)));
  283. $this->reposComplete();
  284. $this->request->install('B');
  285. $this->checkSolverResult(array(
  286. array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
  287. ));
  288. }
  289. public function testInstallOneOfTwoAlternatives()
  290. {
  291. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  292. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  293. $this->reposComplete();
  294. $this->request->install('A');
  295. $this->checkSolverResult(array(
  296. array('job' => 'install', 'package' => $packageA),
  297. ));
  298. }
  299. public function testInstallProvider()
  300. {
  301. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  302. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  303. $this->repo->addPackage($packageB = $this->getPackage('B', '0.8'));
  304. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  305. $packageQ->setProvides(array(new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
  306. $this->reposComplete();
  307. $this->request->install('A');
  308. $this->checkSolverResult(array(
  309. array('job' => 'install', 'package' => $packageQ),
  310. array('job' => 'install', 'package' => $packageA),
  311. ));
  312. }
  313. public function testSkipReplacerOfExistingPackage()
  314. {
  315. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  316. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  317. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  318. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  319. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  320. $this->reposComplete();
  321. $this->request->install('A');
  322. $this->checkSolverResult(array(
  323. array('job' => 'install', 'package' => $packageB),
  324. array('job' => 'install', 'package' => $packageA),
  325. ));
  326. }
  327. public function testInstallReplacerOfMissingPackage()
  328. {
  329. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  330. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  331. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  332. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  333. $this->reposComplete();
  334. $this->request->install('A');
  335. $this->checkSolverResult(array(
  336. array('job' => 'install', 'package' => $packageQ),
  337. array('job' => 'install', 'package' => $packageA),
  338. ));
  339. }
  340. public function testSkipReplacedPackageIfReplacerIsSelected()
  341. {
  342. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  343. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  344. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  345. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  346. $packageQ->setReplaces(array(new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  347. $this->reposComplete();
  348. $this->request->install('A');
  349. $this->request->install('Q');
  350. $this->checkSolverResult(array(
  351. array('job' => 'install', 'package' => $packageQ),
  352. array('job' => 'install', 'package' => $packageA),
  353. ));
  354. }
  355. public function testPickOlderIfNewerConflicts()
  356. {
  357. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  358. $packageX->setRequires(array(
  359. new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  360. new Link('X', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  361. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  362. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  363. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  364. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  365. // new package A depends on version of package B that does not exist
  366. // => new package A is not installable
  367. $newPackageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
  368. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  369. // but an alternative option for A and B both exists
  370. // this creates a more difficult so solve conflict
  371. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  372. $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')));
  373. $this->reposComplete();
  374. $this->request->install('X');
  375. $this->checkSolverResult(array(
  376. array('job' => 'install', 'package' => $packageA),
  377. array('job' => 'install', 'package' => $newPackageB),
  378. array('job' => 'install', 'package' => $packageX),
  379. ));
  380. }
  381. public function testInstallCircularRequire()
  382. {
  383. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  384. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  385. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  386. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  387. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  388. $this->reposComplete();
  389. $this->request->install('A');
  390. $this->checkSolverResult(array(
  391. array('job' => 'install', 'package' => $packageB2),
  392. array('job' => 'install', 'package' => $packageA),
  393. ));
  394. }
  395. public function testInstallAlternativeWithCircularRequire()
  396. {
  397. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  398. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  399. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  400. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  401. $packageA->setRequires(array(new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  402. $packageB->setRequires(array(new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  403. $packageC->setProvides(array(new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  404. $packageD->setProvides(array(new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  405. $packageC->setRequires(array(new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  406. $packageD->setRequires(array(new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  407. $this->reposComplete();
  408. $this->request->install('A');
  409. $this->checkSolverResult(array(
  410. array('job' => 'install', 'package' => $packageC),
  411. array('job' => 'install', 'package' => $packageB),
  412. array('job' => 'install', 'package' => $packageA),
  413. ));
  414. }
  415. /**
  416. * If a replacer D replaces B and C with C not otherwise available,
  417. * D must be installed instead of the original B.
  418. */
  419. public function testUseReplacerIfNecessary()
  420. {
  421. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  422. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  423. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  424. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  425. $packageA->setRequires(array(
  426. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  427. new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  428. ));
  429. $packageD->setReplaces(array(
  430. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  431. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  432. ));
  433. $packageD2->setReplaces(array(
  434. new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  435. new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  436. ));
  437. $this->reposComplete();
  438. $this->request->install('A');
  439. $this->checkSolverResult(array(
  440. array('job' => 'install', 'package' => $packageD2),
  441. array('job' => 'install', 'package' => $packageA),
  442. ));
  443. }
  444. public function testIssue265()
  445. {
  446. $this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
  447. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
  448. $this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
  449. $this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
  450. $this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
  451. $this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
  452. $this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
  453. $packageC->setRequires(array(
  454. new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  455. new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  456. ));
  457. $packageD->setRequires(array(
  458. new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
  459. new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
  460. ));
  461. $packageB1->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  462. $packageB2->setRequires(array(new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  463. $packageB2->setReplaces(array(new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
  464. $this->reposComplete();
  465. $this->request->install('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
  466. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  467. $this->solver->solve($this->request);
  468. }
  469. public function testConflictResultEmpty()
  470. {
  471. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  472. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));;
  473. $packageA->setConflicts(array(
  474. new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'conflicts'),
  475. ));
  476. $this->reposComplete();
  477. $this->request->install('A');
  478. $this->request->install('B');
  479. try {
  480. $transaction = $this->solver->solve($this->request);
  481. $this->fail('Unsolvable conflict did not result in exception.');
  482. } catch (SolverProblemsException $e) {
  483. $problems = $e->getProblems();
  484. $this->assertEquals(1, count($problems));
  485. // TODO assert problem properties
  486. }
  487. }
  488. public function testUnsatisfiableRequires()
  489. {
  490. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  491. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  492. $packageA->setRequires(array(
  493. new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  494. ));
  495. $this->reposComplete();
  496. $this->request->install('A');
  497. try {
  498. $transaction = $this->solver->solve($this->request);
  499. $this->fail('Unsolvable conflict did not result in exception.');
  500. } catch (SolverProblemsException $e) {
  501. $problems = $e->getProblems();
  502. $this->assertEquals(1, count($problems));
  503. // TODO assert problem properties
  504. }
  505. }
  506. public function testLearnLiteralsWithSortedRuleLiterals()
  507. {
  508. $this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
  509. $this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
  510. $this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
  511. $this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
  512. $this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
  513. $packageTwigBridge->setRequires(array(
  514. new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
  515. ));
  516. $packageSymfony->setReplaces(array(
  517. new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
  518. ));
  519. $this->reposComplete();
  520. $this->request->install('symfony/twig-bridge');
  521. $this->request->install('twig/twig');
  522. $this->checkSolverResult(array(
  523. array('job' => 'install', 'package' => $packageTwig16),
  524. array('job' => 'install', 'package' => $packageTwigBridge),
  525. ));
  526. }
  527. public function testInstallRecursiveAliasDependencies()
  528. {
  529. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  530. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  531. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  532. $packageA2->setRequires(array(
  533. new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
  534. ));
  535. $packageB->setRequires(array(
  536. new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  537. ));
  538. $this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
  539. $this->reposComplete();
  540. $this->request->install('A', $this->getVersionConstraint('==', '1.1.0.0'));
  541. $this->checkSolverResult(array(
  542. array('job' => 'install', 'package' => $packageB),
  543. array('job' => 'install', 'package' => $packageA2),
  544. array('job' => 'install', 'package' => $packageA2Alias),
  545. ));
  546. }
  547. public function testInstallDevAlias()
  548. {
  549. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  550. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  551. $packageB->setRequires(array(
  552. new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
  553. ));
  554. $this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
  555. $this->reposComplete();
  556. $this->request->install('A', $this->getVersionConstraint('==', '2.0'));
  557. $this->request->install('B');
  558. $this->checkSolverResult(array(
  559. array('job' => 'install', 'package' => $packageAAlias),
  560. array('job' => 'install', 'package' => $packageB),
  561. array('job' => 'install', 'package' => $packageA),
  562. ));
  563. }
  564. protected function reposComplete()
  565. {
  566. $this->pool->addRepository($this->repoInstalled);
  567. $this->pool->addRepository($this->repo);
  568. }
  569. protected function checkSolverResult(array $expected)
  570. {
  571. $transaction = $this->solver->solve($this->request);
  572. $result = array();
  573. foreach ($transaction as $operation) {
  574. if ('update' === $operation->getJobType()) {
  575. $result[] = array(
  576. 'job' => 'update',
  577. 'from' => $operation->getInitialPackage(),
  578. 'to' => $operation->getTargetPackage()
  579. );
  580. } else {
  581. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  582. $result[] = array(
  583. 'job' => $job,
  584. 'package' => $operation->getPackage()
  585. );
  586. }
  587. }
  588. $this->assertEquals($expected, $result);
  589. }
  590. }