ComposerRepositoryTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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', 'createPackages'))
  31. ->setConstructorArgs(array(
  32. $repoConfig,
  33. new NullIO,
  34. FactoryMock::createConfig(),
  35. $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
  36. $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
  37. ))
  38. ->getMock();
  39. $repository
  40. ->expects($this->exactly(2))
  41. ->method('loadRootServerFile')
  42. ->will($this->returnValue($repoPackages));
  43. $stubs = array();
  44. foreach ($expected as $at => $arg) {
  45. $stubs[] = $this->getPackage('stub/stub', '1.0.0');
  46. }
  47. $repository
  48. ->expects($this->at(2))
  49. ->method('createPackages')
  50. ->with($this->identicalTo($expected), $this->equalTo('Composer\Package\CompletePackage'))
  51. ->will($this->returnValue($stubs));
  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('a' => array('sha256' => 'xxx')),
  101. 'providersUrl' => 'https://dummy.test.link/to/%package%/file',
  102. );
  103. foreach ($properties as $property => $value) {
  104. $ref = new \ReflectionProperty($repo, $property);
  105. $ref->setAccessible(true);
  106. $ref->setValue($repo, $value);
  107. }
  108. $repo->expects($this->any())
  109. ->method('fetchFile')
  110. ->will($this->returnValue(array(
  111. 'packages' => array(
  112. array(array(
  113. 'uid' => 1,
  114. 'name' => 'a',
  115. 'version' => 'dev-master',
  116. 'extra' => array('branch-alias' => array('dev-master' => '1.0.x-dev')),
  117. )),
  118. array(array(
  119. 'uid' => 2,
  120. 'name' => 'a',
  121. 'version' => 'dev-develop',
  122. 'extra' => array('branch-alias' => array('dev-develop' => '1.1.x-dev')),
  123. )),
  124. array(array(
  125. 'uid' => 3,
  126. 'name' => 'a',
  127. 'version' => '0.6',
  128. )),
  129. ),
  130. )));
  131. $versionParser = new VersionParser();
  132. $repo->setRootAliases(array(
  133. 'a' => array(
  134. $versionParser->normalize('0.6') => array('alias' => 'dev-feature', 'alias_normalized' => $versionParser->normalize('dev-feature')),
  135. $versionParser->normalize('1.1.x-dev') => array('alias' => '1.0', 'alias_normalized' => $versionParser->normalize('1.0')),
  136. ),
  137. ));
  138. $reflMethod = new \ReflectionMethod($repo, 'whatProvides');
  139. $reflMethod->setAccessible(true);
  140. $packages = $reflMethod->invoke($repo, 'a', array($this, 'isPackageAcceptableReturnTrue'));
  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. public function isPackageAcceptableReturnTrue()
  148. {
  149. return true;
  150. }
  151. public function testSearchWithType()
  152. {
  153. $repoConfig = array(
  154. 'url' => 'http://example.org',
  155. );
  156. $result = array(
  157. 'results' => array(
  158. array(
  159. 'name' => 'foo',
  160. 'description' => null,
  161. ),
  162. ),
  163. );
  164. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  168. ->disableOriginalConstructor()
  169. ->getMock();
  170. $httpDownloader->expects($this->at(0))
  171. ->method('get')
  172. ->with($url = 'http://example.org/packages.json')
  173. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode(array('search' => '/search.json?q=%query%&type=%type%'))));
  174. $httpDownloader->expects($this->at(1))
  175. ->method('get')
  176. ->with($url = 'http://example.org/search.json?q=foo&type=composer-plugin')
  177. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode($result)));
  178. $httpDownloader->expects($this->at(2))
  179. ->method('get')
  180. ->with($url = 'http://example.org/search.json?q=foo&type=library')
  181. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode(array())));
  182. $repository = new ComposerRepository($repoConfig, new NullIO, FactoryMock::createConfig(), $httpDownloader, $eventDispatcher);
  183. $this->assertSame(
  184. array(array('name' => 'foo', 'description' => null)),
  185. $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'composer-plugin')
  186. );
  187. $this->assertEmpty(
  188. $repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'library')
  189. );
  190. }
  191. }