選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

500 行
16KB

  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\Installer;
  11. use Composer\Downloader\DownloadManager;
  12. use Composer\IO\IOInterface;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\Package;
  15. use Composer\Package\RootPackageInterface;
  16. use Composer\Repository\InstalledRepositoryInterface;
  17. use Composer\Util\Filesystem;
  18. use Composer\TestCase;
  19. use Composer\Composer;
  20. use Composer\Config;
  21. use Fxp\Composer\AssetPlugin\Installer\BowerInstaller;
  22. use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
  23. use Fxp\Composer\AssetPlugin\Util\AssetPlugin;
  24. /**
  25. * Tests of bower asset installer.
  26. *
  27. * @author François Pluchino <francois.pluchino@gmail.com>
  28. */
  29. class BowerInstallerTest extends TestCase
  30. {
  31. /**
  32. * @var Composer
  33. */
  34. protected $composer;
  35. /**
  36. * @var Config
  37. */
  38. protected $config;
  39. /**
  40. * @var string
  41. */
  42. protected $vendorDir;
  43. /**
  44. * @var string
  45. */
  46. protected $binDir;
  47. /**
  48. * @var DownloadManager|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $dm;
  51. /**
  52. * @var InstalledRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $repository;
  55. /**
  56. * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $io;
  59. /**
  60. * @var Filesystem
  61. */
  62. protected $fs;
  63. /**
  64. * @var AssetTypeInterface|\PHPUnit_Framework_MockObject_MockObject
  65. */
  66. protected $type;
  67. protected function setUp()
  68. {
  69. $this->fs = new Filesystem();
  70. $this->composer = new Composer();
  71. $this->config = new Config();
  72. $this->composer->setConfig($this->config);
  73. $this->vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-vendor';
  74. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  75. $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin';
  76. $this->ensureDirectoryExistsAndClear($this->binDir);
  77. $this->config->merge(array(
  78. 'config' => array(
  79. 'vendor-dir' => $this->vendorDir,
  80. 'bin-dir' => $this->binDir,
  81. ),
  82. ));
  83. $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. /* @var DownloadManager $dm */
  87. $dm = $this->dm;
  88. $this->composer->setDownloadManager($dm);
  89. $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
  90. $this->io = $this->getMock('Composer\IO\IOInterface');
  91. $this->type = $this->getMock('Fxp\Composer\AssetPlugin\Type\AssetTypeInterface');
  92. $this->type->expects($this->any())
  93. ->method('getName')
  94. ->will($this->returnValue('foo'));
  95. $this->type->expects($this->any())
  96. ->method('getComposerVendorName')
  97. ->will($this->returnValue('foo-asset'));
  98. $this->type->expects($this->any())
  99. ->method('getComposerType')
  100. ->will($this->returnValue('foo-asset-library'));
  101. $this->type->expects($this->any())
  102. ->method('getFilename')
  103. ->will($this->returnValue('foo.json'));
  104. $this->type->expects($this->any())
  105. ->method('getVersionConverter')
  106. ->will($this->returnValue($this->getMock('Fxp\Composer\AssetPlugin\Converter\VersionConverterInterface')));
  107. $this->type->expects($this->any())
  108. ->method('getPackageConverter')
  109. ->will($this->returnValue($this->getMock('Fxp\Composer\AssetPlugin\Converter\PackageConverterInterface')));
  110. }
  111. protected function tearDown()
  112. {
  113. $this->fs->removeDirectory($this->vendorDir);
  114. $this->fs->removeDirectory($this->binDir);
  115. }
  116. public function testInstallerCreationShouldNotCreateVendorDirectory()
  117. {
  118. /* @var RootPackageInterface $rootPackage */
  119. $rootPackage = $this->createRootPackageMock();
  120. /* @var IOInterface $io */
  121. $io = $this->io;
  122. /* @var AssetTypeInterface $type */
  123. $type = $this->type;
  124. $this->fs->removeDirectory($this->vendorDir);
  125. $this->composer->setPackage($rootPackage);
  126. new BowerInstaller($io, $this->composer, $type);
  127. $this->assertFileNotExists($this->vendorDir);
  128. }
  129. public function testInstallerCreationShouldNotCreateBinDirectory()
  130. {
  131. /* @var RootPackageInterface $rootPackage */
  132. $rootPackage = $this->createRootPackageMock();
  133. /* @var IOInterface $io */
  134. $io = $this->io;
  135. /* @var AssetTypeInterface $type */
  136. $type = $this->type;
  137. $this->fs->removeDirectory($this->binDir);
  138. $this->composer->setPackage($rootPackage);
  139. new BowerInstaller($io, $this->composer, $type);
  140. $this->assertFileNotExists($this->binDir);
  141. }
  142. public function testIsInstalled()
  143. {
  144. /* @var RootPackageInterface $rootPackage */
  145. $rootPackage = $this->createRootPackageMock();
  146. /* @var IOInterface $io */
  147. $io = $this->io;
  148. /* @var AssetTypeInterface $type */
  149. $type = $this->type;
  150. $this->composer->setPackage($rootPackage);
  151. $library = new BowerInstaller($io, $this->composer, $type);
  152. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  153. $package = $this->createPackageMock();
  154. $package
  155. ->expects($this->any())
  156. ->method('getPrettyName')
  157. ->will($this->returnValue('foo-asset/package'));
  158. /* @var PackageInterface $package */
  159. $packageDir = $this->vendorDir.'/'.$package->getPrettyName();
  160. mkdir($packageDir, 0777, true);
  161. /* @var \PHPUnit_Framework_MockObject_MockObject $repository */
  162. $repository = $this->repository;
  163. $repository
  164. ->expects($this->exactly(2))
  165. ->method('hasPackage')
  166. ->with($package)
  167. ->will($this->onConsecutiveCalls(true, false));
  168. /* @var InstalledRepositoryInterface $repository */
  169. $this->assertTrue($library->isInstalled($repository, $package));
  170. $this->assertFalse($library->isInstalled($repository, $package));
  171. $this->ensureDirectoryExistsAndClear($packageDir);
  172. }
  173. public function getAssetIgnoreFiles()
  174. {
  175. return array(
  176. array(array()),
  177. array(array('foo', 'bar')),
  178. );
  179. }
  180. public function getAssetMainFiles()
  181. {
  182. return array(
  183. array(array()),
  184. array(array(
  185. 'asset-main-files' => array(
  186. 'foo-asset/bar' => array(
  187. 'foo',
  188. 'bar',
  189. ),
  190. ),
  191. )),
  192. );
  193. }
  194. /**
  195. * @dataProvider getAssetIgnoreFiles
  196. */
  197. public function testInstall(array $ignoreFiles)
  198. {
  199. /* @var RootPackageInterface $rootPackage */
  200. $rootPackage = $this->createRootPackageMock();
  201. /* @var IOInterface $io */
  202. $io = $this->io;
  203. /* @var AssetTypeInterface $type */
  204. $type = $this->type;
  205. $this->composer->setPackage($rootPackage);
  206. $library = new BowerInstaller($io, $this->composer, $type);
  207. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  208. $package = $this->createPackageMock($ignoreFiles);
  209. $package
  210. ->expects($this->any())
  211. ->method('getPrettyName')
  212. ->will($this->returnValue('foo-asset/package'));
  213. /* @var PackageInterface $package */
  214. $packageDir = $this->vendorDir.'/'.$package->getPrettyName();
  215. mkdir($packageDir, 0777, true);
  216. /* @var \PHPUnit_Framework_MockObject_MockObject $dm */
  217. $dm = $this->dm;
  218. $dm
  219. ->expects($this->once())
  220. ->method('download')
  221. ->with($package, $this->vendorDir.DIRECTORY_SEPARATOR.'foo-asset/package');
  222. /* @var \PHPUnit_Framework_MockObject_MockObject $repository */
  223. $repository = $this->repository;
  224. $repository
  225. ->expects($this->once())
  226. ->method('addPackage')
  227. ->with($package);
  228. /* @var InstalledRepositoryInterface $repository */
  229. $library->install($repository, $package);
  230. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  231. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  232. $this->ensureDirectoryExistsAndClear($packageDir);
  233. }
  234. /**
  235. * @dataProvider getAssetIgnoreFiles
  236. */
  237. public function testUpdate(array $ignoreFiles)
  238. {
  239. /* @var RootPackageInterface $rootPackage */
  240. $rootPackage = $this->createRootPackageMock();
  241. /* @var IOInterface $io */
  242. $io = $this->io;
  243. /* @var AssetTypeInterface $type */
  244. $type = $this->type;
  245. $this->composer->setPackage($rootPackage);
  246. $library = new BowerInstaller($io, $this->composer, $type);
  247. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  248. $package = $this->createPackageMock($ignoreFiles);
  249. $package
  250. ->expects($this->any())
  251. ->method('getPrettyName')
  252. ->will($this->returnValue('foo-asset/package'));
  253. /* @var PackageInterface $package */
  254. $packageDir = $this->vendorDir.'/'.$package->getPrettyName();
  255. mkdir($packageDir, 0777, true);
  256. /* @var \PHPUnit_Framework_MockObject_MockObject $repository */
  257. $repository = $this->repository;
  258. $repository
  259. ->expects($this->exactly(2))
  260. ->method('hasPackage')
  261. ->with($package)
  262. ->will($this->returnValue(true));
  263. /* @var InstalledRepositoryInterface $repository */
  264. $library->update($repository, $package, $package);
  265. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  266. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  267. $this->ensureDirectoryExistsAndClear($packageDir);
  268. }
  269. public function testUninstall()
  270. {
  271. /* @var RootPackageInterface $rootPackage */
  272. $rootPackage = $this->createRootPackageMock();
  273. /* @var IOInterface $io */
  274. $io = $this->io;
  275. /* @var AssetTypeInterface $type */
  276. $type = $this->type;
  277. $this->composer->setPackage($rootPackage);
  278. $library = new BowerInstaller($io, $this->composer, $type);
  279. $package = $this->createPackageMock();
  280. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  281. $package
  282. ->expects($this->any())
  283. ->method('getPrettyName')
  284. ->will($this->returnValue('foo-asset/pkg'));
  285. /* @var \PHPUnit_Framework_MockObject_MockObject $repository */
  286. $repository = $this->repository;
  287. $repository
  288. ->expects($this->exactly(2))
  289. ->method('hasPackage')
  290. ->with($package)
  291. ->will($this->onConsecutiveCalls(true, false));
  292. $repository
  293. ->expects($this->once())
  294. ->method('removePackage')
  295. ->with($package);
  296. /* @var \PHPUnit_Framework_MockObject_MockObject $dm */
  297. $dm = $this->dm;
  298. $dm
  299. ->expects($this->once())
  300. ->method('remove')
  301. ->with($package, $this->vendorDir.DIRECTORY_SEPARATOR.'foo-asset/pkg');
  302. /* @var InstalledRepositoryInterface $repository */
  303. /* @var PackageInterface $package */
  304. $library->uninstall($repository, $package);
  305. $this->setExpectedException('InvalidArgumentException');
  306. $library->uninstall($repository, $package);
  307. }
  308. public function testGetInstallPath()
  309. {
  310. /* @var RootPackageInterface $rootPackage */
  311. $rootPackage = $this->createRootPackageMock();
  312. /* @var IOInterface $io */
  313. $io = $this->io;
  314. /* @var AssetTypeInterface $type */
  315. $type = $this->type;
  316. $this->composer->setPackage($rootPackage);
  317. $library = new BowerInstaller($io, $this->composer, $type);
  318. $package = $this->createPackageMock();
  319. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  320. $package
  321. ->expects($this->once())
  322. ->method('getTargetDir')
  323. ->will($this->returnValue(null));
  324. $package
  325. ->expects($this->any())
  326. ->method('getName')
  327. ->will($this->returnValue('foo-asset/bar'));
  328. $package
  329. ->expects($this->any())
  330. ->method('getPrettyName')
  331. ->will($this->returnValue('foo-asset/bar'));
  332. /* @var PackageInterface $package */
  333. $exceptDir = $this->vendorDir.'/'.$package->getName();
  334. $exceptDir = str_replace('\\', '/', $exceptDir);
  335. $packageDir = $library->getInstallPath($package);
  336. $packageDir = str_replace('\\', '/', $packageDir);
  337. $this->assertEquals($exceptDir, $packageDir);
  338. }
  339. public function testGetInstallPathWithTargetDir()
  340. {
  341. /* @var RootPackageInterface $rootPackage */
  342. $rootPackage = $this->createRootPackageMock();
  343. /* @var IOInterface $io */
  344. $io = $this->io;
  345. /* @var AssetTypeInterface $type */
  346. $type = $this->type;
  347. $this->composer->setPackage($rootPackage);
  348. $library = new BowerInstaller($io, $this->composer, $type);
  349. $package = $this->createPackageMock();
  350. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  351. $package
  352. ->expects($this->once())
  353. ->method('getTargetDir')
  354. ->will($this->returnValue('Some/Namespace'));
  355. $package
  356. ->expects($this->any())
  357. ->method('getPrettyName')
  358. ->will($this->returnValue('foo-asset/bar'));
  359. /* @var PackageInterface $package */
  360. $exceptDir = $this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace';
  361. $exceptDir = str_replace('\\', '/', $exceptDir);
  362. $packageDir = $library->getInstallPath($package);
  363. $packageDir = str_replace('\\', '/', $packageDir);
  364. $this->assertEquals($exceptDir, $packageDir);
  365. }
  366. /**
  367. * @dataProvider getAssetMainFiles
  368. */
  369. public function testMainFiles(array $mainFiles)
  370. {
  371. /* @var RootPackageInterface $rootPackage */
  372. $rootPackage = $this->createRootPackageMock($mainFiles);
  373. $this->composer->setPackage($rootPackage);
  374. $package = new Package('foo-asset/bar', '1.0.0', '1.0.0');
  375. $package = AssetPlugin::addMainFiles($this->composer, $package);
  376. $extra = $package->getExtra();
  377. if (isset($mainFiles['asset-main-files'])) {
  378. $this->assertEquals($extra['bower-asset-main'], $mainFiles['asset-main-files']['foo-asset/bar']);
  379. } else {
  380. $this->assertEquals($extra, array());
  381. }
  382. }
  383. /**
  384. * @param array $ignoreFiles
  385. *
  386. * @return PackageInterface|\PHPUnit_Framework_MockObject_MockObject
  387. */
  388. protected function createPackageMock(array $ignoreFiles = array())
  389. {
  390. $package = $this->getMockBuilder('Composer\Package\Package')
  391. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  392. ->getMock();
  393. $package
  394. ->expects($this->any())
  395. ->method('getExtra')
  396. ->will($this->returnValue(array(
  397. 'bower-asset-ignore' => $ignoreFiles,
  398. )));
  399. return $package;
  400. }
  401. /**
  402. * @param array $mainFiles
  403. *
  404. * @return RootPackageInterface|\PHPUnit_Framework_MockObject_MockObject
  405. */
  406. protected function createRootPackageMock(array $mainFiles = array())
  407. {
  408. $package = $this->getMockBuilder('Composer\Package\RootPackageInterface')
  409. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  410. ->getMock();
  411. $package
  412. ->expects($this->any())
  413. ->method('getExtra')
  414. ->will($this->returnValue($mainFiles));
  415. return $package;
  416. }
  417. }