LibraryInstallerTest.php 7.3 KB

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