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. 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. $this->fs->removeDirectory($this->vendorDir);
  29. }
  30. mkdir($this->vendorDir);
  31. $this->binDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'composer-test-bin';
  32. if (is_dir($this->binDir)) {
  33. $this->fs->removeDirectory($this->binDir);
  34. }
  35. mkdir($this->binDir);
  36. $this->dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->repository = $this->getMockBuilder('Composer\Repository\WritableRepositoryInterface')
  40. ->getMock();
  41. $this->io = $this->getMockBuilder('Composer\IO\IOInterface')
  42. ->getMock();
  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(2))
  107. ->method('hasPackage')
  108. ->with($initial)
  109. ->will($this->onConsecutiveCalls(true, false));
  110. $this->dm
  111. ->expects($this->once())
  112. ->method('update')
  113. ->with($initial, $target, $this->vendorDir.'/package1');
  114. $this->repository
  115. ->expects($this->once())
  116. ->method('removePackage')
  117. ->with($initial);
  118. $this->repository
  119. ->expects($this->once())
  120. ->method('addPackage')
  121. ->with($target);
  122. $library->update($initial, $target);
  123. $this->assertFileExists($this->vendorDir, 'Vendor dir should be created');
  124. $this->assertFileExists($this->binDir, 'Bin dir should be created');
  125. $this->setExpectedException('InvalidArgumentException');
  126. $library->update($initial, $target);
  127. }
  128. public function testUninstall()
  129. {
  130. $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
  131. $package = $this->createPackageMock();
  132. $package
  133. ->expects($this->once())
  134. ->method('getPrettyName')
  135. ->will($this->returnValue('pkg'));
  136. $this->repository
  137. ->expects($this->exactly(2))
  138. ->method('hasPackage')
  139. ->with($package)
  140. ->will($this->onConsecutiveCalls(true, false));
  141. $this->dm
  142. ->expects($this->once())
  143. ->method('remove')
  144. ->with($package, $this->vendorDir.'/pkg');
  145. $this->repository
  146. ->expects($this->once())
  147. ->method('removePackage')
  148. ->with($package);
  149. $library->uninstall($package);
  150. // TODO re-enable once #125 is fixed and we throw exceptions again
  151. // $this->setExpectedException('InvalidArgumentException');
  152. $library->uninstall($package);
  153. }
  154. public function testGetInstallPath()
  155. {
  156. $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
  157. $package = $this->createPackageMock();
  158. $package
  159. ->expects($this->once())
  160. ->method('getTargetDir')
  161. ->will($this->returnValue(null));
  162. $this->assertEquals($this->vendorDir.'/'.$package->getName(), $library->getInstallPath($package));
  163. }
  164. public function testGetInstallPathWithTargetDir()
  165. {
  166. $library = new LibraryInstaller($this->vendorDir, $this->binDir, $this->dm, $this->repository, $this->io);
  167. $package = $this->createPackageMock();
  168. $package
  169. ->expects($this->once())
  170. ->method('getTargetDir')
  171. ->will($this->returnValue('Some/Namespace'));
  172. $package
  173. ->expects($this->any())
  174. ->method('getPrettyName')
  175. ->will($this->returnValue('foo/bar'));
  176. $this->assertEquals($this->vendorDir.'/'.$package->getPrettyName().'/Some/Namespace', $library->getInstallPath($package));
  177. }
  178. private function createPackageMock()
  179. {
  180. return $this->getMockBuilder('Composer\Package\MemoryPackage')
  181. ->setConstructorArgs(array(md5(rand()), '1.0.0.0', '1.0.0'))
  182. ->getMock();
  183. }
  184. }