LibraryInstallerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 $registry;
  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->registry = $this->getMockBuilder('Composer\Installer\Registry\RegistryInterface')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. }
  33. public function testInstallerCreation()
  34. {
  35. $this->registry
  36. ->expects($this->once())
  37. ->method('open');
  38. $this->registry
  39. ->expects($this->once())
  40. ->method('close');
  41. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  42. $this->assertTrue(is_dir($this->dir));
  43. $file = sys_get_temp_dir().'/file';
  44. touch($file);
  45. $this->setExpectedException('UnexpectedValueException');
  46. $library = new LibraryInstaller($file, $this->dm, $this->registry);
  47. }
  48. public function testExecuteOperation()
  49. {
  50. $library = $this->getMockBuilder('Composer\Installer\LibraryInstaller')
  51. ->setConstructorArgs(array($this->dir, $this->dm, $this->registry))
  52. ->setMethods(array('install', 'update', 'uninstall'))
  53. ->getMock();
  54. $packageToInstall = $this->createPackageMock();
  55. $packageToRemove = $this->createPackageMock();
  56. $packageToUpdate = $this->createPackageMock();
  57. $updatedPackage = $this->createPackageMock();
  58. $library
  59. ->expects($this->once())
  60. ->method('install')
  61. ->with($packageToInstall);
  62. $library
  63. ->expects($this->once())
  64. ->method('uninstall')
  65. ->with($packageToRemove);
  66. $library
  67. ->expects($this->once())
  68. ->method('update')
  69. ->with($packageToUpdate, $updatedPackage);
  70. $library->executeOperation(new Operation\InstallOperation($packageToInstall));
  71. $library->executeOperation(new Operation\UninstallOperation($packageToRemove));
  72. $library->executeOperation(new Operation\UpdateOperation($packageToUpdate, $updatedPackage));
  73. }
  74. public function testIsInstalled()
  75. {
  76. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  77. $package = $this->createPackageMock();
  78. $this->registry
  79. ->expects($this->exactly(2))
  80. ->method('isPackageRegistered')
  81. ->with($package)
  82. ->will($this->onConsecutiveCalls(true, false));
  83. $this->assertTrue($library->isInstalled($package));
  84. $this->assertFalse($library->isInstalled($package));
  85. }
  86. public function testInstall()
  87. {
  88. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  89. $package = $this->createPackageMock();
  90. $package
  91. ->expects($this->once())
  92. ->method('getName')
  93. ->will($this->returnValue('some/package'));
  94. $this->dm
  95. ->expects($this->once())
  96. ->method('download')
  97. ->with($package, $this->dir.'/some/package')
  98. ->will($this->returnValue('source'));
  99. $this->registry
  100. ->expects($this->once())
  101. ->method('registerPackage')
  102. ->with($package, 'source');
  103. $library->install($package);
  104. }
  105. public function testUpdate()
  106. {
  107. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  108. $initial = $this->createPackageMock();
  109. $target = $this->createPackageMock();
  110. $initial
  111. ->expects($this->once())
  112. ->method('getName')
  113. ->will($this->returnValue('package1'));
  114. $this->registry
  115. ->expects($this->exactly(2))
  116. ->method('isPackageRegistered')
  117. ->with($initial)
  118. ->will($this->onConsecutiveCalls(true, false));
  119. $this->registry
  120. ->expects($this->once())
  121. ->method('getRegisteredPackageInstallerType')
  122. ->with($initial)
  123. ->will($this->returnValue('dist'));
  124. $this->dm
  125. ->expects($this->once())
  126. ->method('update')
  127. ->with($initial, $target, $this->dir.'/package1', 'dist');
  128. $this->registry
  129. ->expects($this->once())
  130. ->method('unregisterPackage')
  131. ->with($initial);
  132. $this->registry
  133. ->expects($this->once())
  134. ->method('registerPackage')
  135. ->with($target, 'dist');
  136. $library->update($initial, $target);
  137. $this->setExpectedException('UnexpectedValueException');
  138. $library->update($initial, $target);
  139. }
  140. public function testUninstall()
  141. {
  142. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  143. $package = $this->createPackageMock();
  144. $package
  145. ->expects($this->once())
  146. ->method('getName')
  147. ->will($this->returnValue('pkg'));
  148. $this->registry
  149. ->expects($this->exactly(2))
  150. ->method('isPackageRegistered')
  151. ->with($package)
  152. ->will($this->onConsecutiveCalls(true, false));
  153. $this->registry
  154. ->expects($this->once())
  155. ->method('getRegisteredPackageInstallerType')
  156. ->with($package)
  157. ->will($this->returnValue('source'));
  158. $this->dm
  159. ->expects($this->once())
  160. ->method('remove')
  161. ->with($package, $this->dir.'/pkg', 'source');
  162. $this->registry
  163. ->expects($this->once())
  164. ->method('unregisterPackage')
  165. ->with($package);
  166. $library->uninstall($package);
  167. $this->setExpectedException('UnexpectedValueException');
  168. $library->uninstall($package);
  169. }
  170. private function createPackageMock()
  171. {
  172. return $this->getMockBuilder('Composer\Package\MemoryPackage')
  173. ->setConstructorArgs(array(md5(rand()), '1.0.0'))
  174. ->getMock();
  175. }
  176. }