LibraryInstallerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. private $composer;
  20. private $config;
  21. private $vendorDir;
  22. private $binDir;
  23. private $dm;
  24. private $repository;
  25. private $io;
  26. private $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. $library = new LibraryInstaller($this->io, $this->composer);
  110. $initial = $this->createPackageMock();
  111. $target = $this->createPackageMock();
  112. $initial
  113. ->expects($this->any())
  114. ->method('getPrettyName')
  115. ->will($this->returnValue('package1'));
  116. $this->repository
  117. ->expects($this->exactly(3))
  118. ->method('hasPackage')
  119. ->will($this->onConsecutiveCalls(true, false, false));
  120. $this->dm
  121. ->expects($this->once())
  122. ->method('update')
  123. ->with($initial, $target, $this->vendorDir.'/package1');
  124. $this->repository
  125. ->expects($this->once())
  126. ->method('removePackage')
  127. ->with($initial);
  128. $this->repository
  129. ->expects($this->once())
  130. ->method('addPackage')
  131. ->with($target);
  132. $library->update($this->repository, $initial, $target);
  133. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  134. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  135. $this->setExpectedException('InvalidArgumentException');
  136. $library->update($this->repository, $initial, $target);
  137. }
  138. public function testUninstall()
  139. {
  140. $library = new LibraryInstaller($this->io, $this->composer);
  141. $package = $this->createPackageMock();
  142. $package
  143. ->expects($this->any())
  144. ->method('getPrettyName')
  145. ->will($this->returnValue('pkg'));
  146. $this->repository
  147. ->expects($this->exactly(2))
  148. ->method('hasPackage')
  149. ->with($package)
  150. ->will($this->onConsecutiveCalls(true, false));
  151. $this->dm
  152. ->expects($this->once())
  153. ->method('remove')
  154. ->with($package, $this->vendorDir.'/pkg');
  155. $this->repository
  156. ->expects($this->once())
  157. ->method('removePackage')
  158. ->with($package);
  159. $library->uninstall($this->repository, $package);
  160. // TODO re-enable once #125 is fixed and we throw exceptions again
  161. // $this->setExpectedException('InvalidArgumentException');
  162. $library->uninstall($this->repository, $package);
  163. }
  164. public function testGetInstallPath()
  165. {
  166. $library = new LibraryInstaller($this->io, $this->composer);
  167. $package = $this->createPackageMock();
  168. $package
  169. ->expects($this->once())
  170. ->method('getTargetDir')
  171. ->will($this->returnValue(null));
  172. $this->assertEquals($this->vendorDir.'/'.$package->getName(), $library->getInstallPath($package));
  173. }
  174. public function testGetInstallPathWithTargetDir()
  175. {
  176. $library = new LibraryInstaller($this->io, $this->composer);
  177. $package = $this->createPackageMock();
  178. $package
  179. ->expects($this->once())
  180. ->method('getTargetDir')
  181. ->will($this->returnValue('Some/Namespace'));
  182. $package
  183. ->expects($this->any())
  184. ->method('getPrettyName')
  185. ->will($this->returnValue('foo/bar'));
  186. $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
  187. }
  188. private function createPackageMock()
  189. {
  190. return $this->getMockBuilder('Composer\Package\MemoryPackage')
  191. ->setConstructorArgs(array(md5(rand()), '1.0.0.0', '1.0.0'))
  192. ->getMock();
  193. }
  194. }