InstallationManagerTest.php 8.5 KB

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