InstallationManagerTest.php 8.6 KB

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