SolverTest.php 34 KB

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