LibraryInstallerTest.php 7.2 KB

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