LibraryInstallerTest.php 7.4 KB

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