LibraryInstallerTest.php 7.6 KB

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