Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

248 lines
8.3KB

  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\Composer;
  12. use Composer\IO\IOInterface;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\RootPackageInterface;
  15. use Composer\Repository\InstalledRepositoryInterface;
  16. use Composer\Util\Filesystem;
  17. use Fxp\Composer\AssetPlugin\Installer\AssetInstaller;
  18. use Fxp\Composer\AssetPlugin\Type\AssetTypeInterface;
  19. /**
  20. * Tests of asset installer.
  21. *
  22. * @author Martin Hasoň <martin.hason@gmail.com>
  23. * @author François Pluchino <francois.pluchino@gmail.com>
  24. */
  25. class AssetInstallerTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @var Composer|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $composer;
  31. /**
  32. * @var IOInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $io;
  35. /**
  36. * @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $package;
  39. /**
  40. * @var AssetTypeInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $type;
  43. protected function setUp()
  44. {
  45. $this->io = $this->getMock('Composer\IO\IOInterface');
  46. $config = $this->getMock('Composer\Config');
  47. $config->expects($this->any())
  48. ->method('get')
  49. ->will($this->returnCallback(function ($key) {
  50. switch ($key) {
  51. case 'cache-repo-dir':
  52. return sys_get_temp_dir().'/composer-test-repo-cache';
  53. case 'vendor-dir':
  54. return sys_get_temp_dir().'/composer-test/vendor';
  55. }
  56. return;
  57. }));
  58. $this->package = $this->getMock('Composer\Package\PackageInterface');
  59. $this->composer = $this->getMock('Composer\Composer');
  60. $this->composer->expects($this->any())
  61. ->method('getPackage')
  62. ->will($this->returnValue($this->package));
  63. $this->composer->expects($this->any())
  64. ->method('getConfig')
  65. ->will($this->returnValue($config));
  66. $this->type = $this->getMock('Fxp\Composer\AssetPlugin\Type\AssetTypeInterface');
  67. $this->type->expects($this->any())
  68. ->method('getName')
  69. ->will($this->returnValue('foo'));
  70. $this->type->expects($this->any())
  71. ->method('getComposerVendorName')
  72. ->will($this->returnValue('foo-asset'));
  73. $this->type->expects($this->any())
  74. ->method('getComposerType')
  75. ->will($this->returnValue('foo-asset-library'));
  76. $this->type->expects($this->any())
  77. ->method('getFilename')
  78. ->will($this->returnValue('foo.json'));
  79. $this->type->expects($this->any())
  80. ->method('getVersionConverter')
  81. ->will($this->returnValue($this->getMock('Fxp\Composer\AssetPlugin\Converter\VersionConverterInterface')));
  82. $this->type->expects($this->any())
  83. ->method('getPackageConverter')
  84. ->will($this->returnValue($this->getMock('Fxp\Composer\AssetPlugin\Converter\PackageConverterInterface')));
  85. }
  86. protected function tearDown()
  87. {
  88. $this->package = null;
  89. $this->composer = null;
  90. $this->io = null;
  91. $fs = new Filesystem();
  92. $fs->remove(sys_get_temp_dir().'/composer-test-repo-cache');
  93. $fs->remove(sys_get_temp_dir().'/composer-test/vendor');
  94. }
  95. public function testDefaultVendorDir()
  96. {
  97. $installer = $this->createInstaller();
  98. $vendorDir = realpath(sys_get_temp_dir()).'/composer-test/vendor/'.$this->type->getComposerVendorName();
  99. $vendorDir = str_replace('\\', '/', $vendorDir);
  100. $installerPath = $installer->getInstallPath($this->createPackageMock('foo-asset/foo'));
  101. $installerPath = str_replace('\\', '/', $installerPath);
  102. $this->assertEquals($vendorDir.'/foo', $installerPath);
  103. $installerPath2 = $installer->getInstallPath($this->createPackageMock('foo-asset/foo/bar'));
  104. $installerPath2 = str_replace('\\', '/', $installerPath2);
  105. $this->assertEquals($vendorDir.'/foo/bar', $installerPath2);
  106. }
  107. public function testCustomFooDir()
  108. {
  109. $vendorDir = realpath(sys_get_temp_dir()).'/composer-test/web';
  110. $vendorDir = str_replace('\\', '/', $vendorDir);
  111. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  112. $package = $this->package;
  113. $package->expects($this->any())
  114. ->method('getExtra')
  115. ->will($this->returnValue(array(
  116. 'asset-installer-paths' => array(
  117. $this->type->getComposerType() => $vendorDir,
  118. ),
  119. )));
  120. $installer = $this->createInstaller();
  121. $installerPath = $installer->getInstallPath($this->createPackageMock('foo-asset/foo'));
  122. $installerPath = str_replace('\\', '/', $installerPath);
  123. $this->assertEquals($vendorDir.'/foo', $installerPath);
  124. $installerPath2 = $installer->getInstallPath($this->createPackageMock('foo-asset/foo/bar'));
  125. $installerPath2 = str_replace('\\', '/', $installerPath2);
  126. $this->assertEquals($vendorDir.'/foo/bar', $installerPath2);
  127. }
  128. public function testInstall()
  129. {
  130. /* @var RootPackageInterface $rootPackage */
  131. $rootPackage = $this->createRootPackageMock();
  132. /* @var IOInterface $io */
  133. $io = $this->io;
  134. /* @var AssetTypeInterface $type */
  135. $type = $this->type;
  136. $vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test'.DIRECTORY_SEPARATOR.'vendor';
  137. $this->composer->setPackage($rootPackage);
  138. $dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $this->composer->expects($this->any())
  142. ->method('getDownloadManager')
  143. ->will($this->returnValue($dm));
  144. $library = new AssetInstaller($io, $this->composer, $type);
  145. /* @var \PHPUnit_Framework_MockObject_MockObject $package */
  146. $package = $this->createPackageMock('foo-asset/package');
  147. /* @var PackageInterface $package */
  148. $packageDir = $vendorDir.'/'.$package->getPrettyName();
  149. $dm->expects($this->once())
  150. ->method('download')
  151. ->with($package, $vendorDir.DIRECTORY_SEPARATOR.'foo-asset/package');
  152. $repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
  153. $repository->expects($this->once())
  154. ->method('addPackage')
  155. ->with($package);
  156. /* @var InstalledRepositoryInterface $repository */
  157. $library->install($repository, $package);
  158. $this->assertFileExists($vendorDir, 'Vendor dir should be created');
  159. $this->ensureDirectoryExistsAndClear($packageDir);
  160. }
  161. /**
  162. * Creates the asset installer.
  163. *
  164. * @return AssetInstaller
  165. */
  166. protected function createInstaller()
  167. {
  168. /* @var IOInterface $io */
  169. $io = $this->io;
  170. /* @var Composer $composer */
  171. $composer = $this->composer;
  172. /* @var AssetTypeInterface $type */
  173. $type = $this->type;
  174. return new AssetInstaller($io, $composer, $type);
  175. }
  176. /**
  177. * Creates the mock package.
  178. *
  179. * @param string $name
  180. *
  181. * @return PackageInterface
  182. */
  183. private function createPackageMock($name)
  184. {
  185. return $this->getMock('Composer\Package\Package', null, array($name, '1.0.0.0', '1.0.0'));
  186. }
  187. /**
  188. * @return RootPackageInterface|\PHPUnit_Framework_MockObject_MockObject
  189. */
  190. protected function createRootPackageMock()
  191. {
  192. $package = $this->getMockBuilder('Composer\Package\RootPackageInterface')
  193. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  194. ->getMock();
  195. $package->expects($this->any())
  196. ->method('getExtra')
  197. ->will($this->returnValue(array()));
  198. return $package;
  199. }
  200. protected function ensureDirectoryExistsAndClear($directory)
  201. {
  202. $fs = new Filesystem();
  203. if (is_dir($directory)) {
  204. $fs->removeDirectory($directory);
  205. }
  206. mkdir($directory, 0777, true);
  207. }
  208. }