LibraryInstallerTest.php 5.7 KB

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