ComposerRepositoryTest.php 7.5 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\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. $reflMethod = new \ReflectionMethod($repo, 'whatProvides');
  133. $reflMethod->setAccessible(true);
  134. $packages = $reflMethod->invoke($repo, 'a', array($this, 'isPackageAcceptableReturnTrue'));
  135. $this->assertCount(5, $packages);
  136. $this->assertEquals(array('1', '1-alias', '2', '2-alias', '3'), array_keys($packages));
  137. $this->assertSame($packages['2'], $packages['2-alias']->getAliasOf());
  138. }
  139. public function isPackageAcceptableReturnTrue()
  140. {
  141. return true;
  142. }
  143. public function testSearchWithType()
  144. {
  145. $repoConfig = array(
  146. 'url' => 'http://example.org',
  147. );
  148. $result = array(
  149. 'results' => array(
  150. array(
  151. 'name' => 'foo',
  152. 'description' => null,
  153. ),
  154. ),
  155. );
  156. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  157. ->disableOriginalConstructor()
  158. ->getMock();
  159. $eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $httpDownloader->expects($this->at(0))
  163. ->method('get')
  164. ->with($url = 'http://example.org/packages.json')
  165. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode(array('search' => '/search.json?q=%query%&type=%type%'))));
  166. $httpDownloader->expects($this->at(1))
  167. ->method('get')
  168. ->with($url = 'http://example.org/search.json?q=foo&type=composer-plugin')
  169. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode($result)));
  170. $httpDownloader->expects($this->at(2))
  171. ->method('get')
  172. ->with($url = 'http://example.org/search.json?q=foo&type=library')
  173. ->willReturn(new \Composer\Util\Http\Response(array('url' => $url), 200, array(), json_encode(array())));
  174. $repository = new ComposerRepository($repoConfig, new NullIO, FactoryMock::createConfig(), $httpDownloader, $eventDispatcher);
  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. }