DefaultPolicyTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\RepositoryInterface;
  14. use Composer\DependencyResolver\DefaultPolicy;
  15. use Composer\DependencyResolver\Pool;
  16. use Composer\Package\Link;
  17. use Composer\Package\AliasPackage;
  18. use Composer\Semver\Constraint\Constraint;
  19. use Composer\Test\TestCase;
  20. class DefaultPolicyTest extends TestCase
  21. {
  22. /** @var Pool */
  23. protected $pool;
  24. /** @var ArrayRepository */
  25. protected $repo;
  26. /** @var ArrayRepository */
  27. protected $repoInstalled;
  28. /** @var DefaultPolicy */
  29. protected $policy;
  30. public function setUp()
  31. {
  32. $this->pool = new Pool('dev');
  33. $this->repo = new ArrayRepository;
  34. $this->repoInstalled = new ArrayRepository;
  35. $this->policy = new DefaultPolicy;
  36. }
  37. public function testSelectSingle()
  38. {
  39. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  40. $this->pool->addRepository($this->repo);
  41. $literals = array($packageA->getId());
  42. $expected = array($packageA->getId());
  43. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  44. $this->assertSame($expected, $selected);
  45. }
  46. public function testSelectNewest()
  47. {
  48. $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0'));
  49. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  50. $this->pool->addRepository($this->repo);
  51. $literals = array($packageA1->getId(), $packageA2->getId());
  52. $expected = array($packageA2->getId());
  53. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  54. $this->assertSame($expected, $selected);
  55. }
  56. public function testSelectNewestPicksLatest()
  57. {
  58. $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0.0'));
  59. $this->repo->addPackage($packageA2 = $this->getPackage('A', '1.0.1-alpha'));
  60. $this->pool->addRepository($this->repo);
  61. $literals = array($packageA1->getId(), $packageA2->getId());
  62. $expected = array($packageA2->getId());
  63. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  64. $this->assertSame($expected, $selected);
  65. }
  66. public function testSelectNewestPicksLatestStableWithPreferStable()
  67. {
  68. $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0.0'));
  69. $this->repo->addPackage($packageA2 = $this->getPackage('A', '1.0.1-alpha'));
  70. $this->pool->addRepository($this->repo);
  71. $literals = array($packageA1->getId(), $packageA2->getId());
  72. $expected = array($packageA1->getId());
  73. $policy = new DefaultPolicy(true);
  74. $selected = $policy->selectPreferredPackages($this->pool, array(), $literals);
  75. $this->assertSame($expected, $selected);
  76. }
  77. public function testSelectNewestWithDevPicksNonDev()
  78. {
  79. $this->repo->addPackage($packageA1 = $this->getPackage('A', 'dev-foo'));
  80. $this->repo->addPackage($packageA2 = $this->getPackage('A', '1.0.0'));
  81. $this->pool->addRepository($this->repo);
  82. $literals = array($packageA1->getId(), $packageA2->getId());
  83. $expected = array($packageA2->getId());
  84. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  85. $this->assertSame($expected, $selected);
  86. }
  87. public function testSelectNewestOverInstalled()
  88. {
  89. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  90. $this->repoInstalled->addPackage($packageAInstalled = $this->getPackage('A', '1.0'));
  91. $this->pool->addRepository($this->repoInstalled);
  92. $this->pool->addRepository($this->repo);
  93. $literals = array($packageA->getId(), $packageAInstalled->getId());
  94. $expected = array($packageA->getId());
  95. $selected = $this->policy->selectPreferredPackages($this->pool, $this->mapFromRepo($this->repoInstalled), $literals);
  96. $this->assertSame($expected, $selected);
  97. }
  98. public function testSelectFirstRepo()
  99. {
  100. $otherRepository = new ArrayRepository;
  101. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  102. $otherRepository->addPackage($packageAImportant = $this->getPackage('A', '1.0'));
  103. $this->pool->addRepository($this->repoInstalled);
  104. $this->pool->addRepository($otherRepository);
  105. $this->pool->addRepository($this->repo);
  106. $literals = array($packageA->getId(), $packageAImportant->getId());
  107. $expected = array($packageAImportant->getId());
  108. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  109. $this->assertSame($expected, $selected);
  110. }
  111. public function testRepositoryOrderingAffectsPriority()
  112. {
  113. $repo1 = new ArrayRepository;
  114. $repo2 = new ArrayRepository;
  115. $repo1->addPackage($package1 = $this->getPackage('A', '1.0'));
  116. $repo1->addPackage($package2 = $this->getPackage('A', '1.1'));
  117. $repo2->addPackage($package3 = $this->getPackage('A', '1.1'));
  118. $repo2->addPackage($package4 = $this->getPackage('A', '1.2'));
  119. $this->pool->addRepository($repo1);
  120. $this->pool->addRepository($repo2);
  121. $literals = array($package1->getId(), $package2->getId(), $package3->getId(), $package4->getId());
  122. $expected = array($package2->getId());
  123. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  124. $this->assertSame($expected, $selected);
  125. $this->pool = new Pool('dev');
  126. $this->pool->addRepository($repo2);
  127. $this->pool->addRepository($repo1);
  128. $expected = array($package4->getId());
  129. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  130. $this->assertSame($expected, $selected);
  131. }
  132. public function testSelectLocalReposFirst()
  133. {
  134. $repoImportant = new ArrayRepository;
  135. $this->repo->addPackage($packageA = $this->getPackage('A', 'dev-master'));
  136. $this->repo->addPackage($packageAAlias = new AliasPackage($packageA, '2.1.9999999.9999999-dev', '2.1.x-dev'));
  137. $repoImportant->addPackage($packageAImportant = $this->getPackage('A', 'dev-feature-a'));
  138. $repoImportant->addPackage($packageAAliasImportant = new AliasPackage($packageAImportant, '2.1.9999999.9999999-dev', '2.1.x-dev'));
  139. $repoImportant->addPackage($packageA2Important = $this->getPackage('A', 'dev-master'));
  140. $repoImportant->addPackage($packageA2AliasImportant = new AliasPackage($packageA2Important, '2.1.9999999.9999999-dev', '2.1.x-dev'));
  141. $packageAAliasImportant->setRootPackageAlias(true);
  142. $this->pool->addRepository($this->repoInstalled);
  143. $this->pool->addRepository($repoImportant);
  144. $this->pool->addRepository($this->repo);
  145. $packages = $this->pool->whatProvides('a', new Constraint('=', '2.1.9999999.9999999-dev'));
  146. $literals = array();
  147. foreach ($packages as $package) {
  148. $literals[] = $package->getId();
  149. }
  150. $expected = array($packageAAliasImportant->getId());
  151. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  152. $this->assertSame($expected, $selected);
  153. }
  154. public function testSelectAllProviders()
  155. {
  156. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  157. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  158. $packageA->setProvides(array(new Link('A', 'X', new Constraint('==', '1.0'), 'provides')));
  159. $packageB->setProvides(array(new Link('B', 'X', new Constraint('==', '1.0'), 'provides')));
  160. $this->pool->addRepository($this->repo);
  161. $literals = array($packageA->getId(), $packageB->getId());
  162. $expected = $literals;
  163. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  164. $this->assertSame($expected, $selected);
  165. }
  166. public function testPreferNonReplacingFromSameRepo()
  167. {
  168. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  169. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  170. $packageB->setReplaces(array(new Link('B', 'A', new Constraint('==', '1.0'), 'replaces')));
  171. $this->pool->addRepository($this->repo);
  172. $literals = array($packageA->getId(), $packageB->getId());
  173. $expected = $literals;
  174. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals);
  175. $this->assertSame($expected, $selected);
  176. }
  177. public function testPreferReplacingPackageFromSameVendor()
  178. {
  179. // test with default order
  180. $this->repo->addPackage($packageB = $this->getPackage('vendor-b/replacer', '1.0'));
  181. $this->repo->addPackage($packageA = $this->getPackage('vendor-a/replacer', '1.0'));
  182. $packageA->setReplaces(array(new Link('vendor-a/replacer', 'vendor-a/package', new Constraint('==', '1.0'), 'replaces')));
  183. $packageB->setReplaces(array(new Link('vendor-b/replacer', 'vendor-a/package', new Constraint('==', '1.0'), 'replaces')));
  184. $this->pool->addRepository($this->repo);
  185. $literals = array($packageA->getId(), $packageB->getId());
  186. $expected = $literals;
  187. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals, 'vendor-a/package');
  188. $this->assertEquals($expected, $selected);
  189. // test with reversed order in repo
  190. $repo = new ArrayRepository;
  191. $repo->addPackage($packageA = clone $packageA);
  192. $repo->addPackage($packageB = clone $packageB);
  193. $pool = new Pool('dev');
  194. $pool->addRepository($this->repo);
  195. $literals = array($packageA->getId(), $packageB->getId());
  196. $expected = $literals;
  197. $selected = $this->policy->selectPreferredPackages($this->pool, array(), $literals, 'vendor-a/package');
  198. $this->assertSame($expected, $selected);
  199. }
  200. protected function mapFromRepo(RepositoryInterface $repo)
  201. {
  202. $map = array();
  203. foreach ($repo->getPackages() as $package) {
  204. $map[$package->getId()] = true;
  205. }
  206. return $map;
  207. }
  208. public function testSelectLowest()
  209. {
  210. $policy = new DefaultPolicy(false, true);
  211. $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0'));
  212. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  213. $this->pool->addRepository($this->repo);
  214. $literals = array($packageA1->getId(), $packageA2->getId());
  215. $expected = array($packageA1->getId());
  216. $selected = $policy->selectPreferredPackages($this->pool, array(), $literals);
  217. $this->assertSame($expected, $selected);
  218. }
  219. }