LibraryInstallerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. $this->dm
  91. ->expects($this->once())
  92. ->method('download')
  93. ->with($package, $this->dir)
  94. ->will($this->returnValue('source'));
  95. $this->registry
  96. ->expects($this->once())
  97. ->method('registerPackage')
  98. ->with($package, 'source');
  99. $library->install($package);
  100. }
  101. public function testUpdate()
  102. {
  103. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  104. $initial = $this->createPackageMock();
  105. $target = $this->createPackageMock();
  106. $this->registry
  107. ->expects($this->exactly(2))
  108. ->method('isPackageRegistered')
  109. ->with($initial)
  110. ->will($this->onConsecutiveCalls(true, false));
  111. $this->registry
  112. ->expects($this->once())
  113. ->method('getRegisteredPackageInstallerType')
  114. ->with($initial)
  115. ->will($this->returnValue('dist'));
  116. $this->dm
  117. ->expects($this->once())
  118. ->method('update')
  119. ->with($initial, $target, $this->dir, 'dist');
  120. $this->registry
  121. ->expects($this->once())
  122. ->method('unregisterPackage')
  123. ->with($initial);
  124. $this->registry
  125. ->expects($this->once())
  126. ->method('registerPackage')
  127. ->with($target, 'dist');
  128. $library->update($initial, $target);
  129. $this->setExpectedException('UnexpectedValueException');
  130. $library->update($initial, $target);
  131. }
  132. public function testUninstall()
  133. {
  134. $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
  135. $package = $this->createPackageMock();
  136. $this->registry
  137. ->expects($this->exactly(2))
  138. ->method('isPackageRegistered')
  139. ->with($package)
  140. ->will($this->onConsecutiveCalls(true, false));
  141. $this->registry
  142. ->expects($this->once())
  143. ->method('getRegisteredPackageInstallerType')
  144. ->with($package)
  145. ->will($this->returnValue('source'));
  146. $this->dm
  147. ->expects($this->once())
  148. ->method('remove')
  149. ->with($package, $this->dir, 'source');
  150. $this->registry
  151. ->expects($this->once())
  152. ->method('unregisterPackage')
  153. ->with($package);
  154. $library->uninstall($package);
  155. $this->setExpectedException('UnexpectedValueException');
  156. $library->uninstall($package);
  157. }
  158. private function createPackageMock()
  159. {
  160. return $this->getMockBuilder('Composer\Package\MemoryPackage')
  161. ->setConstructorArgs(array(md5(rand()), '1.0.0'))
  162. ->getMock();
  163. }
  164. }