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\Package\Loader;
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;
27 * Tests of lazy asset package loader.
29 * @author François Pluchino <francois.pluchino@gmail.com>
31 class LazyAssetPackageLoaderTest extends \PHPUnit_Framework_TestCase
34 * @var LazyAssetPackageLoader
36 protected $lazyLoader;
39 * @var LazyPackageInterface|\PHPUnit_Framework_MockObject_MockObject
41 protected $lazyPackage;
44 * @var AssetTypeInterface|\PHPUnit_Framework_MockObject_MockObject
49 * @var LoaderInterface|\PHPUnit_Framework_MockObject_MockObject
54 * @var VcsDriverInterface|\PHPUnit_Framework_MockObject_MockObject
64 * @var AssetRepositoryManager|\PHPUnit_Framework_MockObject_MockObject
66 protected $assetRepositoryManager;
68 protected function setUp()
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();
77 $this->assetRepositoryManager->expects($this->any())
78 ->method('solveResolutions')
79 ->willReturnCallback(function ($value) {
84 ->expects($this->any())
86 ->will($this->returnValue('PACKAGE_NAME'));
88 ->expects($this->any())
89 ->method('getUniqueName')
90 ->will($this->returnValue('PACKAGE_NAME-1.0.0.0'));
92 ->expects($this->any())
93 ->method('getPrettyVersion')
94 ->will($this->returnValue('1.0'));
96 ->expects($this->any())
97 ->method('getVersion')
98 ->will($this->returnValue('1.0.0.0'));
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) {
109 $packageConverter = $this->getMockBuilder(PackageConverterInterface::class)->getMock();
110 /* @var LazyPackageInterface $lasyPackage */
111 $lasyPackage = $this->lazyPackage;
112 $packageConverter->expects($this->any())
114 ->will($this->returnCallback(function ($value) use ($lasyPackage) {
115 $value['version'] = $lasyPackage->getPrettyVersion();
116 $value['version_normalized'] = $lasyPackage->getVersion();
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));
137 ->expects($this->any())
139 ->will($this->returnCallback(function ($value) {
142 'url' => 'http://foobar.tld/dist/'.$value,
146 ->expects($this->any())
147 ->method('getSource')
148 ->will($this->returnCallback(function ($value) {
151 'url' => 'http://foobar.tld/source/'.$value,
156 protected function tearDown()
158 $this->lazyPackage = null;
159 $this->assetType = null;
160 $this->loader = null;
161 $this->driver = null;
163 $this->assetRepositoryManager = null;
164 $this->lazyLoader = null;
168 * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
169 * @expectedExceptionMessage The "assetType" property must be defined
171 public function testMissingAssetType()
173 $loader = $this->createLazyLoader('TYPE');
174 $loader->load($this->lazyPackage);
178 * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
179 * @expectedExceptionMessage The "loader" property must be defined
181 public function testMissingLoader()
183 /* @var AssetTypeInterface $assetType */
184 $assetType = $this->assetType;
185 $loader = $this->createLazyLoader('TYPE');
186 $loader->setAssetType($assetType);
187 $loader->load($this->lazyPackage);
191 * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
192 * @expectedExceptionMessage The "driver" property must be defined
194 public function testMissingDriver()
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);
209 * @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
210 * @expectedExceptionMessage The "io" property must be defined
212 public function testMissingIo()
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);
227 public function getConfigIo()
238 * @dataProvider getConfigIo
240 public function testWithoutJsonFile($verbose)
242 /* @var \PHPUnit_Framework_MockObject_MockObject $driver */
243 $driver = $this->driver;
245 ->expects($this->any())
246 ->method('getComposerInformation')
247 ->will($this->returnValue(false));
249 /* @var \PHPUnit_Framework_MockObject_MockObject $loader */
250 $loader = $this->loader;
252 ->expects($this->any())
254 ->will($this->returnValue(false));
256 $this->lazyLoader = $this->createLazyLoaderConfigured('TYPE', $verbose);
257 $package = $this->lazyLoader->load($this->lazyPackage);
259 $this->assertFalse($package);
261 $filename = $this->assetType->getFilename();
262 $validOutput = array('');
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().')',
271 $this->assertSame($validOutput, $this->io->getTraces());
273 $packageCache = $this->lazyLoader->load($this->lazyPackage);
274 $this->assertFalse($packageCache);
275 $this->assertSame($validOutput, $this->io->getTraces());
281 * @dataProvider getConfigIo
283 public function testWithJsonFile($verbose)
285 $arrayPackage = array(
286 'name' => 'PACKAGE_NAME',
290 $realPackage = $this->getMockBuilder(CompletePackageInterface::class)->getMock();
292 ->expects($this->any())
294 ->will($this->returnValue('PACKAGE_NAME'));
296 ->expects($this->any())
297 ->method('getUniqueName')
298 ->will($this->returnValue('PACKAGE_NAME-1.0.0.0'));
300 ->expects($this->any())
301 ->method('getPrettyVersion')
302 ->will($this->returnValue('1.0'));
304 ->expects($this->any())
305 ->method('getVersion')
306 ->will($this->returnValue('1.0.0.0'));
308 /* @var \PHPUnit_Framework_MockObject_MockObject $driver */
309 $driver = $this->driver;
311 ->expects($this->any())
312 ->method('getComposerInformation')
313 ->will($this->returnValue($arrayPackage));
315 /* @var \PHPUnit_Framework_MockObject_MockObject $loader */
316 $loader = $this->loader;
318 ->expects($this->any())
320 ->will($this->returnValue($realPackage));
322 $this->lazyLoader = $this->createLazyLoaderConfigured('TYPE', $verbose);
323 $package = $this->lazyLoader->load($this->lazyPackage);
325 $filename = $this->assetType->getFilename();
326 $validOutput = array('');
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().')',
336 $this->assertInstanceOf('Composer\Package\CompletePackageInterface', $package);
337 $this->assertSame($validOutput, $this->io->getTraces());
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());
345 public function getConfigIoForException()
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>'),
360 * @param string $type
361 * @param bool $verbose
362 * @param string $exceptionClass
363 * @param string $validTrace
365 * @dataProvider getConfigIoForException
367 public function testTagWithTransportException($type, $verbose, $exceptionClass, $validTrace)
369 /* @var \PHPUnit_Framework_MockObject_MockObject $loader */
370 $loader = $this->loader;
372 ->expects($this->any())
374 ->will($this->throwException(new $exceptionClass('MESSAGE')));
376 $this->lazyLoader = $this->createLazyLoaderConfigured($type, $verbose);
377 $package = $this->lazyLoader->load($this->lazyPackage);
379 $this->assertFalse($package);
381 $filename = $this->assetType->getFilename();
382 $validOutput = array('');
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().')',
392 $this->assertSame($validOutput, $this->io->getTraces());
394 $packageCache = $this->lazyLoader->load($this->lazyPackage);
395 $this->assertFalse($packageCache);
396 $this->assertSame($validOutput, $this->io->getTraces());
400 * Creates the lazy asset package loader with full configuration.
402 * @param string $type
403 * @param bool $verbose
405 * @return LazyAssetPackageLoader
407 protected function createLazyLoaderConfigured($type, $verbose = false)
409 $this->io = new MockIO($verbose);
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);
423 * Creates the lazy asset package loader.
425 * @param string $type
427 * @return LazyAssetPackageLoader
429 protected function createLazyLoader($type)
436 return new LazyAssetPackageLoader($type, 'IDENTIFIER', $data);