DefaultPolicyTest.php 11 KB

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