]> git.mxchange.org Git - friendica-addons.git/blob
5415a85072ffd3519923587055519defa630c327
[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\Package\Loader;
13
14 use Composer\Downloader\TransportException;
15 use Composer\Package\CompletePackageInterface;
16 use Composer\Package\Loader\LoaderInterface;
17 use Composer\Repository\Vcs\VcsDriverInterface;
18 use Fxp\Composer\AssetPlugin\Converter\PackageConverterInterface;
19 use Fxp\Composer\AssetPlugin\Converter\VersionConverterInterface;
20 use Fxp\Composer\AssetPlugin\Package\LazyPackageInterface;
21 use Fxp\Composer\AssetPlugin\Package\Loader\LazyAssetPackageLoader;
22 use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager;
23 use Fxp\Composer\AssetPlugin\Tests\Fixtures\IO\MockIO;
24 use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
25
26 /**
27  * Tests of lazy asset package loader.
28  *
29  * @author François Pluchino <francois.pluchino@gmail.com>
30  */
31 class LazyAssetPackageLoaderTest extends \PHPUnit_Framework_TestCase
32 {
33     /**
34      * @var LazyAssetPackageLoader
35      */
36     protected $lazyLoader;
37
38     /**
39      * @var LazyPackageInterface|\PHPUnit_Framework_MockObject_MockObject
40      */
41     protected $lazyPackage;
42
43     /**
44      * @var AssetTypeInterface|\PHPUnit_Framework_MockObject_MockObject
45      */
46     protected $assetType;
47
48     /**
49      * @var LoaderInterface|\PHPUnit_Framework_MockObject_MockObject
50      */
51     protected $loader;
52
53     /**
54      * @var VcsDriverInterface|\PHPUnit_Framework_MockObject_MockObject
55      */
56     protected $driver;
57
58     /**
59      * @var MockIO
60      */
61     protected $io;
62
63     /**
64      * @var AssetRepositoryManager|\PHPUnit_Framework_MockObject_MockObject
65      */
66     protected $assetRepositoryManager;
67
68     protected function setUp()
69     {
70         $this->lazyPackage = $this->getMockBuilder(LazyPackageInterface::class)->getMock();
71         $this->assetType = $this->getMockBuilder(AssetTypeInterface::class)->getMock();
72         $this->loader = $this->getMockBuilder(LoaderInterface::class)->getMock();
73         $this->driver = $this->getMockBuilder(VcsDriverInterface::class)->getMock();
74         $this->assetRepositoryManager = $this->getMockBuilder(AssetRepositoryManager::class)
75             ->disableOriginalConstructor()->getMock();
76
77         $this->assetRepositoryManager->expects($this->any())
78             ->method('solveResolutions')
79             ->willReturnCallback(function ($value) {
80                 return $value;
81             });
82
83         $this->lazyPackage
84             ->expects($this->any())
85             ->method('getName')
86             ->will($this->returnValue('PACKAGE_NAME'));
87         $this->lazyPackage
88             ->expects($this->any())
89             ->method('getUniqueName')
90             ->will($this->returnValue('PACKAGE_NAME-1.0.0.0'));
91         $this->lazyPackage
92             ->expects($this->any())
93             ->method('getPrettyVersion')
94             ->will($this->returnValue('1.0'));
95         $this->lazyPackage
96             ->expects($this->any())
97             ->method('getVersion')
98             ->will($this->returnValue('1.0.0.0'));
99
100         $versionConverter = $this->getMockBuilder(VersionConverterInterface::class)->getMock();
101         $versionConverter->expects($this->any())
102             ->method('convertVersion')
103             ->will($this->returnValue('VERSION_CONVERTED'));
104         $versionConverter->expects($this->any())
105             ->method('convertRange')
106             ->will($this->returnCallback(function ($value) {
107                 return $value;
108             }));
109         $packageConverter = $this->getMockBuilder(PackageConverterInterface::class)->getMock();
110         /* @var LazyPackageInterface $lasyPackage */
111         $lasyPackage = $this->lazyPackage;
112         $packageConverter->expects($this->any())
113             ->method('convert')
114             ->will($this->returnCallback(function ($value) use ($lasyPackage) {
115                 $value['version'] = $lasyPackage->getPrettyVersion();
116                 $value['version_normalized'] = $lasyPackage->getVersion();
117
118                 return $value;
119             }));
120         $this->assetType->expects($this->any())
121             ->method('getComposerVendorName')
122             ->will($this->returnValue('ASSET'));
123         $this->assetType->expects($this->any())
124             ->method('getComposerType')
125             ->will($this->returnValue('ASSET_TYPE'));
126         $this->assetType->expects($this->any())
127             ->method('getFilename')
128             ->will($this->returnValue('ASSET.json'));
129         $this->assetType->expects($this->any())
130             ->method('getVersionConverter')
131             ->will($this->returnValue($versionConverter));
132         $this->assetType->expects($this->any())
133             ->method('getPackageConverter')
134             ->will($this->returnValue($packageConverter));
135
136         $this->driver
137             ->expects($this->any())
138             ->method('getDist')
139             ->will($this->returnCallback(function ($value) {
140                 return array(
141                     'type' => 'vcs',
142                     'url' => 'http://foobar.tld/dist/'.$value,
143                 );
144             }));
145         $this->driver
146             ->expects($this->any())
147             ->method('getSource')
148             ->will($this->returnCallback(function ($value) {
149                 return array(
150                     'type' => 'vcs',
151                     'url' => 'http://foobar.tld/source/'.$value,
152                 );
153             }));
154     }
155
156     protected function tearDown()
157     {
158         $this->lazyPackage = null;
159         $this->assetType = null;
160         $this->loader = null;
161         $this->driver = null;
162         $this->io = null;
163         $this->assetRepositoryManager = null;
164         $this->lazyLoader = null;
165     }
166
167     /**
168      * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
169      * @expectedExceptionMessage The "assetType" property must be defined
170      */
171     public function testMissingAssetType()
172     {
173         $loader = $this->createLazyLoader('TYPE');
174         $loader->load($this->lazyPackage);
175     }
176
177     /**
178      * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
179      * @expectedExceptionMessage The "loader" property must be defined
180      */
181     public function testMissingLoader()
182     {
183         /* @var AssetTypeInterface $assetType */
184         $assetType = $this->assetType;
185         $loader = $this->createLazyLoader('TYPE');
186         $loader->setAssetType($assetType);
187         $loader->load($this->lazyPackage);
188     }
189
190     /**
191      * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
192      * @expectedExceptionMessage The "driver" property must be defined
193      */
194     public function testMissingDriver()
195     {
196         /* @var AssetTypeInterface $assetType */
197         $assetType = $this->assetType;
198         /* @var LoaderInterface $cLoader */
199         $cLoader = $this->loader;
200         /* @var LazyPackageInterface $lazyPackage */
201         $lazyPackage = $this->lazyPackage;
202         $loader = $this->createLazyLoader('TYPE');
203         $loader->setAssetType($assetType);
204         $loader->setLoader($cLoader);
205         $loader->load($lazyPackage);
206     }
207
208     /**
209      * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
210      * @expectedExceptionMessage The "io" property must be defined
211      */
212     public function testMissingIo()
213     {
214         /* @var AssetTypeInterface $assetType */
215         $assetType = $this->assetType;
216         /* @var LoaderInterface $cLoader */
217         $cLoader = $this->loader;
218         /* @var VcsDriverInterface $driver */
219         $driver = $this->driver;
220         $loader = $this->createLazyLoader('TYPE');
221         $loader->setAssetType($assetType);
222         $loader->setLoader($cLoader);
223         $loader->setDriver($driver);
224         $loader->load($this->lazyPackage);
225     }
226
227     public function getConfigIo()
228     {
229         return array(
230             array(false),
231             array(true),
232         );
233     }
234
235     /**
236      * @param $verbose
237      *
238      * @dataProvider getConfigIo
239      */
240     public function testWithoutJsonFile($verbose)
241     {
242         /* @var \PHPUnit_Framework_MockObject_MockObject $driver */
243         $driver = $this->driver;
244         $driver
245             ->expects($this->any())
246             ->method('getComposerInformation')
247             ->will($this->returnValue(false));
248
249         /* @var \PHPUnit_Framework_MockObject_MockObject $loader */
250         $loader = $this->loader;
251         $loader
252             ->expects($this->any())
253             ->method('load')
254             ->will($this->returnValue(false));
255
256         $this->lazyLoader = $this->createLazyLoaderConfigured('TYPE', $verbose);
257         $package = $this->lazyLoader->load($this->lazyPackage);
258
259         $this->assertFalse($package);
260
261         $filename = $this->assetType->getFilename();
262         $validOutput = array('');
263
264         if ($verbose) {
265             $validOutput = array(
266                 'Reading '.$filename.' of <info>'.$this->lazyPackage->getName().'</info> (<comment>'.$this->lazyPackage->getPrettyVersion().'</comment>)',
267                 'Importing empty TYPE '.$this->lazyPackage->getPrettyVersion().' ('.$this->lazyPackage->getVersion().')',
268                 '',
269             );
270         }
271         $this->assertSame($validOutput, $this->io->getTraces());
272
273         $packageCache = $this->lazyLoader->load($this->lazyPackage);
274         $this->assertFalse($packageCache);
275         $this->assertSame($validOutput, $this->io->getTraces());
276     }
277
278     /**
279      * @param $verbose
280      *
281      * @dataProvider getConfigIo
282      */
283     public function testWithJsonFile($verbose)
284     {
285         $arrayPackage = array(
286             'name' => 'PACKAGE_NAME',
287             'version' => '1.0',
288         );
289
290         $realPackage = $this->getMockBuilder(CompletePackageInterface::class)->getMock();
291         $realPackage
292             ->expects($this->any())
293             ->method('getName')
294             ->will($this->returnValue('PACKAGE_NAME'));
295         $realPackage
296             ->expects($this->any())
297             ->method('getUniqueName')
298             ->will($this->returnValue('PACKAGE_NAME-1.0.0.0'));
299         $realPackage
300             ->expects($this->any())
301             ->method('getPrettyVersion')
302             ->will($this->returnValue('1.0'));
303         $realPackage
304             ->expects($this->any())
305             ->method('getVersion')
306             ->will($this->returnValue('1.0.0.0'));
307
308         /* @var \PHPUnit_Framework_MockObject_MockObject $driver */
309         $driver = $this->driver;
310         $driver
311             ->expects($this->any())
312             ->method('getComposerInformation')
313             ->will($this->returnValue($arrayPackage));
314
315         /* @var \PHPUnit_Framework_MockObject_MockObject $loader */
316         $loader = $this->loader;
317         $loader
318             ->expects($this->any())
319             ->method('load')
320             ->will($this->returnValue($realPackage));
321
322         $this->lazyLoader = $this->createLazyLoaderConfigured('TYPE', $verbose);
323         $package = $this->lazyLoader->load($this->lazyPackage);
324
325         $filename = $this->assetType->getFilename();
326         $validOutput = array('');
327
328         if ($verbose) {
329             $validOutput = array(
330                 'Reading '.$filename.' of <info>'.$this->lazyPackage->getName().'</info> (<comment>'.$this->lazyPackage->getPrettyVersion().'</comment>)',
331                 'Importing TYPE'.' '.$this->lazyPackage->getPrettyVersion().' ('.$this->lazyPackage->getVersion().')',
332                 '',
333             );
334         }
335
336         $this->assertInstanceOf('Composer\Package\CompletePackageInterface', $package);
337         $this->assertSame($validOutput, $this->io->getTraces());
338
339         $packageCache = $this->lazyLoader->load($this->lazyPackage);
340         $this->assertInstanceOf('Composer\Package\CompletePackageInterface', $packageCache);
341         $this->assertSame($package, $packageCache);
342         $this->assertSame($validOutput, $this->io->getTraces());
343     }
344
345     public function getConfigIoForException()
346     {
347         return array(
348             array('tag', false, 'Exception', '<warning>Skipped tag 1.0, MESSAGE</warning>'),
349             array('tag', true, 'Exception', '<warning>Skipped tag 1.0, MESSAGE</warning>'),
350             array('branch', false, 'Exception', '<error>Skipped branch 1.0, MESSAGE</error>'),
351             array('branch', true, 'Exception', '<error>Skipped branch 1.0, MESSAGE</error>'),
352             array('tag', false, TransportException::class, '<warning>Skipped tag 1.0, no ASSET.json file was found</warning>'),
353             array('tag', true, TransportException::class, '<warning>Skipped tag 1.0, no ASSET.json file was found</warning>'),
354             array('branch', false, TransportException::class, '<error>Skipped branch 1.0, no ASSET.json file was found</error>'),
355             array('branch', true, TransportException::class, '<error>Skipped branch 1.0, no ASSET.json file was found</error>'),
356         );
357     }
358
359     /**
360      * @param string $type
361      * @param bool   $verbose
362      * @param string $exceptionClass
363      * @param string $validTrace
364      *
365      * @dataProvider getConfigIoForException
366      */
367     public function testTagWithTransportException($type, $verbose, $exceptionClass, $validTrace)
368     {
369         /* @var \PHPUnit_Framework_MockObject_MockObject $loader */
370         $loader = $this->loader;
371         $loader
372             ->expects($this->any())
373             ->method('load')
374             ->will($this->throwException(new $exceptionClass('MESSAGE')));
375
376         $this->lazyLoader = $this->createLazyLoaderConfigured($type, $verbose);
377         $package = $this->lazyLoader->load($this->lazyPackage);
378
379         $this->assertFalse($package);
380
381         $filename = $this->assetType->getFilename();
382         $validOutput = array('');
383
384         if ($verbose) {
385             $validOutput = array(
386                 'Reading '.$filename.' of <info>'.$this->lazyPackage->getName().'</info> (<comment>'.$this->lazyPackage->getPrettyVersion().'</comment>)',
387                 'Importing empty '.$type.' '.$this->lazyPackage->getPrettyVersion().' ('.$this->lazyPackage->getVersion().')',
388                 $validTrace,
389                 '',
390             );
391         }
392         $this->assertSame($validOutput, $this->io->getTraces());
393
394         $packageCache = $this->lazyLoader->load($this->lazyPackage);
395         $this->assertFalse($packageCache);
396         $this->assertSame($validOutput, $this->io->getTraces());
397     }
398
399     /**
400      * Creates the lazy asset package loader with full configuration.
401      *
402      * @param string $type
403      * @param bool   $verbose
404      *
405      * @return LazyAssetPackageLoader
406      */
407     protected function createLazyLoaderConfigured($type, $verbose = false)
408     {
409         $this->io = new MockIO($verbose);
410
411         $cLoader = $this->loader;
412         $loader = $this->createLazyLoader($type);
413         $loader->setAssetType($this->assetType);
414         $loader->setLoader($cLoader);
415         $loader->setDriver($this->driver);
416         $loader->setIO($this->io);
417         $loader->setAssetRepositoryManager($this->assetRepositoryManager);
418
419         return $loader;
420     }
421
422     /**
423      * Creates the lazy asset package loader.
424      *
425      * @param string $type
426      *
427      * @return LazyAssetPackageLoader
428      */
429     protected function createLazyLoader($type)
430     {
431         $data = array(
432             'foo' => 'bar',
433             'bar' => 'foo',
434         );
435
436         return new LazyAssetPackageLoader($type, 'IDENTIFIER', $data);
437     }
438 }