LibraryInstallerTest.php 7.4 KB

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