DefaultPolicyTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\DependencyResolver\Literal;
  17. use Composer\Package\Link;
  18. use Composer\Package\AliasPackage;
  19. use Composer\Package\LinkConstraint\VersionConstraint;
  20. use Composer\Test\TestCase;
  21. class DefaultPolicyTest extends TestCase
  22. {
  23. protected $pool;
  24. protected $repo;
  25. protected $repoInstalled;
  26. protected $request;
  27. protected $policy;
  28. public function setUp()
  29. {
  30. $this->pool = new Pool;
  31. $this->repo = new ArrayRepository;
  32. $this->repoInstalled = new ArrayRepository;
  33. $this->policy = new DefaultPolicy;
  34. }
  35. public function testSelectSingle()
  36. {
  37. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  38. $this->pool->addRepository($this->repo);
  39. $literals = array(new Literal($packageA, true));
  40. $expected = array(new Literal($packageA, true));
  41. $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
  42. $this->assertEquals($expected, $selected);
  43. }
  44. public function testSelectNewest()
  45. {
  46. $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0'));
  47. $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
  48. $this->pool->addRepository($this->repo);
  49. $literals = array(new Literal($packageA1, true), new Literal($packageA2, true));
  50. $expected = array(new Literal($packageA2, true));
  51. $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
  52. $this->assertEquals($expected, $selected);
  53. }
  54. public function testSelectNewestOverInstalled()
  55. {
  56. $this->repo->addPackage($packageA = $this->getPackage('A', '2.0'));
  57. $this->repoInstalled->addPackage($packageAInstalled = $this->getPackage('A', '1.0'));
  58. $this->pool->addRepository($this->repoInstalled);
  59. $this->pool->addRepository($this->repo);
  60. $literals = array(new Literal($packageA, true), new Literal($packageAInstalled, true));
  61. $expected = array(new Literal($packageA, true));
  62. $selected = $this->policy->selectPreferedPackages($this->pool, $this->mapFromRepo($this->repoInstalled), $literals);
  63. $this->assertEquals($expected, $selected);
  64. }
  65. public function testSelectFirstRepo()
  66. {
  67. $this->repoImportant = new ArrayRepository;
  68. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  69. $this->repoImportant->addPackage($packageAImportant = $this->getPackage('A', '1.0'));
  70. $this->pool->addRepository($this->repoInstalled);
  71. $this->pool->addRepository($this->repoImportant);
  72. $this->pool->addRepository($this->repo);
  73. $literals = array(new Literal($packageA, true), new Literal($packageAImportant, true));
  74. $expected = array(new Literal($packageAImportant, true));
  75. $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
  76. $this->assertEquals($expected, $selected);
  77. }
  78. public function testSelectLocalReposFirst()
  79. {
  80. $this->repoImportant = new ArrayRepository;
  81. $this->repo->addPackage($packageA = $this->getPackage('A', 'dev-master'));
  82. $this->repo->addPackage($packageAAlias = new AliasPackage($packageA, '2.1.9999999.9999999-dev', '2.1.x-dev'));
  83. $this->repoImportant->addPackage($packageAImportant = $this->getPackage('A', 'dev-feature-a'));
  84. $this->repoImportant->addPackage($packageAAliasImportant = new AliasPackage($packageAImportant, '2.1.9999999.9999999-dev', '2.1.x-dev'));
  85. $this->repoImportant->addPackage($packageA2Important = $this->getPackage('A', 'dev-master'));
  86. $this->repoImportant->addPackage($packageA2AliasImportant = new AliasPackage($packageA2Important, '2.1.9999999.9999999-dev', '2.1.x-dev'));
  87. $packageAAliasImportant->setRootPackageAlias(true);
  88. $this->pool->addRepository($this->repoInstalled);
  89. $this->pool->addRepository($this->repoImportant);
  90. $this->pool->addRepository($this->repo);
  91. $packages = $this->pool->whatProvides('a', new VersionConstraint('=', '2.1.9999999.9999999-dev'));
  92. $literals = array();
  93. foreach ($packages as $package) {
  94. $literals[] = new Literal($package, true);
  95. }
  96. $expected = array(new Literal($packageAAliasImportant, true));
  97. $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
  98. $this->assertEquals($expected, $selected);
  99. }
  100. public function testSelectAllProviders()
  101. {
  102. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  103. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  104. $packageA->setProvides(array(new Link('A', 'X', new VersionConstraint('==', '1.0'), 'provides')));
  105. $packageB->setProvides(array(new Link('B', 'X', new VersionConstraint('==', '1.0'), 'provides')));
  106. $this->pool->addRepository($this->repo);
  107. $literals = array(new Literal($packageA, true), new Literal($packageB, true));
  108. $expected = $literals;
  109. $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
  110. $this->assertEquals($expected, $selected);
  111. }
  112. public function testPreferNonReplacingFromSameRepo()
  113. {
  114. $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
  115. $this->repo->addPackage($packageB = $this->getPackage('B', '2.0'));
  116. $packageB->setReplaces(array(new Link('B', 'A', new VersionConstraint('==', '1.0'), 'replaces')));
  117. $this->pool->addRepository($this->repo);
  118. $literals = array(new Literal($packageA, true), new Literal($packageB, true));
  119. $expected = array(new Literal($packageA, true), new Literal($packageB, true));
  120. $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
  121. $this->assertEquals($expected, $selected);
  122. }
  123. protected function mapFromRepo(RepositoryInterface $repo)
  124. {
  125. $map = array();
  126. foreach ($repo->getPackages() as $package) {
  127. $map[$package->getId()] = true;
  128. }
  129. return $map;
  130. }
  131. }