4 * This file is part of the Fxp Composer Asset Plugin package.
6 * (c) François Pluchino <francois.pluchino@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Fxp\Composer\AssetPlugin\Tests\Repository;
15 use Composer\DependencyResolver\Pool;
16 use Composer\Downloader\TransportException;
17 use Composer\EventDispatcher\EventDispatcher;
18 use Composer\IO\IOInterface;
19 use Composer\Repository\RepositoryManager;
20 use Fxp\Composer\AssetPlugin\Config\Config as AssetConfig;
21 use Fxp\Composer\AssetPlugin\Repository\AbstractAssetsRepository;
22 use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
23 use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository;
24 use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter;
27 * Abstract class for Tests of assets repository.
29 * @author François Pluchino <francois.pluchino@gmail.com>
31 abstract class AbstractAssetsRepositoryTest extends \PHPUnit_Framework_TestCase
34 * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
44 * @var RepositoryManager
49 * @var AssetRepositoryManager
51 protected $assetRepositoryManager;
54 * @var AbstractAssetsRepository
63 protected function setUp()
65 $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
66 $io->expects($this->any())
68 ->will($this->returnValue(true));
69 /* @var IOInterface $io */
70 $config = new Config();
73 'home' => sys_get_temp_dir().'/composer-test',
74 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache-repo',
77 /* @var VcsPackageFilter $filter */
78 $filter = $this->getMockBuilder(VcsPackageFilter::class)->disableOriginalConstructor()->getMock();
79 $rm = new RepositoryManager($io, $config);
80 $rm->setRepositoryClass($this->getType().'-vcs', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\MockAssetRepository');
81 $this->assetRepositoryManager = new AssetRepositoryManager($io, $rm, new AssetConfig(array()), $filter);
82 $repoConfig = array_merge(array(
83 'asset-repository-manager' => $this->assetRepositoryManager,
84 'asset-options' => array(
87 ), $this->getCustomRepoConfig());
90 $this->config = $config;
92 $this->registry = $this->getRegistry($repoConfig, $io, $config);
93 $this->pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->getMock();
96 protected function tearDown()
101 $this->registry = null;
105 protected function getCustomRepoConfig()
111 * Gets the asset type.
115 abstract protected function getType();
118 * Gets the asset registry.
120 * @param array $repoConfig
121 * @param IOInterface $io
122 * @param Config $config
123 * @param EventDispatcher $eventDispatcher
125 * @return AbstractAssetsRepository
127 abstract protected function getRegistry(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null);
130 * Gets the mock package of asset for the config of VCS repository.
134 abstract protected function getMockPackageForVcsConfig();
137 * Gets the mock search result.
139 * @param string $name
143 abstract protected function getMockSearchResult($name = 'mock-package');
146 * Replaces the Remote file system of Registry by a mock.
148 * @return \PHPUnit_Framework_MockObject_MockObject
150 protected function replaceRegistryRfsByMock()
152 $ref = new \ReflectionClass($this->registry);
153 $pRef = $ref->getParentClass()->getParentClass();
154 $pRfs = $pRef->getProperty('rfs');
155 $pRfs->setAccessible(true);
157 $rfs = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
158 ->setConstructorArgs(array($this->io, $this->config))
161 $pRfs->setValue($this->registry, $rfs);
166 public function testFindPackageMustBeAlwaysNull()
168 $this->assertNull($this->registry->findPackage('foobar', '0'));
171 public function testFindPackageMustBeAlwaysEmpty()
173 $this->assertCount(0, $this->registry->findPackages('foobar', '0'));
177 * @expectedException \LogicException
179 public function testGetPackagesNotBeUsed()
181 $this->registry->getPackages();
184 public function testGetProviderNamesMustBeEmpty()
186 $this->assertCount(0, $this->registry->getProviderNames());
189 public function testGetMinimalPackagesMustBeAlwaysEmpty()
191 $this->assertCount(0, $this->registry->getMinimalPackages());
194 public function testWhatProvidesWithNotAssetName()
196 $this->assertCount(0, $this->registry->whatProvides($this->pool, 'foo/bar'));
199 public function testWhatProvidesWithNonExistentPackage()
201 $name = $this->getType().'-asset/non-existent';
202 $rfs = $this->replaceRegistryRfsByMock();
203 $rfs->expects($this->any())
204 ->method('getContents')
205 ->will($this->throwException(new TransportException('Package not found')));
207 $this->assertCount(0, $this->rm->getRepositories());
208 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
209 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
210 $this->assertCount(0, $this->rm->getRepositories());
213 public function testWhatProvidesWithExistingPackage()
215 $name = $this->getType().'-asset/existing';
216 $rfs = $this->replaceRegistryRfsByMock();
217 $rfs->expects($this->any())
218 ->method('getContents')
219 ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
221 $this->assertCount(0, $this->rm->getRepositories());
222 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
223 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
224 $this->assertCount(1, $this->rm->getRepositories());
227 public function testWhatProvidesWithExistingAliasPackage()
229 $name = $this->getType().'-asset/existing-1.0';
230 $rfs = $this->replaceRegistryRfsByMock();
231 $rfs->expects($this->any())
232 ->method('getContents')
233 ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
235 $this->assertCount(0, $this->rm->getRepositories());
236 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
237 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
238 $this->assertCount(1, $this->rm->getRepositories());
241 public function testWhatProvidesWithCamelcasePackageName()
243 $assetName = 'CamelCasePackage';
244 $name = $this->getType().'-asset/'.strtolower($assetName);
245 $rfs = $this->replaceRegistryRfsByMock();
246 $rfs->expects($this->at(0))
247 ->method('getContents')
248 ->will($this->throwException(new TransportException('Package not found', 404)));
249 $rfs->expects($this->at(1))
250 ->method('getContents')
251 ->will($this->throwException(new TransportException('Package not found', 404)));
252 $rfs->expects($this->at(2))
253 ->method('getContents')
254 ->will($this->throwException(new TransportException('Package not found', 404)));
255 $rfs->expects($this->at(3))
256 ->method('getContents')
257 ->will($this->returnValue(json_encode($this->getMockSearchResult($assetName))));
258 $rfs->expects($this->at(4))
259 ->method('getContents')
260 ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
262 $this->assertCount(0, $this->rm->getRepositories());
263 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
264 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
265 $this->assertCount(1, $this->rm->getRepositories());
268 public function testSearch()
270 $rfs = $this->replaceRegistryRfsByMock();
271 $rfs->expects($this->any())
272 ->method('getContents')
273 ->will($this->returnValue(json_encode($this->getMockSearchResult())));
275 $result = $this->registry->search('query');
276 $this->assertCount(count($this->getMockSearchResult()), $result);
279 public function testSearchWithAssetComposerPrefix()
281 $rfs = $this->replaceRegistryRfsByMock();
282 $rfs->expects($this->any())
283 ->method('getContents')
284 ->will($this->returnValue(json_encode($this->getMockSearchResult())));
286 $result = $this->registry->search($this->getType().'-asset/query');
287 $this->assertCount(count($this->getMockSearchResult()), $result);
290 public function testSearchWithSearchDisabled()
293 'asset-repository-manager' => $this->assetRepositoryManager,
294 'asset-options' => array(
295 'searchable' => false,
298 $this->registry = $this->getRegistry($repoConfig, $this->io, $this->config);
300 $this->assertCount(0, $this->registry->search('query'));
303 public function testOverridingVcsRepositoryConfig()
305 $name = $this->getType().'-asset/foobar';
306 $rfs = $this->replaceRegistryRfsByMock();
307 $rfs->expects($this->any())
308 ->method('getContents')
309 ->will($this->returnValue(json_encode($this->getMockPackageForVcsConfig())));
311 $repo = $this->getMockBuilder('Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository')
312 ->disableOriginalConstructor()
315 $repo->expects($this->any())
316 ->method('getComposerPackageName')
317 ->will($this->returnValue($name));
319 /* @var AssetVcsRepository $repo */
320 $this->rm->addRepository($repo);
322 $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));