LibraryInstallerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\TestCase;
  15. use Composer\Composer;
  16. use Composer\Config;
  17. class LibraryInstallerTest extends TestCase
  18. {
  19. protected $composer;
  20. protected $config;
  21. protected $vendorDir;
  22. protected $binDir;
  23. protected $dm;
  24. protected $repository;
  25. protected $io;
  26. protected $fs;
  27. protected function setUp()
  28. {
  29. $this->fs = new Filesystem;
  30. $this->composer = new Composer();
  31. $this->config = new Config();
  32. $this->composer->setConfig($this->config);
  33. $this->vendorDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-vendor';
  34. $this->ensureDirectoryExistsAndClear($this->vendorDir);
  35. $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin';
  36. $this->ensureDirectoryExistsAndClear($this->binDir);
  37. $this->config->merge(array(
  38. 'config' => array(
  39. 'vendor-dir' => $this->vendorDir,
  40. 'bin-dir' => $this->binDir,
  41. ),
  42. ));
  43. $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->composer->setDownloadManager($this->dm);
  47. $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
  48. $this->io = $this->getMock('Composer\IO\IOInterface');
  49. }
  50. protected function tearDown()
  51. {
  52. $this->fs->removeDirectory($this->vendorDir);
  53. $this->fs->removeDirectory($this->binDir);
  54. }
  55. public function testInstallerCreationShouldNotCreateVendorDirectory()
  56. {
  57. $this->fs->removeDirectory($this->vendorDir);
  58. new LibraryInstaller($this->io, $this->composer);
  59. $this->assertFileNotExists($this->vendorDir);
  60. }
  61. public function testInstallerCreationShouldNotCreateBinDirectory()
  62. {
  63. $this->fs->removeDirectory($this->binDir);
  64. new LibraryInstaller($this->io, $this->composer);
  65. $this->assertFileNotExists($this->binDir);
  66. }
  67. public function testIsInstalled()
  68. {
  69. $library = new LibraryInstaller($this->io, $this->composer);
  70. $package = $this->createPackageMock();
  71. $this->repository
  72. ->expects($this->exactly(2))
  73. ->method('hasPackage')
  74. ->with($package)
  75. ->will($this->onConsecutiveCalls(true, false));
  76. $this->assertTrue($library->isInstalled($this->repository, $package));
  77. $this->assertFalse($library->isInstalled($this->repository, $package));
  78. }
  79. /**
  80. * @depends testInstallerCreationShouldNotCreateVendorDirectory
  81. * @depends testInstallerCreationShouldNotCreateBinDirectory
  82. */
  83. public function testInstall()
  84. {
  85. $library = new LibraryInstaller($this->io, $this->composer);
  86. $package = $this->createPackageMock();
  87. $package
  88. ->expects($this->any())
  89. ->method('getPrettyName')
  90. ->will($this->returnValue('some/package'));
  91. $this->dm
  92. ->expects($this->once())
  93. ->method('download')
  94. ->with($package, $this->vendorDir.'/some/package');
  95. $this->repository
  96. ->expects($this->once())
  97. ->method('addPackage')
  98. ->with($package);
  99. $library->install($this->repository, $package);
  100. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  101. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  102. }
  103. /**
  104. * @depends testInstallerCreationShouldNotCreateVendorDirectory
  105. * @depends testInstallerCreationShouldNotCreateBinDirectory
  106. */
  107. public function testUpdate()
  108. {
  109. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')
  110. ->getMock();
  111. $filesystem
  112. ->expects($this->once())
  113. ->method('rename')
  114. ->with($this->vendorDir.'/package1/oldtarget', $this->vendorDir.'/package1/newtarget');
  115. $initial = $this->createPackageMock();
  116. $target = $this->createPackageMock();
  117. $initial
  118. ->expects($this->once())
  119. ->method('getPrettyName')
  120. ->will($this->returnValue('package1'));
  121. $initial
  122. ->expects($this->once())
  123. ->method('getTargetDir')
  124. ->will($this->returnValue('oldtarget'));
  125. $target
  126. ->expects($this->once())
  127. ->method('getPrettyName')
  128. ->will($this->returnValue('package1'));
  129. $target
  130. ->expects($this->once())
  131. ->method('getTargetDir')
  132. ->will($this->returnValue('newtarget'));
  133. $this->repository
  134. ->expects($this->exactly(3))
  135. ->method('hasPackage')
  136. ->will($this->onConsecutiveCalls(true, false, false));
  137. $this->dm
  138. ->expects($this->once())
  139. ->method('update')
  140. ->with($initial, $target, $this->vendorDir.'/package1/newtarget');
  141. $this->repository
  142. ->expects($this->once())
  143. ->method('removePackage')
  144. ->with($initial);
  145. $this->repository
  146. ->expects($this->once())
  147. ->method('addPackage')
  148. ->with($target);
  149. $library = new LibraryInstaller($this->io, $this->composer, 'library', $filesystem);
  150. $library->update($this->repository, $initial, $target);
  151. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  152. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  153. $this->setExpectedException('InvalidArgumentException');
  154. $library->update($this->repository, $initial, $target);
  155. }
  156. public function testUninstall()
  157. {
  158. $library = new LibraryInstaller($this->io, $this->composer);
  159. $package = $this->createPackageMock();
  160. $package
  161. ->expects($this->any())
  162. ->method('getPrettyName')
  163. ->will($this->returnValue('pkg'));
  164. $this->repository
  165. ->expects($this->exactly(2))
  166. ->method('hasPackage')
  167. ->with($package)
  168. ->will($this->onConsecutiveCalls(true, false));
  169. $this->dm
  170. ->expects($this->once())
  171. ->method('remove')
  172. ->with($package, $this->vendorDir.'/pkg');
  173. $this->repository
  174. ->expects($this->once())
  175. ->method('removePackage')
  176. ->with($package);
  177. $library->uninstall($this->repository, $package);
  178. $this->setExpectedException('InvalidArgumentException');
  179. $library->uninstall($this->repository, $package);
  180. }
  181. public function testGetInstallPath()
  182. {
  183. $library = new LibraryInstaller($this->io, $this->composer);
  184. $package = $this->createPackageMock();
  185. $package
  186. ->expects($this->once())
  187. ->method('getTargetDir')
  188. ->will($this->returnValue(null));
  189. $this->assertEquals($this->vendorDir.'/'.$package->getName(), $library->getInstallPath($package));
  190. }
  191. public function testGetInstallPathWithTargetDir()
  192. {
  193. $library = new LibraryInstaller($this->io, $this->composer);
  194. $package = $this->createPackageMock();
  195. $package
  196. ->expects($this->once())
  197. ->method('getTargetDir')
  198. ->will($this->returnValue('Some/Namespace'));
  199. $package
  200. ->expects($this->any())
  201. ->method('getPrettyName')
  202. ->will($this->returnValue('foo/bar'));
  203. $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
  204. }
  205. protected function createPackageMock()
  206. {
  207. return $this->getMockBuilder('Composer\Package\Package')
  208. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  209. ->getMock();
  210. }
  211. }