SolverTest.php 25 KB

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