ComposerRepositoryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Repository;
  12. use Composer\IO\NullIO;
  13. use Composer\Test\Mock\FactoryMock;
  14. use Composer\TestCase;
  15. use Composer\Package\Loader\ArrayLoader;
  16. use Composer\Semver\VersionParser;
  17. class ComposerRepositoryTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider loadDataProvider
  21. */
  22. public function testLoadData(array $expected, array $repoPackages)
  23. {
  24. $repoConfig = array(
  25. 'url' => 'http://example.org',
  26. );
  27. $repository = $this->getMock(
  28. 'Composer\Repository\ComposerRepository',
  29. array(
  30. 'loadRootServerFile',
  31. 'createPackage',
  32. ),
  33. array(
  34. $repoConfig,
  35. new NullIO,
  36. FactoryMock::createConfig(),
  37. )
  38. );
  39. $repository
  40. ->expects($this->exactly(2))
  41. ->method('loadRootServerFile')
  42. ->will($this->returnValue($repoPackages));
  43. foreach ($expected as $at => $arg) {
  44. $stubPackage = $this->getPackage('stub/stub', '1.0.0');
  45. $repository
  46. ->expects($this->at($at + 2))
  47. ->method('createPackage')
  48. ->with($this->identicalTo($arg), $this->equalTo('Composer\Package\CompletePackage'))
  49. ->will($this->returnValue($stubPackage));
  50. }
  51. // Triggers initialization
  52. $packages = $repository->getPackages();
  53. // Final sanity check, ensure the correct number of packages were added.
  54. $this->assertCount(count($expected), $packages);
  55. }
  56. public function loadDataProvider()
  57. {
  58. return array(
  59. // Old repository format
  60. array(
  61. array(
  62. array('name' => 'foo/bar', 'version' => '1.0.0'),
  63. ),
  64. array('foo/bar' => array(
  65. 'name' => 'foo/bar',
  66. 'versions' => array(
  67. '1.0.0' => array('name' => 'foo/bar', 'version' => '1.0.0'),
  68. ),
  69. )),
  70. ),
  71. // New repository format
  72. array(
  73. array(
  74. array('name' => 'bar/foo', 'version' => '3.14'),
  75. array('name' => 'bar/foo', 'version' => '3.145'),
  76. ),
  77. array('packages' => array(
  78. 'bar/foo' => array(
  79. '3.14' => array('name' => 'bar/foo', 'version' => '3.14'),
  80. '3.145' => array('name' => 'bar/foo', 'version' => '3.145'),
  81. ),
  82. )),
  83. ),
  84. );
  85. }
  86. public function testWhatProvides()
  87. {
  88. $repo = $this->getMockBuilder('Composer\Repository\ComposerRepository')
  89. ->disableOriginalConstructor()
  90. ->setMethods(array('fetchFile'))
  91. ->getMock();
  92. $cache = $this->getMockBuilder('Composer\Cache')->disableOriginalConstructor()->getMock();
  93. $cache->expects($this->any())
  94. ->method('sha256')
  95. ->will($this->returnValue(false));
  96. $properties = array(
  97. 'cache' => $cache,
  98. 'loader' => new ArrayLoader(),
  99. 'providerListing' => array('p/a.json' => array('sha256' => 'xxx')),
  100. );
  101. foreach ($properties as $property => $value) {
  102. $ref = new \ReflectionProperty($repo, $property);
  103. $ref->setAccessible(true);
  104. $ref->setValue($repo, $value);
  105. }
  106. $repo->expects($this->any())
  107. ->method('fetchFile')
  108. ->will($this->returnValue(array(
  109. 'packages' => array(
  110. array(array(
  111. 'uid' => 1,
  112. 'name' => 'a',
  113. 'version' => 'dev-master',
  114. 'extra' => array('branch-alias' => array('dev-master' => '1.0.x-dev')),
  115. )),
  116. array(array(
  117. 'uid' => 2,
  118. 'name' => 'a',
  119. 'version' => 'dev-develop',
  120. 'extra' => array('branch-alias' => array('dev-develop' => '1.1.x-dev')),
  121. )),
  122. array(array(
  123. 'uid' => 3,
  124. 'name' => 'a',
  125. 'version' => '0.6',
  126. )),
  127. ),
  128. )));
  129. $pool = $this->getMock('Composer\DependencyResolver\Pool');
  130. $pool->expects($this->any())
  131. ->method('isPackageAcceptable')
  132. ->will($this->returnValue(true));
  133. $versionParser = new VersionParser();
  134. $repo->setRootAliases(array(
  135. 'a' => array(
  136. $versionParser->normalize('0.6') => array('alias' => 'dev-feature', 'alias_normalized' => $versionParser->normalize('dev-feature')),
  137. $versionParser->normalize('1.1.x-dev') => array('alias' => '1.0', 'alias_normalized' => $versionParser->normalize('1.0')),
  138. ),
  139. ));
  140. $packages = $repo->whatProvides($pool, 'a');
  141. $this->assertCount(7, $packages);
  142. $this->assertEquals(array('1', '1-alias', '2', '2-alias', '2-root', '3', '3-root'), array_keys($packages));
  143. $this->assertInstanceOf('Composer\Package\AliasPackage', $packages['2-root']);
  144. $this->assertSame($packages['2'], $packages['2-root']->getAliasOf());
  145. $this->assertSame($packages['2'], $packages['2-alias']->getAliasOf());
  146. }
  147. }