PackageSorterTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Util;
  12. use Composer\Package\Link;
  13. use Composer\Package\Package;
  14. use Composer\Test\TestCase;
  15. use Composer\Util\PackageSorter;
  16. class PackageSorterTest extends TestCase
  17. {
  18. public function testSortingDoesNothingWithNoDependencies()
  19. {
  20. $packages[] = $this->createPackage('foo/bar1', array());
  21. $packages[] = $this->createPackage('foo/bar2', array());
  22. $packages[] = $this->createPackage('foo/bar3', array());
  23. $packages[] = $this->createPackage('foo/bar4', array());
  24. $sortedPackages = PackageSorter::sortPackages($packages);
  25. self::assertSame($packages, $sortedPackages);
  26. }
  27. public function sortingOrdersDependenciesHigherThanPackageDataProvider()
  28. {
  29. return array(
  30. 'one package is dep' => array(
  31. array(
  32. $this->createPackage('foo/bar1', array('foo/bar4')),
  33. $this->createPackage('foo/bar2', array('foo/bar4')),
  34. $this->createPackage('foo/bar3', array('foo/bar4')),
  35. $this->createPackage('foo/bar4', array()),
  36. ),
  37. array(
  38. 'foo/bar4',
  39. 'foo/bar1',
  40. 'foo/bar2',
  41. 'foo/bar3',
  42. ),
  43. ),
  44. 'one package has more deps' => array(
  45. array(
  46. $this->createPackage('foo/bar1', array('foo/bar2')),
  47. $this->createPackage('foo/bar2', array('foo/bar4')),
  48. $this->createPackage('foo/bar3', array('foo/bar4')),
  49. $this->createPackage('foo/bar4', array()),
  50. ),
  51. array(
  52. 'foo/bar4',
  53. 'foo/bar2',
  54. 'foo/bar1',
  55. 'foo/bar3',
  56. ),
  57. ),
  58. 'package is required by many, but requires one other' => array(
  59. array(
  60. $this->createPackage('foo/bar1', array('foo/bar3')),
  61. $this->createPackage('foo/bar2', array('foo/bar3')),
  62. $this->createPackage('foo/bar3', array('foo/bar4')),
  63. $this->createPackage('foo/bar4', array()),
  64. $this->createPackage('foo/bar5', array('foo/bar3')),
  65. $this->createPackage('foo/bar6', array('foo/bar3')),
  66. ),
  67. array(
  68. 'foo/bar4',
  69. 'foo/bar3',
  70. 'foo/bar1',
  71. 'foo/bar2',
  72. 'foo/bar5',
  73. 'foo/bar6',
  74. ),
  75. ),
  76. 'one package has many requires' => array(
  77. array(
  78. $this->createPackage('foo/bar1', array('foo/bar2')),
  79. $this->createPackage('foo/bar2', array()),
  80. $this->createPackage('foo/bar3', array('foo/bar4')),
  81. $this->createPackage('foo/bar4', array()),
  82. $this->createPackage('foo/bar5', array('foo/bar2')),
  83. $this->createPackage('foo/bar6', array('foo/bar2')),
  84. ),
  85. array(
  86. 'foo/bar2',
  87. 'foo/bar4',
  88. 'foo/bar1',
  89. 'foo/bar3',
  90. 'foo/bar5',
  91. 'foo/bar6',
  92. ),
  93. ),
  94. );
  95. }
  96. /**
  97. * @dataProvider sortingOrdersDependenciesHigherThanPackageDataProvider
  98. * @param array $packages
  99. * @param array $expectedOrderedList
  100. */
  101. public function testSortingOrdersDependenciesHigherThanPackage($packages, $expectedOrderedList)
  102. {
  103. $sortedPackages = PackageSorter::sortPackages($packages);
  104. $sortedPackageNames = array_map(function ($package) { return $package->getName(); }, $sortedPackages);
  105. self::assertSame($expectedOrderedList, $sortedPackageNames);
  106. }
  107. private function createPackage($name, $requires)
  108. {
  109. $package = new Package($name, '1.0.0.0', '1.0.0');
  110. $links = array();
  111. foreach ($requires as $requireName) {
  112. $links[] = new Link($package->getName(), $requireName);
  113. }
  114. $package->setRequires($links);
  115. return $package;
  116. }
  117. }