LibraryInstallerTest.php 6.4 KB

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