PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Composer/Test/Installer/LibraryInstallerTest.php

http://github.com/composer/composer
PHP | 290 lines | 212 code | 57 blank | 21 comment | 0 complexity | 7cdbd57f6ca357f4c9e78c33af99df4f MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Installer;
  12. use Composer\Installer\LibraryInstaller;
  13. use Composer\Util\Filesystem;
  14. use Composer\Test\TestCase;
  15. use Composer\Composer;
  16. use Composer\Config;
  17. class LibraryInstallerTest extends TestCase
  18. {
  19. protected $composer;
  20. protected $config;
  21. protected $rootDir;
  22. protected $vendorDir;
  23. protected $binDir;
  24. protected $dm;
  25. protected $repository;
  26. protected $io;
  27. protected $fs;
  28. protected function setUp()
  29. {
  30. $this->fs = new Filesystem;
  31. $this->composer = new Composer();
  32. $this->config = new Config(false);
  33. $this->composer->setConfig($this->config);
  34. $this->rootDir = $this->getUniqueTmpDirectory();
  35. $this->vendorDir = $this->rootDir.DIRECTORY_SEPARATOR.'vendor';
  36. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  37. $this->binDir = $this->rootDir.DIRECTORY_SEPARATOR.'bin';
  38. $this->ensureDirectoryExistsAndClear($this->binDir);
  39. $this->config->merge(array(
  40. 'config' => array(
  41. 'vendor-dir' => $this->vendorDir,
  42. 'bin-dir' => $this->binDir,
  43. ),
  44. ));
  45. $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->composer->setDownloadManager($this->dm);
  49. $this->repository = $this->getMockBuilder('Composer\Repository\InstalledRepositoryInterface')->getMock();
  50. $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  51. }
  52. protected function tearDown()
  53. {
  54. $this->fs->removeDirectory($this->rootDir);
  55. }
  56. public function testInstallerCreationShouldNotCreateVendorDirectory()
  57. {
  58. $this->fs->removeDirectory($this->vendorDir);
  59. new LibraryInstaller($this->io, $this->composer);
  60. $this->assertFileNotExists($this->vendorDir);
  61. }
  62. public function testInstallerCreationShouldNotCreateBinDirectory()
  63. {
  64. $this->fs->removeDirectory($this->binDir);
  65. new LibraryInstaller($this->io, $this->composer);
  66. $this->assertFileNotExists($this->binDir);
  67. }
  68. public function testIsInstalled()
  69. {
  70. $library = new LibraryInstaller($this->io, $this->composer);
  71. $package = $this->createPackageMock();
  72. $this->repository
  73. ->expects($this->exactly(2))
  74. ->method('hasPackage')
  75. ->with($package)
  76. ->will($this->onConsecutiveCalls(true, false));
  77. $this->assertTrue($library->isInstalled($this->repository, $package));
  78. $this->assertFalse($library->isInstalled($this->repository, $package));
  79. }
  80. /**
  81. * @depends testInstallerCreationShouldNotCreateVendorDirectory
  82. * @depends testInstallerCreationShouldNotCreateBinDirectory
  83. */
  84. public function testInstall()
  85. {
  86. $library = new LibraryInstaller($this->io, $this->composer);
  87. $package = $this->createPackageMock();
  88. $package
  89. ->expects($this->any())
  90. ->method('getPrettyName')
  91. ->will($this->returnValue('some/package'));
  92. $this->dm
  93. ->expects($this->once())
  94. ->method('install')
  95. ->with($package, $this->vendorDir.'/some/package');
  96. $this->repository
  97. ->expects($this->once())
  98. ->method('addPackage')
  99. ->with($package);
  100. $library->install($this->repository, $package);
  101. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  102. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  103. }
  104. /**
  105. * @depends testInstallerCreationShouldNotCreateVendorDirectory
  106. * @depends testInstallerCreationShouldNotCreateBinDirectory
  107. */
  108. public function testUpdate()
  109. {
  110. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')
  111. ->getMock();
  112. $filesystem
  113. ->expects($this->once())
  114. ->method('rename')
  115. ->with($this->vendorDir.'/package1/oldtarget', $this->vendorDir.'/package1/newtarget');
  116. $initial = $this->createPackageMock();
  117. $target = $this->createPackageMock();
  118. $initial
  119. ->expects($this->any())
  120. ->method('getPrettyName')
  121. ->will($this->returnValue('package1'));
  122. $initial
  123. ->expects($this->any())
  124. ->method('getTargetDir')
  125. ->will($this->returnValue('oldtarget'));
  126. $target
  127. ->expects($this->any())
  128. ->method('getPrettyName')
  129. ->will($this->returnValue('package1'));
  130. $target
  131. ->expects($this->any())
  132. ->method('getTargetDir')
  133. ->will($this->returnValue('newtarget'));
  134. $this->repository
  135. ->expects($this->exactly(3))
  136. ->method('hasPackage')
  137. ->will($this->onConsecutiveCalls(true, false, false));
  138. $this->dm
  139. ->expects($this->once())
  140. ->method('update')
  141. ->with($initial, $target, $this->vendorDir.'/package1/newtarget');
  142. $this->repository
  143. ->expects($this->once())
  144. ->method('removePackage')
  145. ->with($initial);
  146. $this->repository
  147. ->expects($this->once())
  148. ->method('addPackage')
  149. ->with($target);
  150. $library = new LibraryInstaller($this->io, $this->composer, 'library', $filesystem);
  151. $library->update($this->repository, $initial, $target);
  152. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  153. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  154. $this->setExpectedException('InvalidArgumentException');
  155. $library->update($this->repository, $initial, $target);
  156. }
  157. public function testUninstall()
  158. {
  159. $library = new LibraryInstaller($this->io, $this->composer);
  160. $package = $this->createPackageMock();
  161. $package
  162. ->expects($this->any())
  163. ->method('getPrettyName')
  164. ->will($this->returnValue('pkg'));
  165. $this->repository
  166. ->expects($this->exactly(2))
  167. ->method('hasPackage')
  168. ->with($package)
  169. ->will($this->onConsecutiveCalls(true, false));
  170. $this->dm
  171. ->expects($this->once())
  172. ->method('remove')
  173. ->with($package, $this->vendorDir.'/pkg');
  174. $this->repository
  175. ->expects($this->once())
  176. ->method('removePackage')
  177. ->with($package);
  178. $library->uninstall($this->repository, $package);
  179. $this->setExpectedException('InvalidArgumentException');
  180. $library->uninstall($this->repository, $package);
  181. }
  182. public function testGetInstallPath()
  183. {
  184. $library = new LibraryInstaller($this->io, $this->composer);
  185. $package = $this->createPackageMock();
  186. $package
  187. ->expects($this->once())
  188. ->method('getTargetDir')
  189. ->will($this->returnValue(null));
  190. $this->assertEquals($this->vendorDir.'/'.$package->getName(), $library->getInstallPath($package));
  191. }
  192. public function testGetInstallPathWithTargetDir()
  193. {
  194. $library = new LibraryInstaller($this->io, $this->composer);
  195. $package = $this->createPackageMock();
  196. $package
  197. ->expects($this->once())
  198. ->method('getTargetDir')
  199. ->will($this->returnValue('Some/Namespace'));
  200. $package
  201. ->expects($this->any())
  202. ->method('getPrettyName')
  203. ->will($this->returnValue('foo/bar'));
  204. $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
  205. }
  206. /**
  207. * @depends testInstallerCreationShouldNotCreateVendorDirectory
  208. * @depends testInstallerCreationShouldNotCreateBinDirectory
  209. */
  210. public function testEnsureBinariesInstalled()
  211. {
  212. $binaryInstallerMock = $this->getMockBuilder('Composer\Installer\BinaryInstaller')
  213. ->disableOriginalConstructor()
  214. ->getMock();
  215. $library = new LibraryInstaller($this->io, $this->composer, 'library', null, $binaryInstallerMock);
  216. $package = $this->createPackageMock();
  217. $binaryInstallerMock
  218. ->expects($this->never())
  219. ->method('removeBinaries')
  220. ->with($package);
  221. $binaryInstallerMock
  222. ->expects($this->once())
  223. ->method('installBinaries')
  224. ->with($package, $library->getInstallPath($package), false);
  225. $library->ensureBinariesPresence($package);
  226. }
  227. protected function createPackageMock()
  228. {
  229. return $this->getMockBuilder('Composer\Package\Package')
  230. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  231. ->getMock();
  232. }
  233. }