SolverTest.php 38 KB

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