SolverTest.php 20 KB

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