ComposerRepositoryTest.php 5.7 KB

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