InstallationManagerTest.php 9.1 KB

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