LibraryInstallerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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('copyThenRemove')
  114. ->with($this->vendorDir.'/package1', $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. $target
  122. ->expects($this->once())
  123. ->method('getPrettyName')
  124. ->will($this->returnValue('package1'));
  125. $target
  126. ->expects($this->once())
  127. ->method('getTargetDir')
  128. ->will($this->returnValue('newtarget'));
  129. $this->repository
  130. ->expects($this->exactly(3))
  131. ->method('hasPackage')
  132. ->will($this->onConsecutiveCalls(true, false, false));
  133. $this->dm
  134. ->expects($this->once())
  135. ->method('update')
  136. ->with($initial, $target, $this->vendorDir.'/package1/newtarget');
  137. $this->repository
  138. ->expects($this->once())
  139. ->method('removePackage')
  140. ->with($initial);
  141. $this->repository
  142. ->expects($this->once())
  143. ->method('addPackage')
  144. ->with($target);
  145. $library = new LibraryInstaller($this->io, $this->composer, 'library', $filesystem);
  146. $library->update($this->repository, $initial, $target);
  147. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  148. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  149. $this->setExpectedException('InvalidArgumentException');
  150. $library->update($this->repository, $initial, $target);
  151. }
  152. public function testUninstall()
  153. {
  154. $library = new LibraryInstaller($this->io, $this->composer);
  155. $package = $this->createPackageMock();
  156. $package
  157. ->expects($this->any())
  158. ->method('getPrettyName')
  159. ->will($this->returnValue('pkg'));
  160. $this->repository
  161. ->expects($this->exactly(2))
  162. ->method('hasPackage')
  163. ->with($package)
  164. ->will($this->onConsecutiveCalls(true, false));
  165. $this->dm
  166. ->expects($this->once())
  167. ->method('remove')
  168. ->with($package, $this->vendorDir.'/pkg');
  169. $this->repository
  170. ->expects($this->once())
  171. ->method('removePackage')
  172. ->with($package);
  173. $library->uninstall($this->repository, $package);
  174. $this->setExpectedException('InvalidArgumentException');
  175. $library->uninstall($this->repository, $package);
  176. }
  177. public function testGetInstallPath()
  178. {
  179. $library = new LibraryInstaller($this->io, $this->composer);
  180. $package = $this->createPackageMock();
  181. $package
  182. ->expects($this->once())
  183. ->method('getTargetDir')
  184. ->will($this->returnValue(null));
  185. $this->assertEquals($this->vendorDir.'/'.$package->getName(), $library->getInstallPath($package));
  186. }
  187. public function testGetInstallPathWithTargetDir()
  188. {
  189. $library = new LibraryInstaller($this->io, $this->composer);
  190. $package = $this->createPackageMock();
  191. $package
  192. ->expects($this->once())
  193. ->method('getTargetDir')
  194. ->will($this->returnValue('Some/Namespace'));
  195. $package
  196. ->expects($this->any())
  197. ->method('getPrettyName')
  198. ->will($this->returnValue('foo/bar'));
  199. $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
  200. }
  201. protected function createPackageMock()
  202. {
  203. return $this->getMockBuilder('Composer\Package\Package')
  204. ->setConstructorArgs(array(md5(mt_rand()), '1.0.0.0', '1.0.0'))
  205. ->getMock();
  206. }
  207. }