ComposerRepositoryTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\Test\TestCase;
  17. use Composer\Package\Loader\ArrayLoader;
  18. use Composer\Package\Version\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->getMockBuilder('Composer\Repository\ComposerRepository')
  30. ->setMethods(array('loadRootServerFile', 'createPackage'))
  31. ->setConstructorArgs(array(
  32. $repoConfig,
  33. new NullIO,
  34. FactoryMock::createConfig(),
  35. $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(),
  36. $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock()
  37. ))
  38. ->getMock();
  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('a' => array('sha256' => 'xxx')),
  100. 'providersUrl' => 'https://dummy.test.link/to/%package%/file',
  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. $versionParser = new VersionParser();
  131. $repo->setRootAliases(array(
  132. 'a' => array(
  133. $versionParser->normalize('0.6') => array('alias' => 'dev-feature', 'alias_normalized' => $versionParser->normalize('dev-feature')),
  134. $versionParser->normalize('1.1.x-dev') => array('alias' => '1.0', 'alias_normalized' => $versionParser->normalize('1.0')),
  135. ),
  136. ));
  137. $packages = $repo->whatProvides('a', false, array($this, 'isPackageAcceptableReturnTrue'));
  138. $this->assertCount(7, $packages);
  139. $this->assertEquals(array('1', '1-alias', '2', '2-alias', '2-root', '3', '3-root'), array_keys($packages));
  140. $this->assertInstanceOf('Composer\Package\AliasPackage', $packages['2-root']);
  141. $this->assertSame($packages['2'], $packages['2-root']->getAliasOf());
  142. $this->assertSame($packages['2'], $packages['2-alias']->getAliasOf());
  143. }
  144. public function isPackageAcceptableReturnTrue()
  145. {
  146. return true;
  147. }
  148. public function testSearchWithType()
  149. {
  150. $repoConfig = array(
  151. 'url' => 'http://example.org',
  152. );
  153. $result = array(
  154. 'results' => array(
  155. array(
  156. 'name' => 'foo',
  157. 'description' => null,
  158. ),
  159. ),
  160. );
  161. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $httpDownloader->expects($this->at(0))
  168. ->method('get')
  169. ->with($url = 'http://example.org/packages.json')
  170. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode(array('search' => '/search.json?q=%query%&type=%type%'))));
  171. $httpDownloader->expects($this->at(1))
  172. ->method('get')
  173. ->with($url = 'http://example.org/search.json?q=foo&type=composer-plugin')
  174. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode($result)));
  175. $httpDownloader->expects($this->at(2))
  176. ->method('get')
  177. ->with($url = 'http://example.org/search.json?q=foo&type=library')
  178. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode(array())));
  179. $repository = new ComposerRepository($repoConfig, new NullIO, FactoryMock::createConfig(), $eventDispatcher, $httpDownloader);
  180. $this->assertSame(
  181. array(array('name' => 'foo', 'description' => null)),
  182. $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'composer-plugin')
  183. );
  184. $this->assertEmpty(
  185. $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'library')
  186. );
  187. }
  188. }