You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.9KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Tests\Repository;
  11. use Composer\DependencyResolver\Pool;
  12. use Composer\Downloader\TransportException;
  13. use Composer\EventDispatcher\EventDispatcher;
  14. use Composer\IO\IOInterface;
  15. use Composer\Config;
  16. use Composer\Repository\RepositoryManager;
  17. use Fxp\Composer\AssetPlugin\Repository\AbstractAssetsRepository;
  18. use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository;
  19. /**
  20. * Abstract class for Tests of assets repository.
  21. *
  22. * @author François Pluchino <francois.pluchino@gmail.com>
  23. */
  24. abstract class AbstractAssetsRepositoryTest extends \PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $io;
  30. /**
  31. * @var Config
  32. */
  33. protected $config;
  34. /**
  35. * @var RepositoryManager
  36. */
  37. protected $rm;
  38. /**
  39. * @var AbstractAssetsRepository
  40. */
  41. protected $registry;
  42. /**
  43. * @var Pool
  44. */
  45. protected $pool;
  46. protected function setUp()
  47. {
  48. $io = $this->getMock('Composer\IO\IOInterface');
  49. $io->expects($this->any())
  50. ->method('isVerbose')
  51. ->will($this->returnValue(true));
  52. /* @var IOInterface $io */
  53. $config = new Config();
  54. $config->merge(array(
  55. 'config' => array(
  56. 'home' => sys_get_temp_dir().'/composer-test',
  57. 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache-repo',
  58. ),
  59. ));
  60. $rm = new RepositoryManager($io, $config);
  61. $rm->setRepositoryClass($this->getType().'-vcs', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\MockAssetRepository');
  62. $repoConfig = array(
  63. 'repository-manager' => $rm,
  64. 'asset-options' => array(
  65. 'searchable' => true,
  66. ),
  67. );
  68. $this->io = $io;
  69. $this->config = $config;
  70. $this->rm = $rm;
  71. $this->registry = $this->getRegistry($repoConfig, $io, $config);
  72. $this->pool = $this->getMock('Composer\DependencyResolver\Pool');
  73. }
  74. protected function tearDown()
  75. {
  76. $this->io = null;
  77. $this->config = null;
  78. $this->rm = null;
  79. $this->registry = null;
  80. $this->pool = null;
  81. }
  82. /**
  83. * Gets the asset type.
  84. *
  85. * @return string
  86. */
  87. abstract protected function getType();
  88. /**
  89. * Gets the asset registry.
  90. *
  91. * @param array $repoConfig
  92. * @param IOInterface $io
  93. * @param Config $config
  94. * @param EventDispatcher $eventDispatcher
  95. *
  96. * @return AbstractAssetsRepository
  97. */
  98. abstract protected function getRegistry(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null);
  99. /**
  100. * Gets the mock package of asset for the config of VCS repository.
  101. *
  102. * @return array
  103. */
  104. abstract protected function getMockPackageForVcsConfig();
  105. /**
  106. * Gets the mock search result.
  107. *
  108. * @param string $name
  109. *
  110. * @return array
  111. */
  112. abstract protected function getMockSearchResult($name = 'mock-package');
  113. /**
  114. * Replaces the Remote file system of Registry by a mock.
  115. *
  116. * @return \PHPUnit_Framework_MockObject_MockObject
  117. */
  118. protected function replaceRegistryRfsByMock()
  119. {
  120. $ref = new \ReflectionClass($this->registry);
  121. $pRef = $ref->getParentClass()->getParentClass();
  122. $pRfs = $pRef->getProperty('rfs');
  123. $pRfs->setAccessible(true);
  124. $rfs = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  125. ->setConstructorArgs(array($this->io, $this->config))
  126. ->getMock();
  127. $pRfs->setValue($this->registry, $rfs);
  128. return $rfs;
  129. }
  130. public function testFindPackageMustBeAlwaysNull()
  131. {
  132. $this->assertNull($this->registry->findPackage('foobar', '0'));
  133. }
  134. public function testFindPackageMustBeAlwaysEmpty()
  135. {
  136. $this->assertCount(0, $this->registry->findPackages('foobar', '0'));
  137. }
  138. public function testGetPackagesNotBeUsed()
  139. {
  140. $this->setExpectedException('LogicException');
  141. $this->registry->getPackages();
  142. }
  143. public function testGetProviderNamesMustBeEmpty()
  144. {
  145. $this->assertCount(0, $this->registry->getProviderNames());
  146. }
  147. public function testGetMinimalPackagesMustBeAlwaysEmpty()
  148. {
  149. $this->assertCount(0, $this->registry->getMinimalPackages());
  150. }
  151. public function testWhatProvidesWithNotAssetName()
  152. {
  153. $this->assertCount(0, $this->registry->whatProvides($this->pool, 'foo/bar'));
  154. }
  155. public function testWhatProvidesWithNonExistentPackage()
  156. {
  157. $name = $this->getType().'-asset/non-existent';
  158. $rfs = $this->replaceRegistryRfsByMock();
  159. $rfs->expects($this->any())
  160. ->method('getContents')
  161. ->will($this->throwException(new TransportException('Package not found')));
  162. $this->assertCount(0, $this->rm->getRepositories());
  163. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  164. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  165. $this->assertCount(0, $this->rm->getRepositories());
  166. }
  167. public function testWhatProvidesWithExistingPackage()
  168. {
  169. $name = $this->getType().'-asset/existing';
  170. $rfs = $this->replaceRegistryRfsByMock();
  171. $rfs->expects($this->any())
  172. ->method('getContents')
  173. ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
  174. $this->assertCount(0, $this->rm->getRepositories());
  175. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  176. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  177. $this->assertCount(1, $this->rm->getRepositories());
  178. }
  179. public function testWhatProvidesWithExistingAliasPackage()
  180. {
  181. $name = $this->getType().'-asset/existing-1.0';
  182. $rfs = $this->replaceRegistryRfsByMock();
  183. $rfs->expects($this->any())
  184. ->method('getContents')
  185. ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
  186. $this->assertCount(0, $this->rm->getRepositories());
  187. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  188. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  189. $this->assertCount(1, $this->rm->getRepositories());
  190. }
  191. public function testWhatProvidesWithCamelcasePackageName()
  192. {
  193. $assetName = 'CamelCasePackage';
  194. $name = $this->getType().'-asset/'.strtolower($assetName);
  195. $rfs = $this->replaceRegistryRfsByMock();
  196. $rfs->expects($this->at(0))
  197. ->method('getContents')
  198. ->will($this->throwException(new TransportException('Package not found', 404)));
  199. $rfs->expects($this->at(1))
  200. ->method('getContents')
  201. ->will($this->throwException(new TransportException('Package not found', 404)));
  202. $rfs->expects($this->at(2))
  203. ->method('getContents')
  204. ->will($this->throwException(new TransportException('Package not found', 404)));
  205. $rfs->expects($this->at(3))
  206. ->method('getContents')
  207. ->will($this->returnValue(json_encode($this->getMockSearchResult($assetName))));
  208. $rfs->expects($this->at(4))
  209. ->method('getContents')
  210. ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
  211. $this->assertCount(0, $this->rm->getRepositories());
  212. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  213. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  214. $this->assertCount(1, $this->rm->getRepositories());
  215. }
  216. public function testSearch()
  217. {
  218. $rfs = $this->replaceRegistryRfsByMock();
  219. $rfs->expects($this->any())
  220. ->method('getContents')
  221. ->will($this->returnValue(json_encode($this->getMockSearchResult())));
  222. $result = $this->registry->search('query');
  223. $this->assertCount(count($this->getMockSearchResult()), $result);
  224. }
  225. public function testSearchWithAssetComposerPrefix()
  226. {
  227. $rfs = $this->replaceRegistryRfsByMock();
  228. $rfs->expects($this->any())
  229. ->method('getContents')
  230. ->will($this->returnValue(json_encode($this->getMockSearchResult())));
  231. $result = $this->registry->search($this->getType().'-asset/query');
  232. $this->assertCount(count($this->getMockSearchResult()), $result);
  233. }
  234. public function testSearchWithSearchDisabled()
  235. {
  236. $repoConfig = array(
  237. 'repository-manager' => $this->rm,
  238. 'asset-options' => array(
  239. 'searchable' => false,
  240. ),
  241. );
  242. $this->registry = $this->getRegistry($repoConfig, $this->io, $this->config);
  243. $this->assertCount(0, $this->registry->search('query'));
  244. }
  245. public function testOverridingVcsRepositoryConfig()
  246. {
  247. $name = $this->getType().'-asset/foobar';
  248. $rfs = $this->replaceRegistryRfsByMock();
  249. $rfs->expects($this->any())
  250. ->method('getContents')
  251. ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
  252. $repo = $this->getMockBuilder('Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository')
  253. ->disableOriginalConstructor()
  254. ->getMock();
  255. $repo->expects($this->any())
  256. ->method('getComposerPackageName')
  257. ->will($this->returnValue($name));
  258. /* @var AssetVcsRepository $repo */
  259. $this->rm->addRepository($repo);
  260. $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
  261. }
  262. }