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.

428 lines
15KB

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