InstallationManagerTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Composer\Test\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(),
  72. $this->createPackageMock()
  73. );
  74. $manager
  75. ->expects($this->once())
  76. ->method('install')
  77. ->with($this->repository, $installOperation);
  78. $manager
  79. ->expects($this->once())
  80. ->method('uninstall')
  81. ->with($this->repository, $removeOperation);
  82. $manager
  83. ->expects($this->once())
  84. ->method('update')
  85. ->with($this->repository, $updateOperation);
  86. $manager->execute($this->repository, $installOperation);
  87. $manager->execute($this->repository, $removeOperation);
  88. $manager->execute($this->repository, $updateOperation);
  89. }
  90. public function testInstall()
  91. {
  92. $installer = $this->createInstallerMock();
  93. $manager = new InstallationManager();
  94. $manager->addInstaller($installer);
  95. $package = $this->createPackageMock();
  96. $operation = new InstallOperation($package, 'test');
  97. $package
  98. ->expects($this->once())
  99. ->method('getType')
  100. ->will($this->returnValue('library'));
  101. $installer
  102. ->expects($this->once())
  103. ->method('supports')
  104. ->with('library')
  105. ->will($this->returnValue(true));
  106. $installer
  107. ->expects($this->once())
  108. ->method('install')
  109. ->with($this->repository, $package);
  110. $manager->install($this->repository, $operation);
  111. }
  112. public function testUpdateWithEqualTypes()
  113. {
  114. $installer = $this->createInstallerMock();
  115. $manager = new InstallationManager();
  116. $manager->addInstaller($installer);
  117. $initial = $this->createPackageMock();
  118. $target = $this->createPackageMock();
  119. $operation = new UpdateOperation($initial, $target, 'test');
  120. $initial
  121. ->expects($this->once())
  122. ->method('getType')
  123. ->will($this->returnValue('library'));
  124. $target
  125. ->expects($this->once())
  126. ->method('getType')
  127. ->will($this->returnValue('library'));
  128. $installer
  129. ->expects($this->once())
  130. ->method('supports')
  131. ->with('library')
  132. ->will($this->returnValue(true));
  133. $installer
  134. ->expects($this->once())
  135. ->method('update')
  136. ->with($this->repository, $initial, $target);
  137. $manager->update($this->repository, $operation);
  138. }
  139. public function testUpdateWithNotEqualTypes()
  140. {
  141. $libInstaller = $this->createInstallerMock();
  142. $bundleInstaller = $this->createInstallerMock();
  143. $manager = new InstallationManager();
  144. $manager->addInstaller($libInstaller);
  145. $manager->addInstaller($bundleInstaller);
  146. $initial = $this->createPackageMock();
  147. $target = $this->createPackageMock();
  148. $operation = new UpdateOperation($initial, $target, 'test');
  149. $initial
  150. ->expects($this->once())
  151. ->method('getType')
  152. ->will($this->returnValue('library'));
  153. $target
  154. ->expects($this->once())
  155. ->method('getType')
  156. ->will($this->returnValue('bundles'));
  157. $bundleInstaller
  158. ->expects($this->exactly(2))
  159. ->method('supports')
  160. ->will($this->returnCallback(function ($arg) {
  161. return $arg === 'bundles';
  162. }));
  163. $libInstaller
  164. ->expects($this->once())
  165. ->method('supports')
  166. ->with('library')
  167. ->will($this->returnValue(true));
  168. $libInstaller
  169. ->expects($this->once())
  170. ->method('uninstall')
  171. ->with($this->repository, $initial);
  172. $bundleInstaller
  173. ->expects($this->once())
  174. ->method('install')
  175. ->with($this->repository, $target);
  176. $manager->update($this->repository, $operation);
  177. }
  178. public function testUninstall()
  179. {
  180. $installer = $this->createInstallerMock();
  181. $manager = new InstallationManager();
  182. $manager->addInstaller($installer);
  183. $package = $this->createPackageMock();
  184. $operation = new UninstallOperation($package, 'test');
  185. $package
  186. ->expects($this->once())
  187. ->method('getType')
  188. ->will($this->returnValue('library'));
  189. $installer
  190. ->expects($this->once())
  191. ->method('uninstall')
  192. ->with($this->repository, $package);
  193. $installer
  194. ->expects($this->once())
  195. ->method('supports')
  196. ->with('library')
  197. ->will($this->returnValue(true));
  198. $manager->uninstall($this->repository, $operation);
  199. }
  200. public function testInstallBinary()
  201. {
  202. $installer = $this->getMockBuilder('Composer\Installer\LibraryInstaller')
  203. ->disableOriginalConstructor()
  204. ->getMock();
  205. $manager = new InstallationManager();
  206. $manager->addInstaller($installer);
  207. $package = $this->createPackageMock();
  208. $package
  209. ->expects($this->once())
  210. ->method('getType')
  211. ->will($this->returnValue('library'));
  212. $installer
  213. ->expects($this->once())
  214. ->method('supports')
  215. ->with('library')
  216. ->will($this->returnValue(true));
  217. $installer
  218. ->expects($this->once())
  219. ->method('ensureBinariesPresence')
  220. ->with($package);
  221. $manager->ensureBinariesPresence($package);
  222. }
  223. private function createInstallerMock()
  224. {
  225. return $this->getMockBuilder('Composer\Installer\InstallerInterface')
  226. ->getMock();
  227. }
  228. private function createPackageMock()
  229. {
  230. return $this->getMockBuilder('Composer\Package\PackageInterface')
  231. ->getMock();
  232. }
  233. }