ComposerRepositoryTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\Repository\ComposerRepository;
  14. use Composer\Repository\RepositoryInterface;
  15. use Composer\Test\Mock\FactoryMock;
  16. use Composer\TestCase;
  17. use Composer\Package\Loader\ArrayLoader;
  18. use Composer\Semver\VersionParser;
  19. class ComposerRepositoryTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider loadDataProvider
  23. */
  24. public function testLoadData(array $expected, array $repoPackages)
  25. {
  26. $repoConfig = array(
  27. 'url' => 'http://example.org',
  28. );
  29. $repository = $this->getMock(
  30. 'Composer\Repository\ComposerRepository',
  31. array(
  32. 'loadRootServerFile',
  33. 'createPackage',
  34. ),
  35. array(
  36. $repoConfig,
  37. new NullIO,
  38. FactoryMock::createConfig(),
  39. )
  40. );
  41. $repository
  42. ->expects($this->exactly(2))
  43. ->method('loadRootServerFile')
  44. ->will($this->returnValue($repoPackages));
  45. foreach ($expected as $at => $arg) {
  46. $stubPackage = $this->getPackage('stub/stub', '1.0.0');
  47. $repository
  48. ->expects($this->at($at + 2))
  49. ->method('createPackage')
  50. ->with($this->identicalTo($arg), $this->equalTo('Composer\Package\CompletePackage'))
  51. ->will($this->returnValue($stubPackage));
  52. }
  53. // Triggers initialization
  54. $packages = $repository->getPackages();
  55. // Final sanity check, ensure the correct number of packages were added.
  56. $this->assertCount(count($expected), $packages);
  57. }
  58. public function loadDataProvider()
  59. {
  60. return array(
  61. // Old repository format
  62. array(
  63. array(
  64. array('name' => 'foo/bar', 'version' => '1.0.0'),
  65. ),
  66. array('foo/bar' => array(
  67. 'name' => 'foo/bar',
  68. 'versions' => array(
  69. '1.0.0' => array('name' => 'foo/bar', 'version' => '1.0.0'),
  70. ),
  71. )),
  72. ),
  73. // New repository format
  74. array(
  75. array(
  76. array('name' => 'bar/foo', 'version' => '3.14'),
  77. array('name' => 'bar/foo', 'version' => '3.145'),
  78. ),
  79. array('packages' => array(
  80. 'bar/foo' => array(
  81. '3.14' => array('name' => 'bar/foo', 'version' => '3.14'),
  82. '3.145' => array('name' => 'bar/foo', 'version' => '3.145'),
  83. ),
  84. )),
  85. ),
  86. );
  87. }
  88. public function testWhatProvides()
  89. {
  90. $repo = $this->getMockBuilder('Composer\Repository\ComposerRepository')
  91. ->disableOriginalConstructor()
  92. ->setMethods(array('fetchFile'))
  93. ->getMock();
  94. $cache = $this->getMockBuilder('Composer\Cache')->disableOriginalConstructor()->getMock();
  95. $cache->expects($this->any())
  96. ->method('sha256')
  97. ->will($this->returnValue(false));
  98. $properties = array(
  99. 'cache' => $cache,
  100. 'loader' => new ArrayLoader(),
  101. 'providerListing' => array('a' => array('sha256' => 'xxx')),
  102. 'providersUrl' => 'https://dummy.test.link/to/%package%/file',
  103. );
  104. foreach ($properties as $property => $value) {
  105. $ref = new \ReflectionProperty($repo, $property);
  106. $ref->setAccessible(true);
  107. $ref->setValue($repo, $value);
  108. }
  109. $repo->expects($this->any())
  110. ->method('fetchFile')
  111. ->will($this->returnValue(array(
  112. 'packages' => array(
  113. array(array(
  114. 'uid' => 1,
  115. 'name' => 'a',
  116. 'version' => 'dev-master',
  117. 'extra' => array('branch-alias' => array('dev-master' => '1.0.x-dev')),
  118. )),
  119. array(array(
  120. 'uid' => 2,
  121. 'name' => 'a',
  122. 'version' => 'dev-develop',
  123. 'extra' => array('branch-alias' => array('dev-develop' => '1.1.x-dev')),
  124. )),
  125. array(array(
  126. 'uid' => 3,
  127. 'name' => 'a',
  128. 'version' => '0.6',
  129. )),
  130. ),
  131. )));
  132. $pool = $this->getMock('Composer\DependencyResolver\Pool');
  133. $pool->expects($this->any())
  134. ->method('isPackageAcceptable')
  135. ->will($this->returnValue(true));
  136. $versionParser = new VersionParser();
  137. $repo->setRootAliases(array(
  138. 'a' => array(
  139. $versionParser->normalize('0.6') => array('alias' => 'dev-feature', 'alias_normalized' => $versionParser->normalize('dev-feature')),
  140. $versionParser->normalize('1.1.x-dev') => array('alias' => '1.0', 'alias_normalized' => $versionParser->normalize('1.0')),
  141. ),
  142. ));
  143. $packages = $repo->whatProvides($pool, 'a');
  144. $this->assertCount(7, $packages);
  145. $this->assertEquals(array('1', '1-alias', '2', '2-alias', '2-root', '3', '3-root'), array_keys($packages));
  146. $this->assertInstanceOf('Composer\Package\AliasPackage', $packages['2-root']);
  147. $this->assertSame($packages['2'], $packages['2-root']->getAliasOf());
  148. $this->assertSame($packages['2'], $packages['2-alias']->getAliasOf());
  149. }
  150. public function testSearchWithType()
  151. {
  152. $repoConfig = array(
  153. 'url' => 'http://example.org',
  154. );
  155. $result = array(
  156. 'results' => array(
  157. array(
  158. 'name' => 'foo',
  159. 'description' => null,
  160. ),
  161. ),
  162. );
  163. $rfs = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $rfs->expects($this->at(0))
  167. ->method('getContents')
  168. ->with('example.org', 'http://example.org/packages.json', false)
  169. ->willReturn(json_encode(array('search' => '/search.json?q=%query%&type=%type%')));
  170. $rfs->expects($this->at(1))
  171. ->method('getContents')
  172. ->with('example.org', 'http://example.org/search.json?q=foo&type=composer-plugin', false)
  173. ->willReturn(json_encode($result));
  174. $repository = new ComposerRepository($repoConfig, new NullIO, FactoryMock::createConfig(), null, $rfs);
  175. $this->assertSame(
  176. array(array('name' => 'foo', 'description' => null)),
  177. $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'composer-plugin')
  178. );
  179. $this->assertEmpty(
  180. $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'library')
  181. );
  182. }
  183. }