LibraryInstallerTest.php 7.2 KB

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