SolverTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 testSolverUpdateSingle()
  92. {
  93. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  94. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.1'));
  95. $this->reposComplete();
  96. $this->request->update('A');
  97. $this->checkSolverResult(array(
  98. array('job' => 'update', 'from' => $packageA, 'to' => $newPackageA),
  99. ));
  100. }
  101. public function testSolverUpdateCurrent()
  102. {
  103. $this->repoInstalled->addPackage($this->getPackage('A', '1.0'));
  104. $this->repo->addPackage($this->getPackage('A', '1.0'));
  105. $this->reposComplete();
  106. $this->request->update('A');
  107. $this->checkSolverResult(array());
  108. }
  109. public function testSolverUpdateConstrained()
  110. {
  111. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  112. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  113. $this->repo->addPackage($this->getPackage('A', '2.0'));
  114. $this->reposComplete();
  115. $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
  116. $this->request->update('A');
  117. $this->checkSolverResult(array(array(
  118. 'job' => 'update',
  119. 'from' => $packageA,
  120. 'to' => $newPackageA,
  121. )));
  122. }
  123. public function testSolverUpdateFullyConstrained()
  124. {
  125. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  126. $this->repo->addPackage($newPackageA = $this->getPackage('A', '1.2'));
  127. $this->repo->addPackage($this->getPackage('A', '2.0'));
  128. $this->reposComplete();
  129. $this->request->install('A', new VersionConstraint('<', '2.0.0.0'));
  130. $this->request->update('A', new VersionConstraint('=', '1.0.0.0'));
  131. $this->checkSolverResult(array(array(
  132. 'job' => 'update',
  133. 'from' => $packageA,
  134. 'to' => $newPackageA,
  135. )));
  136. }
  137. public function testSolverAllJobs()
  138. {
  139. $this->repoInstalled->addPackage($packageD = $this->getPackage('D', '1.0'));
  140. $this->repoInstalled->addPackage($oldPackageC = $this->getPackage('C', '1.0'));
  141. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  142. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  143. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  144. $this->repo->addPackage($packageC = $this->getPackage('C', '1.1'));
  145. $this->repo->addPackage($this->getPackage('D', '1.0'));
  146. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
  147. $this->reposComplete();
  148. $this->request->install('A');
  149. $this->request->update('C');
  150. $this->request->remove('D');
  151. $this->checkSolverResult(array(
  152. array('job' => 'update', 'from' => $oldPackageC, 'to' => $packageC),
  153. array('job' => 'install', 'package' => $packageB),
  154. array('job' => 'remove', 'package' => $packageD),
  155. array('job' => 'install', 'package' => $packageA),
  156. ));
  157. }
  158. public function testSolverThreeAlternativeRequireAndConflict()
  159. {
  160. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  161. $this->repo->addPackage($middlePackageB = $this->getPackage('B', '1.0'));
  162. $this->repo->addPackage($newPackageB = $this->getPackage('B', '1.1'));
  163. $this->repo->addPackage($oldPackageB = $this->getPackage('B', '0.9'));
  164. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('<', '1.1'), 'requires')));
  165. $packageA->setConflicts(array(new Link('A', 'B', new VersionConstraint('<', '1.0'), 'conflicts')));
  166. $this->reposComplete();
  167. $this->request->install('A');
  168. $this->checkSolverResult(array(
  169. array('job' => 'install', 'package' => $middlePackageB),
  170. array('job' => 'install', 'package' => $packageA),
  171. ));
  172. }
  173. public function testSolverObsolete()
  174. {
  175. $this->repoInstalled->addPackage($packageA = $this->getPackage('A', '1.0'));
  176. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  177. $packageB->setReplaces(array(new Link('B', 'A', null)));
  178. $this->reposComplete();
  179. $this->request->install('B');
  180. $this->checkSolverResult(array(
  181. array('job' => 'update', 'from' => $packageA, 'to' => $packageB),
  182. ));
  183. }
  184. public function testInstallOneOfTwoAlternatives()
  185. {
  186. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  187. $this->repo->addPackage($packageB = $this->getPackage('A', '1.0'));
  188. $this->reposComplete();
  189. $this->request->install('A');
  190. $this->checkSolverResult(array(
  191. array('job' => 'install', 'package' => $packageA),
  192. ));
  193. }
  194. public function testInstallProvider()
  195. {
  196. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  197. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  198. $this->repo->addPackage($packageB = $this->getPackage('B', '0.8'));
  199. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  200. $packageQ->setProvides(array(new Link('Q', 'B', new VersionConstraint('=', '1.0'), 'provides')));
  201. $this->reposComplete();
  202. $this->request->install('A');
  203. $this->checkSolverResult(array(
  204. array('job' => 'install', 'package' => $packageQ),
  205. array('job' => 'install', 'package' => $packageA),
  206. ));
  207. }
  208. public function testSkipReplacerOfExistingPackage()
  209. {
  210. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  211. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  212. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  213. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  214. $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
  215. $this->reposComplete();
  216. $this->request->install('A');
  217. $this->checkSolverResult(array(
  218. array('job' => 'install', 'package' => $packageB),
  219. array('job' => 'install', 'package' => $packageA),
  220. ));
  221. }
  222. public function testInstallReplacerOfMissingPackage()
  223. {
  224. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  225. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  226. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  227. $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
  228. $this->reposComplete();
  229. $this->request->install('A');
  230. $this->checkSolverResult(array(
  231. array('job' => 'install', 'package' => $packageQ),
  232. array('job' => 'install', 'package' => $packageA),
  233. ));
  234. }
  235. public function testSkipReplacedPackageIfReplacerIsSelected()
  236. {
  237. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  238. $this->repo->addPackage($packageQ = $this->getPackage('Q', '1.0'));
  239. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  240. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  241. $packageQ->setReplaces(array(new Link('Q', 'B', new VersionConstraint('>=', '1.0'), 'replaces')));
  242. $this->reposComplete();
  243. $this->request->install('A');
  244. $this->request->install('Q');
  245. $this->checkSolverResult(array(
  246. array('job' => 'install', 'package' => $packageQ),
  247. array('job' => 'install', 'package' => $packageA),
  248. ));
  249. }
  250. public function testPickOlderIfNewerConflicts()
  251. {
  252. $this->repo->addPackage($packageX = $this->getPackage('X', '1.0'));
  253. $packageX->setRequires(array(
  254. new Link('X', 'A', new VersionConstraint('>=', '2.0.0.0'), 'requires'),
  255. new Link('X', 'B', new VersionConstraint('>=', '2.0.0.0'), 'requires')));
  256. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0.0'));
  257. $this->repo->addPackage($newPackageA = $this->getPackage('A', '2.1.0'));
  258. $this->repo->addPackage($newPackageB = $this->getPackage('B', '2.1.0'));
  259. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '2.0.0.0'), 'requires')));
  260. // new package A depends on version of package B that does not exist
  261. // => new package A is not installable
  262. $newPackageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '2.2.0.0'), 'requires')));
  263. // add a package S replacing both A and B, so that S and B or S and A cannot be simultaneously installed
  264. // but an alternative option for A and B both exists
  265. // this creates a more difficult so solve conflict
  266. $this->repo->addPackage($packageS = $this->getPackage('S', '2.0.0'));
  267. $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')));
  268. $this->reposComplete();
  269. $this->request->install('X');
  270. $this->checkSolverResult(array(
  271. array('job' => 'install', 'package' => $packageA),
  272. array('job' => 'install', 'package' => $newPackageB),
  273. array('job' => 'install', 'package' => $packageX),
  274. ));
  275. }
  276. public function testInstallCircularRequire()
  277. {
  278. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  279. $this->repo->addPackage($packageB1 = $this->getPackage('B', '0.9'));
  280. $this->repo->addPackage($packageB2 = $this->getPackage('B', '1.1'));
  281. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  282. $packageB2->setRequires(array(new Link('B', 'A', new VersionConstraint('>=', '1.0'), 'requires')));
  283. $this->reposComplete();
  284. $this->request->install('A');
  285. $this->checkSolverResult(array(
  286. array('job' => 'install', 'package' => $packageB2),
  287. array('job' => 'install', 'package' => $packageA),
  288. ));
  289. }
  290. public function testInstallAlternativeWithCircularRequire()
  291. {
  292. $this->markTestIncomplete();
  293. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  294. $this->repo->addPackage($packageB = $this->getPackage('B', '1.0'));
  295. $this->repo->addPackage($packageC = $this->getPackage('C', '1.0'));
  296. $this->repo->addPackage($packageD = $this->getPackage('D', '1.0'));
  297. $packageA->setRequires(array(new Link('A', 'B', new VersionConstraint('>=', '1.0'), 'requires')));
  298. $packageB->setRequires(array(new Link('B', 'Virtual', new VersionConstraint('>=', '1.0'), 'requires')));
  299. $packageC->setRequires(array(new Link('C', 'Virtual', new VersionConstraint('==', '1.0'), 'provides')));
  300. $packageD->setRequires(array(new Link('D', 'Virtual', new VersionConstraint('==', '1.0'), 'provides')));
  301. $this->reposComplete();
  302. $this->request->install('A');
  303. $this->checkSolverResult(array(
  304. array('job' => 'install', 'package' => $packageC),
  305. array('job' => 'install', 'package' => $packageB),
  306. array('job' => 'install', 'package' => $packageA),
  307. ));
  308. }
  309. protected function reposComplete()
  310. {
  311. $this->pool->addRepository($this->repoInstalled);
  312. $this->pool->addRepository($this->repo);
  313. }
  314. protected function checkSolverResult(array $expected)
  315. {
  316. $transaction = $this->solver->solve($this->request);
  317. $result = array();
  318. foreach ($transaction as $operation) {
  319. if ('update' === $operation->getJobType()) {
  320. $result[] = array(
  321. 'job' => 'update',
  322. 'from' => $operation->getInitialPackage(),
  323. 'to' => $operation->getTargetPackage()
  324. );
  325. } else {
  326. $job = ('uninstall' === $operation->getJobType() ? 'remove' : 'install');
  327. $result[] = array(
  328. 'job' => $job,
  329. 'package' => $operation->getPackage()
  330. );
  331. }
  332. }
  333. $this->assertEquals($expected, $result);
  334. }
  335. }