SolverTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\DependencyResolver;
  12. use Composer\Repository\ArrayRepository;
  13. use Composer\Repository\PlatformRepository;
  14. use Composer\Repository\ComposerRepository;
  15. use Composer\DependencyResolver\DefaultPolicy;
  16. use Composer\DependencyResolver\Pool;
  17. use Composer\DependencyResolver\Request;
  18. use Composer\DependencyResolver\Solver;
  19. use Composer\Package\Link;
  20. use Composer\Package\LinkConstraint\VersionConstraint;
  21. use Composer\Test\TestCase;
  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);
  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 testSolverInstallWithDeps()
  48. {
  49. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  50. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  51. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  52. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
  53. $this->reposComplete();
  54. $this->request->install('A');
  55. $this->checkSolverResult(array(
  56. array('job' => 'install', 'package' => $packageB),
  57. array('job' => 'install', 'package' => $packageA),
  58. ));
  59. }
  60. public function testSolverInstallInstalled()
  61. {
  62. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  63. $this->reposComplete();
  64. $this->request->install('A');
  65. $this->checkSolverResult(array());
  66. }
  67. public function testSolverInstallInstalledWithAlternative()
  68. {
  69. $this->repo->addPackage($this->getPackage('A', '1.0'));
  70. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  71. $this->reposComplete();
  72. $this->request->install('A');
  73. $this->checkSolverResult(array());
  74. }
  75. public function testSolverRemoveSingle()
  76. {
  77. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  78. $this->reposComplete();
  79. $this->request->remove('A');
  80. $this->checkSolverResult(array(
  81. array('job' => 'remove', 'package' => $packageA),
  82. ));
  83. }
  84. public function testSolverRemoveUninstalled()
  85. {
  86. $this->repo->addPackage($this->getPackage('A', '1.0'));
  87. $this->reposComplete();
  88. $this->request->remove('A');
  89. $this->checkSolverResult(array());
  90. }
  91. public function testSolverUpdateDoesOnlyUpdate()
  92. {
  93. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  94. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  95. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  96. $this->reposComplete();
  97. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0.0.0'), 'requires')));
  98. $this->request->install('A', new VersionConstraint('=', '1.0.0.0'));
  99. $this->request->install('B', new VersionConstraint('=', '1.1.0.0'));
  100. $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
  101. $this->request->update('B', new VersionConstraint('=', '1.0.0.0'));
  102. $this->checkSolverResult(array(
  103. array('job' => 'update', 'from' => $packageB, 'to' => $newPackageB),
  104. ));
  105. }
  106. public function testSolverUpdateSingle()
  107. {
  108. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  109. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  110. $this->reposComplete();
  111. $this->request->update('A');
  112. $this->checkSolverResult(array(
  113. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  114. ));
  115. }
  116. public function testSolverUpdateCurrent()
  117. {
  118. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  119. $this->repo->addPackage($this->getPackage('A', '1.0'));
  120. $this->reposComplete();
  121. $this->request->update('A');
  122. $this->checkSolverResult(array());
  123. }
  124. public function testSolverUpdateOnlyUpdatesSelectedPackage()
  125. {
  126. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  127. $this->repoInstalled->addPackage($packageB = $this->getPackage('B', '1.0'));
  128. $this->repo->addPackage($packageAnewer = $this->getPackage('A', '1.1'));
  129. $this->repo->addPackage($packageBnewer = $this->getPackage('B', '1.1'));
  130. $this->reposComplete();
  131. $this->request->update('A');
  132. $this->checkSolverResult(array(
  133. array('job' => 'update', 'from' => $packageA, 'to' => $packageAnewer),
  134. ));
  135. }
  136. public function testSolverUpdateConstrained()
  137. {
  138. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  139. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  140. $this->repo->addPackage($this->getPackage('A', '2.0'));
  141. $this->reposComplete();
  142. $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
  143. $this->request->update('A');
  144. $this->checkSolverResult(array(array(
  145. 'job' => 'update',
  146. 'from' => $packageA,
  147. 'to' => $newPackageA,
  148. )));
  149. }
  150. public function testSolverUpdateFullyConstrained()
  151. {
  152. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  153. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  154. $this->repo->addPackage($this->getPackage('A', '2.0'));
  155. $this->reposComplete();
  156. $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
  157. $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
  158. $this->checkSolverResult(array(array(
  159. 'job' => 'update',
  160. 'from' => $packageA,
  161. 'to' => $newPackageA,
  162. )));
  163. }
  164. public function testSolverUpdateFullyConstrainedPrunesInstalledPackages()
  165. {
  166. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  167. $this->repoInstalled->addPackage($this->getPackage('B', '1.0'));
  168. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  169. $this->repo->addPackage($this->getPackage('A', '2.0'));
  170. $this->reposComplete();
  171. $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
  172. $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
  173. $this->checkSolverResult(array(array(
  174. 'job' => 'update',
  175. 'from' => $packageA,
  176. 'to' => $newPackageA,
  177. )));
  178. }
  179. public function testSolverAllJobs()
  180. {
  181. $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
  182. $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  183. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  184. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  185. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  186. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  187. $this->repo->addPackage($this->getPackage('D', '1.0'));
  188. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
  189. $this->reposComplete();
  190. $this->request->install('A');
  191. $this->request->update('C');
  192. $this->request->remove('D');
  193. $this->checkSolverResult(array(
  194. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  195. array('job' => 'install', 'package' => $packageB),
  196. array('job' => 'remove', 'package' => $packageD),
  197. array('job' => 'install', 'package' => $packageA),
  198. ));
  199. }
  200. public function testSolverThreeAlternativeRequireAndConflict()
  201. {
  202. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  203. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  204. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  205. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  206. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
  207. $packageA->setConflicts(array(new Link('A', 'B', new VersionConstraint('<', '1.0'), 'conflicts')));
  208. $this->reposComplete();
  209. $this->request->install('A');
  210. $this->checkSolverResult(array(
  211. array('job' => 'install', 'package' => $middlePackageB),
  212. array('job' => 'install', 'package' => $packageA),
  213. ));
  214. }
  215. public function testSolverObsolete()
  216. {
  217. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  218. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  219. $packageB->setReplaces(array(new Link('B', 'A', null)));
  220. $this->reposComplete();
  221. $this->request->install('B');
  222. $this->checkSolverResult(array(
  223. array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
  224. ));
  225. }
  226. public function testInstallOneOfTwoAlternatives()
  227. {
  228. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  229. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  230. $this->reposComplete();
  231. $this->request->install('A');
  232. $this->checkSolverResult(array(
  233. array('job' => 'install', 'package' => $packageA),
  234. ));
  235. }
  236. public function testInstallProvider()
  237. {
  238. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  239. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  240. $this->repo->addPackage($packageB = $this->getPackage('B', '0.8'));
  241. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  242. $packageQ->setProvides(array(new Link('Q', 'B', new VersionConstraint('=', '1.0'), 'provides')));
  243. $this->reposComplete();
  244. $this->request->install('A');
  245. $this->checkSolverResult(array(
  246. array('job' => 'install', 'package' => $packageQ),
  247. array('job' => 'install', 'package' => $packageA),
  248. ));
  249. }
  250. public function testSkipReplacerOfExistingPackage()
  251. {
  252. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  253. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  254. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  255. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  256. $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
  257. $this->reposComplete();
  258. $this->request->install('A');
  259. $this->checkSolverResult(array(
  260. array('job' => 'install', 'package' => $packageB),
  261. array('job' => 'install', 'package' => $packageA),
  262. ));
  263. }
  264. public function testInstallReplacerOfMissingPackage()
  265. {
  266. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  267. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  268. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  269. $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
  270. $this->reposComplete();
  271. $this->request->install('A');
  272. $this->checkSolverResult(array(
  273. array('job' => 'install', 'package' => $packageQ),
  274. array('job' => 'install', 'package' => $packageA),
  275. ));
  276. }
  277. public function testSkipReplacedPackageIfReplacerIsSelected()
  278. {
  279. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  280. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  281. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  282. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  283. $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
  284. $this->reposComplete();
  285. $this->request->install('A');
  286. $this->request->install('Q');
  287. $this->checkSolverResult(array(
  288. array('job' => 'install', 'package' => $packageQ),
  289. array('job' => 'install', 'package' => $packageA),
  290. ));
  291. }
  292. public function testPickOlderIfNewerConflicts()
  293. {
  294. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  295. $packageX->setRequires(array(
  296. new Link('X', 'A', new VersionConstraint('>=', '2.0.0.0'), 'requires'),
  297. new Link('X', 'B', new VersionConstraint('>=', '2.0.0.0'), 'requires')));
  298. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  299. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  300. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  301. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '2.0.0.0'), 'requires')));
  302. // new package A depends on version of package B that does not exist
  303. // => new package A is not installable
  304. $newPackageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '2.2.0.0'), 'requires')));
  305. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  306. // but an alternative option for A and B both exists
  307. // this creates a more difficult so solve conflict
  308. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  309. $packageS->setReplaces(array(new Link('S', 'A', new VersionConstraint('>=', '2.0.0.0'), 'replaces'), new Link('S', 'B', new VersionConstraint('>=', '2.0.0.0'), 'replaces')));
  310. $this->reposComplete();
  311. $this->request->install('X');
  312. $this->checkSolverResult(array(
  313. array('job' => 'install', 'package' => $packageA),
  314. array('job' => 'install', 'package' => $newPackageB),
  315. array('job' => 'install', 'package' => $packageX),
  316. ));
  317. }
  318. public function testInstallCircularRequire()
  319. {
  320. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  321. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  322. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  323. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  324. $packageB2->setRequires(array(new Link('B', 'A', new VersionConstraint('>=', '1.0'), 'requires')));
  325. $this->reposComplete();
  326. $this->request->install('A');
  327. $this->checkSolverResult(array(
  328. array('job' => 'install', 'package' => $packageB2),
  329. array('job' => 'install', 'package' => $packageA),
  330. ));
  331. }
  332. public function testInstallAlternativeWithCircularRequire()
  333. {
  334. $this->markTestIncomplete();
  335. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  336. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  337. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  338. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  339. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  340. $packageB->setRequires(array(new Link('B', 'Virtual', new VersionConstraint('>=', '1.0'), 'requires')));
  341. $packageC->setRequires(array(new Link('C', 'Virtual', new VersionConstraint('==', '1.0'), 'provides')));
  342. $packageD->setRequires(array(new Link('D', 'Virtual', new VersionConstraint('==', '1.0'), 'provides')));
  343. $this->reposComplete();
  344. $this->request->install('A');
  345. $this->checkSolverResult(array(
  346. array('job' => 'install', 'package' => $packageC),
  347. array('job' => 'install', 'package' => $packageB),
  348. array('job' => 'install', 'package' => $packageA),
  349. ));
  350. }
  351. /**
  352. * If a replacer D replaces B and C with C not otherwise available,
  353. * D must be installed instead of the original B.
  354. */
  355. public function testUseReplacerIfNecessary()
  356. {
  357. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  358. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  359. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  360. $this->repo->addPackage($packageD2 = $this->getPackage('D', '1.1'));
  361. $packageA->setRequires(array(
  362. new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires'),
  363. new Link('A', 'C', new VersionConstraint('>=', '1.0'), 'requires'),
  364. ));
  365. $packageD->setReplaces(array(
  366. new Link('D', 'B', new VersionConstraint('>=', '1.0'), 'replaces'),
  367. new Link('D', 'C', new VersionConstraint('>=', '1.0'), 'replaces'),
  368. ));
  369. $packageD2->setReplaces(array(
  370. new Link('D', 'B', new VersionConstraint('>=', '1.0'), 'replaces'),
  371. new Link('D', 'C', new VersionConstraint('>=', '1.0'), 'replaces'),
  372. ));
  373. $this->reposComplete();
  374. $this->request->install('A');
  375. $this->checkSolverResult(array(
  376. array('job' => 'install', 'package' => $packageD2),
  377. array('job' => 'install', 'package' => $packageA),
  378. ));
  379. }
  380. protected function reposComplete()
  381. {
  382. $this->pool->addRepository($this->repoInstalled);
  383. $this->pool->addRepository($this->repo);
  384. }
  385. protected function checkSolverResult(array $expected)
  386. {
  387. $transaction = $this->solver->solve($this->request);
  388. $result = array();
  389. foreach ($transaction as $operation) {
  390. if ('update' === $operation->getJobType()) {
  391. $result[] = array(
  392. 'job' => 'update',
  393. 'from' => $operation->getInitialPackage(),
  394. 'to' => $operation->getTargetPackage()
  395. );
  396. } else {
  397. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  398. $result[] = array(
  399. 'job' => $job,
  400. 'package' => $operation->getPackage()
  401. );
  402. }
  403. }
  404. $this->assertEquals($expected, $result);
  405. }
  406. }