]> git.mxchange.org Git - friendica-addons.git/blob
cedad9eb01e4118ec2ddb560636c43a308d9bd08
[friendica-addons.git] /
1 <?php
2
3 /*
4  * This file is part of the Fxp Composer Asset Plugin package.
5  *
6  * (c) François Pluchino <francois.pluchino@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Fxp\Composer\AssetPlugin\Tests\Repository;
13
14 use Composer\Config;
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;
25
26 /**
27  * Abstract class for Tests of assets repository.
28  *
29  * @author François Pluchino <francois.pluchino@gmail.com>
30  */
31 abstract class AbstractAssetsRepositoryTest extends \PHPUnit_Framework_TestCase
32 {
33     /**
34      * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
35      */
36     protected $io;
37
38     /**
39      * @var Config
40      */
41     protected $config;
42
43     /**
44      * @var RepositoryManager
45      */
46     protected $rm;
47
48     /**
49      * @var AssetRepositoryManager
50      */
51     protected $assetRepositoryManager;
52
53     /**
54      * @var AbstractAssetsRepository
55      */
56     protected $registry;
57
58     /**
59      * @var Pool
60      */
61     protected $pool;
62
63     protected function setUp()
64     {
65         $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
66         $io->expects($this->any())
67             ->method('isVerbose')
68             ->will($this->returnValue(true));
69         /* @var IOInterface $io */
70         $config = new Config();
71         $config->merge(array(
72             'config' => array(
73                 'home' => sys_get_temp_dir().'/composer-test',
74                 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache-repo',
75             ),
76         ));
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(
85                 'searchable' => true,
86             ),
87         ), $this->getCustomRepoConfig());
88
89         $this->io = $io;
90         $this->config = $config;
91         $this->rm = $rm;
92         $this->registry = $this->getRegistry($repoConfig, $io, $config);
93         $this->pool = $this->getMockBuilder('Composer\DependencyResolver\Pool')->getMock();
94     }
95
96     protected function tearDown()
97     {
98         $this->io = null;
99         $this->config = null;
100         $this->rm = null;
101         $this->registry = null;
102         $this->pool = null;
103     }
104
105     protected function getCustomRepoConfig()
106     {
107         return array();
108     }
109
110     /**
111      * Gets the asset type.
112      *
113      * @return string
114      */
115     abstract protected function getType();
116
117     /**
118      * Gets the asset registry.
119      *
120      * @param array           $repoConfig
121      * @param IOInterface     $io
122      * @param Config          $config
123      * @param EventDispatcher $eventDispatcher
124      *
125      * @return AbstractAssetsRepository
126      */
127     abstract protected function getRegistry(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null);
128
129     /**
130      * Gets the mock package of asset for the config of VCS repository.
131      *
132      * @return array
133      */
134     abstract protected function getMockPackageForVcsConfig();
135
136     /**
137      * Gets the mock search result.
138      *
139      * @param string $name
140      *
141      * @return array
142      */
143     abstract protected function getMockSearchResult($name = 'mock-package');
144
145     /**
146      * Replaces the Remote file system of Registry by a mock.
147      *
148      * @return \PHPUnit_Framework_MockObject_MockObject
149      */
150     protected function replaceRegistryRfsByMock()
151     {
152         $ref = new \ReflectionClass($this->registry);
153         $pRef = $ref->getParentClass()->getParentClass();
154         $pRfs = $pRef->getProperty('rfs');
155         $pRfs->setAccessible(true);
156
157         $rfs = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
158             ->setConstructorArgs(array($this->io, $this->config))
159             ->getMock();
160
161         $pRfs->setValue($this->registry, $rfs);
162
163         return $rfs;
164     }
165
166     public function testFindPackageMustBeAlwaysNull()
167     {
168         $this->assertNull($this->registry->findPackage('foobar', '0'));
169     }
170
171     public function testFindPackageMustBeAlwaysEmpty()
172     {
173         $this->assertCount(0, $this->registry->findPackages('foobar', '0'));
174     }
175
176     /**
177      * @expectedException \LogicException
178      */
179     public function testGetPackagesNotBeUsed()
180     {
181         $this->registry->getPackages();
182     }
183
184     public function testGetProviderNamesMustBeEmpty()
185     {
186         $this->assertCount(0, $this->registry->getProviderNames());
187     }
188
189     public function testGetMinimalPackagesMustBeAlwaysEmpty()
190     {
191         $this->assertCount(0, $this->registry->getMinimalPackages());
192     }
193
194     public function testWhatProvidesWithNotAssetName()
195     {
196         $this->assertCount(0, $this->registry->whatProvides($this->pool, 'foo/bar'));
197     }
198
199     public function testWhatProvidesWithNonExistentPackage()
200     {
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')));
206
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());
211     }
212
213     public function testWhatProvidesWithExistingPackage()
214     {
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())));
220
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());
225     }
226
227     public function testWhatProvidesWithExistingAliasPackage()
228     {
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())));
234
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());
239     }
240
241     public function testWhatProvidesWithCamelcasePackageName()
242     {
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())));
261
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());
266     }
267
268     public function testSearch()
269     {
270         $rfs = $this->replaceRegistryRfsByMock();
271         $rfs->expects($this->any())
272             ->method('getContents')
273             ->will($this->returnValue(json_encode($this->getMockSearchResult())));
274
275         $result = $this->registry->search('query');
276         $this->assertCount(count($this->getMockSearchResult()), $result);
277     }
278
279     public function testSearchWithAssetComposerPrefix()
280     {
281         $rfs = $this->replaceRegistryRfsByMock();
282         $rfs->expects($this->any())
283             ->method('getContents')
284             ->will($this->returnValue(json_encode($this->getMockSearchResult())));
285
286         $result = $this->registry->search($this->getType().'-asset/query');
287         $this->assertCount(count($this->getMockSearchResult()), $result);
288     }
289
290     public function testSearchWithSearchDisabled()
291     {
292         $repoConfig = array(
293             'asset-repository-manager' => $this->assetRepositoryManager,
294             'asset-options' => array(
295                 'searchable' => false,
296             ),
297         );
298         $this->registry = $this->getRegistry($repoConfig, $this->io, $this->config);
299
300         $this->assertCount(0, $this->registry->search('query'));
301     }
302
303     public function testOverridingVcsRepositoryConfig()
304     {
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())));
310
311         $repo = $this->getMockBuilder('Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository')
312             ->disableOriginalConstructor()
313             ->getMock();
314
315         $repo->expects($this->any())
316             ->method('getComposerPackageName')
317             ->will($this->returnValue($name));
318
319         /* @var AssetVcsRepository $repo */
320         $this->rm->addRepository($repo);
321
322         $this->assertCount(0, $this->registry->whatProvides($this->pool, $name));
323     }
324 }