SolverTest.php 34 KB

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