SolverTest.php 16 KB

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