SolverTest.php 21 KB

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