LibraryInstallerTest.php 6.6 KB

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