SolverTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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\IO\NullIO;
  13. use Composer\Repository\ArrayRepository;
  14. use Composer\DependencyResolver\DefaultPolicy;
  15. use Composer\DependencyResolver\Pool;
  16. use Composer\DependencyResolver\Request;
  17. use Composer\DependencyResolver\Solver;
  18. use Composer\DependencyResolver\SolverProblemsException;
  19. use Composer\Package\Link;
  20. use Composer\Repository\InstalledArrayRepository;
  21. use Composer\Repository\RepositorySet;
  22. use Composer\TestCase;
  23. use Composer\Semver\Constraint\MultiConstraint;
  24. class SolverTest extends TestCase
  25. {
  26. protected $repoSet;
  27. protected $repo;
  28. protected $repoInstalled;
  29. protected $request;
  30. protected $policy;
  31. protected $solver;
  32. public function setUp()
  33. {
  34. $this->repoSet = new RepositorySet(array());
  35. $this->repo = new ArrayRepository;
  36. $this->repoInstalled = new InstalledArrayRepository;
  37. $this->request = new Request($this->repoSet);
  38. $this->policy = new DefaultPolicy;
  39. }
  40. public function testSolverInstallSingle()
  41. {
  42. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  43. $this->reposComplete();
  44. $this->request->install('A');
  45. $this->checkSolverResult(array(
  46. array('job' => 'install', 'package' => $packageA),
  47. ));
  48. }
  49. public function testSolverRemoveIfNotInstalled()
  50. {
  51. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  52. $this->reposComplete();
  53. $this->checkSolverResult(array(
  54. array('job' => 'remove', 'package' => $packageA),
  55. ));
  56. }
  57. public function testInstallNonExistingPackageFails()
  58. {
  59. $this->repo->addPackage($this->getPackage('A', '1.0'));
  60. $this->reposComplete();
  61. $this->request->install('B', $this->getVersionConstraint('==', '1'));
  62. $this->createSolver();
  63. try {
  64. $transaction = $this->solver->solve($this->request);
  65. $this->fail('Unsolvable conflict did not result in exception.');
  66. } catch (SolverProblemsException $e) {
  67. $problems = $e->getProblems();
  68. $this->assertCount(1, $problems);
  69. $this->assertEquals(2, $e->getCode());
  70. $this->assertEquals("\n - The requested package b could not be found in any version, there may be a typo in the package name.", $problems[0]->getPrettyString());
  71. }
  72. }
  73. public function testSolverInstallSamePackageFromDifferentRepositories()
  74. {
  75. $repo1 = new ArrayRepository;
  76. $repo2 = new ArrayRepository;
  77. $repo1->addPackage($foo1 = $this->getPackage('foo', '1'));
  78. $repo2->addPackage($foo2 = $this->getPackage('foo', '1'));
  79. $this->repoSet->addRepository($this->repoInstalled);
  80. $this->repoSet->addRepository($repo1);
  81. $this->repoSet->addRepository($repo2);
  82. $this->request->install('foo');
  83. $this->checkSolverResult(array(
  84. array('job' => 'install', 'package' => $foo1),
  85. ));
  86. }
  87. public function testSolverInstallWithDeps()
  88. {
  89. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  90. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  91. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  92. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  93. $this->reposComplete();
  94. $this->request->install('A');
  95. $this->checkSolverResult(array(
  96. array('job' => 'install', 'package' => $packageB),
  97. array('job' => 'install', 'package' => $packageA),
  98. ));
  99. }
  100. public function testSolverInstallHonoursNotEqualOperator()
  101. {
  102. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  103. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  104. $this->repo->addPackage($newPackageB11 = $this->getPackage('B', '1.1'));
  105. $this->repo->addPackage($newPackageB12 = $this->getPackage('B', '1.2'));
  106. $this->repo->addPackage($newPackageB13 = $this->getPackage('B', '1.3'));
  107. $packageA->setRequires(array(
  108. 'b' => new Link('A', 'B', new MultiConstraint(array(
  109. $this->getVersionConstraint('<=', '1.3'),
  110. $this->getVersionConstraint('<>', '1.3'),
  111. $this->getVersionConstraint('!=', '1.2'),
  112. )), 'requires'),
  113. ));
  114. $this->reposComplete();
  115. $this->request->install('A');
  116. $this->checkSolverResult(array(
  117. array('job' => 'install', 'package' => $newPackageB11),
  118. array('job' => 'install', 'package' => $packageA),
  119. ));
  120. }
  121. public function testSolverInstallWithDepsInOrder()
  122. {
  123. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  124. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  125. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  126. $packageB->setRequires(array(
  127. 'a' => new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  128. 'c' => new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  129. ));
  130. $packageC->setRequires(array(
  131. 'a' => new Link('C', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  132. ));
  133. $this->reposComplete();
  134. $this->request->install('A');
  135. $this->request->install('B');
  136. $this->request->install('C');
  137. $this->checkSolverResult(array(
  138. array('job' => 'install', 'package' => $packageA),
  139. array('job' => 'install', 'package' => $packageC),
  140. array('job' => 'install', 'package' => $packageB),
  141. ));
  142. }
  143. public function testSolverInstallInstalled()
  144. {
  145. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  146. $this->reposComplete();
  147. $this->request->install('A');
  148. $this->checkSolverResult(array());
  149. }
  150. public function testSolverInstallInstalledWithAlternative()
  151. {
  152. $this->repo->addPackage($this->getPackage('A', '1.0'));
  153. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  154. $this->reposComplete();
  155. $this->request->install('A');
  156. $this->checkSolverResult(array());
  157. }
  158. public function testSolverRemoveSingle()
  159. {
  160. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  161. $this->reposComplete();
  162. $this->request->remove('A');
  163. $this->checkSolverResult(array(
  164. array('job' => 'remove', 'package' => $packageA),
  165. ));
  166. }
  167. public function testSolverRemoveUninstalled()
  168. {
  169. $this->repo->addPackage($this->getPackage('A', '1.0'));
  170. $this->reposComplete();
  171. $this->request->remove('A');
  172. $this->checkSolverResult(array());
  173. }
  174. public function testSolverUpdateDoesOnlyUpdate()
  175. {
  176. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  177. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  178. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  179. $this->reposComplete();
  180. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0.0.0'), 'requires')));
  181. $this->request->install('A', $this->getVersionConstraint('=', '1.0.0.0'));
  182. $this->request->install('B', $this->getVersionConstraint('=', '1.1.0.0'));
  183. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  184. $this->request->update('B', $this->getVersionConstraint('=', '1.0.0.0'));
  185. $this->checkSolverResult(array(
  186. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  187. ));
  188. }
  189. public function testSolverUpdateSingle()
  190. {
  191. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  192. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  193. $this->reposComplete();
  194. $this->request->install('A');
  195. $this->request->update('A');
  196. $this->checkSolverResult(array(
  197. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  198. ));
  199. }
  200. public function testSolverUpdateAll()
  201. {
  202. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  203. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  204. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  205. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  206. $packageA->setRequires(array('b' => new Link('A', 'B', null, 'requires')));
  207. $newPackageA->setRequires(array('b' => new Link('A', 'B', null, 'requires')));
  208. $this->reposComplete();
  209. $this->request->install('A');
  210. $this->request->updateAll();
  211. $this->checkSolverResult(array(
  212. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  213. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  214. ));
  215. }
  216. public function testSolverUpdateCurrent()
  217. {
  218. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  219. $this->repo->addPackage($this->getPackage('A', '1.0'));
  220. $this->reposComplete();
  221. $this->request->install('A');
  222. $this->request->update('A');
  223. $this->checkSolverResult(array());
  224. }
  225. public function testSolverUpdateOnlyUpdatesSelectedPackage()
  226. {
  227. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  228. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  229. $this->repo->addPackage($packageAnewer = $this->getPackage('A', '1.1'));
  230. $this->repo->addPackage($packageBnewer = $this->getPackage('B', '1.1'));
  231. $this->reposComplete();
  232. $this->request->install('A');
  233. $this->request->install('B');
  234. $this->request->update('A');
  235. $this->checkSolverResult(array(
  236. array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
  237. ));
  238. }
  239. public function testSolverUpdateConstrained()
  240. {
  241. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  242. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  243. $this->repo->addPackage($this->getPackage('A', '2.0'));
  244. $this->reposComplete();
  245. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  246. $this->request->update('A');
  247. $this->checkSolverResult(array(array(
  248. 'job' => 'update',
  249. 'from' => $packageA,
  250. 'to' => $newPackageA,
  251. )));
  252. }
  253. public function testSolverUpdateFullyConstrained()
  254. {
  255. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  256. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  257. $this->repo->addPackage($this->getPackage('A', '2.0'));
  258. $this->reposComplete();
  259. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  260. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  261. $this->checkSolverResult(array(array(
  262. 'job' => 'update',
  263. 'from' => $packageA,
  264. 'to' => $newPackageA,
  265. )));
  266. }
  267. public function testSolverUpdateFullyConstrainedPrunesInstalledPackages()
  268. {
  269. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  270. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  271. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  272. $this->repo->addPackage($this->getPackage('A', '2.0'));
  273. $this->reposComplete();
  274. $this->request->install('A', $this->getVersionConstraint('<', '2.0.0.0'));
  275. $this->request->update('A', $this->getVersionConstraint('=', '1.0.0.0'));
  276. $this->checkSolverResult(array(
  277. array(
  278. 'job' => 'update',
  279. 'from' => $packageA,
  280. 'to' => $newPackageA,
  281. ),
  282. array(
  283. 'job' => 'remove',
  284. 'package' => $packageB,
  285. ),
  286. ));
  287. }
  288. public function testSolverAllJobs()
  289. {
  290. $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
  291. $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  292. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  293. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  294. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  295. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  296. $this->repo->addPackage($this->getPackage('D', '1.0'));
  297. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  298. $this->reposComplete();
  299. $this->request->install('A');
  300. $this->request->install('C');
  301. $this->request->update('C');
  302. $this->request->remove('D');
  303. $this->checkSolverResult(array(
  304. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  305. array('job' => 'install', 'package' => $packageB),
  306. array('job' => 'install', 'package' => $packageA),
  307. array('job' => 'remove', 'package' => $packageD),
  308. ));
  309. }
  310. public function testSolverThreeAlternativeRequireAndConflict()
  311. {
  312. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  313. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  314. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  315. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  316. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.1'), 'requires')));
  317. $packageA->setConflicts(array('b' => new Link('A', 'B', $this->getVersionConstraint('<', '1.0'), 'conflicts')));
  318. $this->reposComplete();
  319. $this->request->install('A');
  320. $this->checkSolverResult(array(
  321. array('job' => 'install', 'package' => $middlePackageB),
  322. array('job' => 'install', 'package' => $packageA),
  323. ));
  324. }
  325. public function testSolverObsolete()
  326. {
  327. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  328. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  329. $packageB->setReplaces(array('a' => new Link('B', 'A', new MultiConstraint(array()))));
  330. $this->reposComplete();
  331. $this->request->install('B');
  332. $this->checkSolverResult(array(
  333. array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
  334. ));
  335. }
  336. public function testInstallOneOfTwoAlternatives()
  337. {
  338. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  339. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  340. $this->reposComplete();
  341. $this->request->install('A');
  342. $this->checkSolverResult(array(
  343. array('job' => 'install', 'package' => $packageA),
  344. ));
  345. }
  346. public function testInstallProvider()
  347. {
  348. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  349. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  350. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  351. $packageQ->setProvides(array('b' => new Link('Q', 'B', $this->getVersionConstraint('=', '1.0'), 'provides')));
  352. $this->reposComplete();
  353. $this->request->install('A');
  354. // must explicitly pick the provider, so error in this case
  355. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  356. $this->createSolver();
  357. $this->solver->solve($this->request);
  358. }
  359. public function testSkipReplacerOfExistingPackage()
  360. {
  361. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  362. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  363. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  364. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  365. $packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  366. $this->reposComplete();
  367. $this->request->install('A');
  368. $this->checkSolverResult(array(
  369. array('job' => 'install', 'package' => $packageB),
  370. array('job' => 'install', 'package' => $packageA),
  371. ));
  372. }
  373. public function testNoInstallReplacerOfMissingPackage()
  374. {
  375. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  376. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  377. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  378. $packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  379. $this->reposComplete();
  380. $this->request->install('A');
  381. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  382. $this->createSolver();
  383. $this->solver->solve($this->request);
  384. }
  385. public function testSkipReplacedPackageIfReplacerIsSelected()
  386. {
  387. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  388. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  389. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  390. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  391. $packageQ->setReplaces(array('b' => new Link('Q', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces')));
  392. $this->reposComplete();
  393. $this->request->install('A');
  394. $this->request->install('Q');
  395. $this->checkSolverResult(array(
  396. array('job' => 'install', 'package' => $packageQ),
  397. array('job' => 'install', 'package' => $packageA),
  398. ));
  399. }
  400. public function testPickOlderIfNewerConflicts()
  401. {
  402. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  403. $packageX->setRequires(array(
  404. 'a' => new Link('X', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  405. 'b' => new Link('X', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires'),
  406. ));
  407. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  408. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  409. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  410. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'requires')));
  411. // new package A depends on version of package B that does not exist
  412. // => new package A is not installable
  413. $newPackageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.2.0.0'), 'requires')));
  414. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  415. // but an alternative option for A and B both exists
  416. // this creates a more difficult so solve conflict
  417. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  418. $packageS->setReplaces(array(
  419. 'a' => new Link('S', 'A', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'),
  420. 'b' => new Link('S', 'B', $this->getVersionConstraint('>=', '2.0.0.0'), 'replaces'),
  421. ));
  422. $this->reposComplete();
  423. $this->request->install('X');
  424. $this->checkSolverResult(array(
  425. array('job' => 'install', 'package' => $newPackageB),
  426. array('job' => 'install', 'package' => $packageA),
  427. array('job' => 'install', 'package' => $packageX),
  428. ));
  429. }
  430. public function testInstallCircularRequire()
  431. {
  432. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  433. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  434. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  435. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  436. $packageB2->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  437. $this->reposComplete();
  438. $this->request->install('A');
  439. $this->checkSolverResult(array(
  440. array('job' => 'install', 'package' => $packageB2),
  441. array('job' => 'install', 'package' => $packageA),
  442. ));
  443. }
  444. public function testInstallAlternativeWithCircularRequire()
  445. {
  446. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  447. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  448. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  449. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  450. $packageA->setRequires(array('b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  451. $packageB->setRequires(array('virtual' => new Link('B', 'Virtual', $this->getVersionConstraint('>=', '1.0'), 'requires')));
  452. $packageC->setProvides(array('virtual' => new Link('C', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  453. $packageD->setProvides(array('virtual' => new Link('D', 'Virtual', $this->getVersionConstraint('==', '1.0'), 'provides')));
  454. $packageC->setRequires(array('a' => new Link('C', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  455. $packageD->setRequires(array('a' => new Link('D', 'A', $this->getVersionConstraint('==', '1.0'), 'requires')));
  456. $this->reposComplete();
  457. $this->request->install('A');
  458. $this->request->install('C');
  459. $this->checkSolverResult(array(
  460. array('job' => 'install', 'package' => $packageA),
  461. array('job' => 'install', 'package' => $packageC),
  462. array('job' => 'install', 'package' => $packageB),
  463. ));
  464. }
  465. /**
  466. * If a replacer D replaces B and C with C not otherwise available,
  467. * D must be installed instead of the original B.
  468. */
  469. public function testUseReplacerIfNecessary()
  470. {
  471. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  472. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  473. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  474. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  475. $packageA->setRequires(array(
  476. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  477. 'c' => new Link('A', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  478. ));
  479. $packageD->setReplaces(array(
  480. 'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  481. 'c' => new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  482. ));
  483. $packageD2->setReplaces(array(
  484. 'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  485. 'c' => new Link('D', 'C', $this->getVersionConstraint('>=', '1.0'), 'replaces'),
  486. ));
  487. $this->reposComplete();
  488. $this->request->install('A');
  489. $this->request->install('D');
  490. $this->checkSolverResult(array(
  491. array('job' => 'install', 'package' => $packageD2),
  492. array('job' => 'install', 'package' => $packageA),
  493. ));
  494. }
  495. public function testIssue265()
  496. {
  497. $this->repo->addPackage($packageA1 = $this->getPackage('A', '2.0.999999-dev'));
  498. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.1-dev'));
  499. $this->repo->addPackage($packageA3 = $this->getPackage('A', '2.2-dev'));
  500. $this->repo->addPackage($packageB1 = $this->getPackage('B', '2.0.10'));
  501. $this->repo->addPackage($packageB2 = $this->getPackage('B', '2.0.9'));
  502. $this->repo->addPackage($packageC = $this->getPackage('C', '2.0-dev'));
  503. $this->repo->addPackage($packageD = $this->getPackage('D', '2.0.9'));
  504. $packageC->setRequires(array(
  505. 'a' => new Link('C', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  506. 'd' => new Link('C', 'D', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  507. ));
  508. $packageD->setRequires(array(
  509. 'a' => new Link('D', 'A', $this->getVersionConstraint('>=', '2.1'), 'requires'),
  510. 'b' => new Link('D', 'B', $this->getVersionConstraint('>=', '2.0-dev'), 'requires'),
  511. ));
  512. $packageB1->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  513. $packageB2->setRequires(array('a' => new Link('B', 'A', $this->getVersionConstraint('==', '2.1.0.0-dev'), 'requires')));
  514. $packageB2->setReplaces(array('d' => new Link('B', 'D', $this->getVersionConstraint('==', '2.0.9.0'), 'replaces')));
  515. $this->reposComplete();
  516. $this->request->install('C', $this->getVersionConstraint('==', '2.0.0.0-dev'));
  517. $this->setExpectedException('Composer\DependencyResolver\SolverProblemsException');
  518. $this->createSolver();
  519. $this->solver->solve($this->request);
  520. }
  521. public function testConflictResultEmpty()
  522. {
  523. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  524. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  525. $packageA->setConflicts(array(
  526. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'conflicts'),
  527. ));
  528. $this->reposComplete();
  529. $this->request->install('A');
  530. $this->request->install('B');
  531. $this->createSolver();
  532. try {
  533. $transaction = $this->solver->solve($this->request);
  534. $this->fail('Unsolvable conflict did not result in exception.');
  535. } catch (SolverProblemsException $e) {
  536. $problems = $e->getProblems();
  537. $this->assertCount(1, $problems);
  538. $msg = "\n";
  539. $msg .= " Problem 1\n";
  540. $msg .= " - Installation request for a -> satisfiable by A[1.0].\n";
  541. $msg .= " - B 1.0 conflicts with A[1.0].\n";
  542. $msg .= " - Installation request for b -> satisfiable by B[1.0].\n";
  543. $this->assertEquals($msg, $e->getMessage());
  544. }
  545. }
  546. public function testUnsatisfiableRequires()
  547. {
  548. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  549. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  550. $packageA->setRequires(array(
  551. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  552. ));
  553. $this->reposComplete();
  554. $this->request->install('A');
  555. $this->createSolver();
  556. try {
  557. $transaction = $this->solver->solve($this->request);
  558. $this->fail('Unsolvable conflict did not result in exception.');
  559. } catch (SolverProblemsException $e) {
  560. $problems = $e->getProblems();
  561. $this->assertCount(1, $problems);
  562. // TODO assert problem properties
  563. $msg = "\n";
  564. $msg .= " Problem 1\n";
  565. $msg .= " - Installation request for a -> satisfiable by A[1.0].\n";
  566. $msg .= " - A 1.0 requires b >= 2.0 -> no matching package found.\n\n";
  567. $msg .= "Potential causes:\n";
  568. $msg .= " - A typo in the package name\n";
  569. $msg .= " - The package is not available in a stable-enough version according to your minimum-stability setting\n";
  570. $msg .= " see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.\n";
  571. $msg .= " - It's a private package and you forgot to add a custom repository to find it\n\n";
  572. $msg .= "Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.";
  573. $this->assertEquals($msg, $e->getMessage());
  574. }
  575. }
  576. public function testRequireMismatchException()
  577. {
  578. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  579. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  580. $this->repo->addPackage($packageB2 = $this->getPackage('B', '0.9'));
  581. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  582. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  583. $packageA->setRequires(array(
  584. 'b' => new Link('A', 'B', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  585. ));
  586. $packageB->setRequires(array(
  587. 'c' => new Link('B', 'C', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  588. ));
  589. $packageC->setRequires(array(
  590. 'd' => new Link('C', 'D', $this->getVersionConstraint('>=', '1.0'), 'requires'),
  591. ));
  592. $packageD->setRequires(array(
  593. 'b' => new Link('D', 'B', $this->getVersionConstraint('<', '1.0'), 'requires'),
  594. ));
  595. $this->reposComplete();
  596. $this->request->install('A');
  597. $this->createSolver();
  598. try {
  599. $transaction = $this->solver->solve($this->request);
  600. $this->fail('Unsolvable conflict did not result in exception.');
  601. } catch (SolverProblemsException $e) {
  602. $problems = $e->getProblems();
  603. $this->assertCount(1, $problems);
  604. $msg = "\n";
  605. $msg .= " Problem 1\n";
  606. $msg .= " - C 1.0 requires d >= 1.0 -> satisfiable by D[1.0].\n";
  607. $msg .= " - D 1.0 requires b < 1.0 -> satisfiable by B[0.9].\n";
  608. $msg .= " - B 1.0 requires c >= 1.0 -> satisfiable by C[1.0].\n";
  609. $msg .= " - Can only install one of: B[0.9, 1.0].\n";
  610. $msg .= " - A 1.0 requires b >= 1.0 -> satisfiable by B[1.0].\n";
  611. $msg .= " - Installation request for a -> satisfiable by A[1.0].\n";
  612. $this->assertEquals($msg, $e->getMessage());
  613. }
  614. }
  615. public function testLearnLiteralsWithSortedRuleLiterals()
  616. {
  617. $this->repo->addPackage($packageTwig2 = $this->getPackage('twig/twig', '2.0'));
  618. $this->repo->addPackage($packageTwig16 = $this->getPackage('twig/twig', '1.6'));
  619. $this->repo->addPackage($packageTwig15 = $this->getPackage('twig/twig', '1.5'));
  620. $this->repo->addPackage($packageSymfony = $this->getPackage('symfony/symfony', '2.0'));
  621. $this->repo->addPackage($packageTwigBridge = $this->getPackage('symfony/twig-bridge', '2.0'));
  622. $packageTwigBridge->setRequires(array(
  623. 'twig/twig' => new Link('symfony/twig-bridge', 'twig/twig', $this->getVersionConstraint('<', '2.0'), 'requires'),
  624. ));
  625. $packageSymfony->setReplaces(array(
  626. 'symfony/twig-bridge' => new Link('symfony/symfony', 'symfony/twig-bridge', $this->getVersionConstraint('==', '2.0'), 'replaces'),
  627. ));
  628. $this->reposComplete();
  629. $this->request->install('symfony/twig-bridge');
  630. $this->request->install('twig/twig');
  631. $this->checkSolverResult(array(
  632. array('job' => 'install', 'package' => $packageTwig16),
  633. array('job' => 'install', 'package' => $packageTwigBridge),
  634. ));
  635. }
  636. public function testInstallRecursiveAliasDependencies()
  637. {
  638. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  639. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  640. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  641. $packageA2->setRequires(array(
  642. 'b' => new Link('A', 'B', $this->getVersionConstraint('==', '2.0'), 'requires', '== 2.0'),
  643. ));
  644. $packageB->setRequires(array(
  645. 'a' => new Link('B', 'A', $this->getVersionConstraint('>=', '2.0'), 'requires'),
  646. ));
  647. $this->repo->addPackage($packageA2Alias = $this->getAliasPackage($packageA2, '1.1'));
  648. $this->reposComplete();
  649. $this->request->install('A', $this->getVersionConstraint('==', '1.1.0.0'));
  650. $this->checkSolverResult(array(
  651. array('job' => 'install', 'package' => $packageA2),
  652. array('job' => 'install', 'package' => $packageB),
  653. array('job' => 'install', 'package' => $packageA2Alias),
  654. ));
  655. }
  656. public function testInstallDevAlias()
  657. {
  658. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  659. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  660. $packageB->setRequires(array(
  661. 'a' => new Link('B', 'A', $this->getVersionConstraint('<', '2.0'), 'requires'),
  662. ));
  663. $this->repo->addPackage($packageAAlias = $this->getAliasPackage($packageA, '1.1'));
  664. $this->reposComplete();
  665. $this->request->install('A', $this->getVersionConstraint('==', '2.0'));
  666. $this->request->install('B');
  667. $this->checkSolverResult(array(
  668. array('job' => 'install', 'package' => $packageA),
  669. array('job' => 'install', 'package' => $packageAAlias),
  670. array('job' => 'install', 'package' => $packageB),
  671. ));
  672. }
  673. protected function reposComplete()
  674. {
  675. $this->repoSet->addRepository($this->repoInstalled);
  676. $this->repoSet->addRepository($this->repo);
  677. }
  678. protected function createSolver()
  679. {
  680. $this->solver = new Solver($this->policy, $this->repoSet->createPool($this->request), $this->repoInstalled, new NullIO());
  681. }
  682. protected function checkSolverResult(array $expected)
  683. {
  684. $this->createSolver();
  685. $transaction = $this->solver->solve($this->request);
  686. $result = array();
  687. foreach ($transaction as $operation) {
  688. if ('update' === $operation->getJobType()) {
  689. $result[] = array(
  690. 'job' => 'update',
  691. 'from' => $operation->getInitialPackage(),
  692. 'to' => $operation->getTargetPackage(),
  693. );
  694. } else {
  695. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  696. $result[] = array(
  697. 'job' => $job,
  698. 'package' => $operation->getPackage(),
  699. );
  700. }
  701. }
  702. $this->assertEquals($expected, $result);
  703. }
  704. }