InstallationManagerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\InstallationManager;
  13. use Composer\DependencyResolver\Operation\InstallOperation;
  14. use Composer\DependencyResolver\Operation\UpdateOperation;
  15. use Composer\DependencyResolver\Operation\UninstallOperation;
  16. use PHPUnit\Framework\TestCase;
  17. class InstallationManagerTest extends TestCase
  18. {
  19. public function setUp()
  20. {
  21. $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
  22. }
  23. public function testAddGetInstaller()
  24. {
  25. $installer = $this->createInstallerMock();
  26. $installer
  27. ->expects($this->exactly(2))
  28. ->method('supports')
  29. ->will($this->returnCallback(function ($arg) {
  30. return $arg === 'vendor';
  31. }));
  32. $manager = new InstallationManager();
  33. $manager->addInstaller($installer);
  34. $this->assertSame($installer, $manager->getInstaller('vendor'));
  35. $this->setExpectedException('InvalidArgumentException');
  36. $manager->getInstaller('unregistered');
  37. }
  38. public function testAddRemoveInstaller()
  39. {
  40. $installer = $this->createInstallerMock();
  41. $installer
  42. ->expects($this->exactly(2))
  43. ->method('supports')
  44. ->will($this->returnCallback(function ($arg) {
  45. return $arg === 'vendor';
  46. }));
  47. $installer2 = $this->createInstallerMock();
  48. $installer2
  49. ->expects($this->exactly(1))
  50. ->method('supports')
  51. ->will($this->returnCallback(function ($arg) {
  52. return $arg === 'vendor';
  53. }));
  54. $manager = new InstallationManager();
  55. $manager->addInstaller($installer);
  56. $this->assertSame($installer, $manager->getInstaller('vendor'));
  57. $manager->addInstaller($installer2);
  58. $this->assertSame($installer2, $manager->getInstaller('vendor'));
  59. $manager->removeInstaller($installer2);
  60. $this->assertSame($installer, $manager->getInstaller('vendor'));
  61. }
  62. public function testExecute()
  63. {
  64. $manager = $this->getMockBuilder('Composer\Installer\InstallationManager')
  65. ->setMethods(array('install', 'update', 'uninstall'))
  66. ->getMock();
  67. $installOperation = new InstallOperation($this->createPackageMock());
  68. $removeOperation = new UninstallOperation($this->createPackageMock());
  69. $updateOperation = new UpdateOperation(
  70. $this->createPackageMock(), $this->createPackageMock()
  71. );
  72. $manager
  73. ->expects($this->once())
  74. ->method('install')
  75. ->with($this->repository, $installOperation);
  76. $manager
  77. ->expects($this->once())
  78. ->method('uninstall')
  79. ->with($this->repository, $removeOperation);
  80. $manager
  81. ->expects($this->once())
  82. ->method('update')
  83. ->with($this->repository, $updateOperation);
  84. $manager->execute($this->repository, $installOperation);
  85. $manager->execute($this->repository, $removeOperation);
  86. $manager->execute($this->repository, $updateOperation);
  87. }
  88. public function testInstall()
  89. {
  90. $installer = $this->createInstallerMock();
  91. $manager = new InstallationManager();
  92. $manager->addInstaller($installer);
  93. $package = $this->createPackageMock();
  94. $operation = new InstallOperation($package, 'test');
  95. $package
  96. ->expects($this->once())
  97. ->method('getType')
  98. ->will($this->returnValue('library'));
  99. $installer
  100. ->expects($this->once())
  101. ->method('supports')
  102. ->with('library')
  103. ->will($this->returnValue(true));
  104. $installer
  105. ->expects($this->once())
  106. ->method('install')
  107. ->with($this->repository, $package);
  108. $manager->install($this->repository, $operation);
  109. }
  110. public function testUpdateWithEqualTypes()
  111. {
  112. $installer = $this->createInstallerMock();
  113. $manager = new InstallationManager();
  114. $manager->addInstaller($installer);
  115. $initial = $this->createPackageMock();
  116. $target = $this->createPackageMock();
  117. $operation = new UpdateOperation($initial, $target, 'test');
  118. $initial
  119. ->expects($this->once())
  120. ->method('getType')
  121. ->will($this->returnValue('library'));
  122. $target
  123. ->expects($this->once())
  124. ->method('getType')
  125. ->will($this->returnValue('library'));
  126. $installer
  127. ->expects($this->once())
  128. ->method('supports')
  129. ->with('library')
  130. ->will($this->returnValue(true));
  131. $installer
  132. ->expects($this->once())
  133. ->method('update')
  134. ->with($this->repository, $initial, $target);
  135. $manager->update($this->repository, $operation);
  136. }
  137. public function testUpdateWithNotEqualTypes()
  138. {
  139. $libInstaller = $this->createInstallerMock();
  140. $bundleInstaller = $this->createInstallerMock();
  141. $manager = new InstallationManager();
  142. $manager->addInstaller($libInstaller);
  143. $manager->addInstaller($bundleInstaller);
  144. $initial = $this->createPackageMock();
  145. $target = $this->createPackageMock();
  146. $operation = new UpdateOperation($initial, $target, 'test');
  147. $initial
  148. ->expects($this->once())
  149. ->method('getType')
  150. ->will($this->returnValue('library'));
  151. $target
  152. ->expects($this->once())
  153. ->method('getType')
  154. ->will($this->returnValue('bundles'));
  155. $bundleInstaller
  156. ->expects($this->exactly(2))
  157. ->method('supports')
  158. ->will($this->returnCallback(function ($arg) {
  159. return $arg === 'bundles';
  160. }));
  161. $libInstaller
  162. ->expects($this->once())
  163. ->method('supports')
  164. ->with('library')
  165. ->will($this->returnValue(true));
  166. $libInstaller
  167. ->expects($this->once())
  168. ->method('uninstall')
  169. ->with($this->repository, $initial);
  170. $bundleInstaller
  171. ->expects($this->once())
  172. ->method('install')
  173. ->with($this->repository, $target);
  174. $manager->update($this->repository, $operation);
  175. }
  176. public function testUninstall()
  177. {
  178. $installer = $this->createInstallerMock();
  179. $manager = new InstallationManager();
  180. $manager->addInstaller($installer);
  181. $package = $this->createPackageMock();
  182. $operation = new UninstallOperation($package, 'test');
  183. $package
  184. ->expects($this->once())
  185. ->method('getType')
  186. ->will($this->returnValue('library'));
  187. $installer
  188. ->expects($this->once())
  189. ->method('uninstall')
  190. ->with($this->repository, $package);
  191. $installer
  192. ->expects($this->once())
  193. ->method('supports')
  194. ->with('library')
  195. ->will($this->returnValue(true));
  196. $manager->uninstall($this->repository, $operation);
  197. }
  198. public function testInstallBinary()
  199. {
  200. $installer = $this->getMockBuilder('Composer\Installer\LibraryInstaller')
  201. ->disableOriginalConstructor()
  202. ->getMock();
  203. $manager = new InstallationManager();
  204. $manager->addInstaller($installer);
  205. $package = $this->createPackageMock();
  206. $package
  207. ->expects($this->once())
  208. ->method('getType')
  209. ->will($this->returnValue('library'));
  210. $installer
  211. ->expects($this->once())
  212. ->method('supports')
  213. ->with('library')
  214. ->will($this->returnValue(true));
  215. $installer
  216. ->expects($this->once())
  217. ->method('ensureBinariesPresence')
  218. ->with($package);
  219. $manager->ensureBinariesPresence($package);
  220. }
  221. private function createInstallerMock()
  222. {
  223. return $this->getMockBuilder('Composer\Installer\InstallerInterface')
  224. ->getMock();
  225. }
  226. private function createPackageMock()
  227. {
  228. return $this->getMockBuilder('Composer\Package\PackageInterface')
  229. ->getMock();
  230. }
  231. }