InstallationManagerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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('vendor');
  32. $manager->addInstaller($installer);
  33. $this->assertSame($installer, $manager->getInstaller('vendor'));
  34. $this->setExpectedException('InvalidArgumentException');
  35. $manager->getInstaller('unregistered');
  36. }
  37. public function testExecute()
  38. {
  39. $manager = $this->getMockBuilder('Composer\Installer\InstallationManager')
  40. ->setMethods(array('install', 'update', 'uninstall'))
  41. ->getMock();
  42. $installOperation = new InstallOperation($this->createPackageMock());
  43. $removeOperation = new UninstallOperation($this->createPackageMock());
  44. $updateOperation = new UpdateOperation(
  45. $this->createPackageMock(), $this->createPackageMock()
  46. );
  47. $manager
  48. ->expects($this->once())
  49. ->method('install')
  50. ->with($this->repository, $installOperation);
  51. $manager
  52. ->expects($this->once())
  53. ->method('uninstall')
  54. ->with($this->repository, $removeOperation);
  55. $manager
  56. ->expects($this->once())
  57. ->method('update')
  58. ->with($this->repository, $updateOperation);
  59. $manager->execute($this->repository, $installOperation);
  60. $manager->execute($this->repository, $removeOperation);
  61. $manager->execute($this->repository, $updateOperation);
  62. }
  63. public function testInstall()
  64. {
  65. $installer = $this->createInstallerMock();
  66. $manager = new InstallationManager('vendor');
  67. $manager->addInstaller($installer);
  68. $package = $this->createPackageMock();
  69. $operation = new InstallOperation($package, 'test');
  70. $package
  71. ->expects($this->once())
  72. ->method('getType')
  73. ->will($this->returnValue('library'));
  74. $installer
  75. ->expects($this->once())
  76. ->method('supports')
  77. ->with('library')
  78. ->will($this->returnValue(true));
  79. $installer
  80. ->expects($this->once())
  81. ->method('install')
  82. ->with($this->repository, $package);
  83. $manager->install($this->repository, $operation);
  84. }
  85. public function testUpdateWithEqualTypes()
  86. {
  87. $installer = $this->createInstallerMock();
  88. $manager = new InstallationManager('vendor');
  89. $manager->addInstaller($installer);
  90. $initial = $this->createPackageMock();
  91. $target = $this->createPackageMock();
  92. $operation = new UpdateOperation($initial, $target, 'test');
  93. $initial
  94. ->expects($this->once())
  95. ->method('getType')
  96. ->will($this->returnValue('library'));
  97. $target
  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('update')
  109. ->with($this->repository, $initial, $target);
  110. $manager->update($this->repository, $operation);
  111. }
  112. public function testUpdateWithNotEqualTypes()
  113. {
  114. $libInstaller = $this->createInstallerMock();
  115. $bundleInstaller = $this->createInstallerMock();
  116. $manager = new InstallationManager('vendor');
  117. $manager->addInstaller($libInstaller);
  118. $manager->addInstaller($bundleInstaller);
  119. $initial = $this->createPackageMock();
  120. $target = $this->createPackageMock();
  121. $operation = new UpdateOperation($initial, $target, 'test');
  122. $initial
  123. ->expects($this->once())
  124. ->method('getType')
  125. ->will($this->returnValue('library'));
  126. $target
  127. ->expects($this->once())
  128. ->method('getType')
  129. ->will($this->returnValue('bundles'));
  130. $bundleInstaller
  131. ->expects($this->exactly(2))
  132. ->method('supports')
  133. ->will($this->returnCallback(function ($arg) {
  134. return $arg === 'bundles';
  135. }));
  136. $libInstaller
  137. ->expects($this->once())
  138. ->method('supports')
  139. ->with('library')
  140. ->will($this->returnValue(true));
  141. $libInstaller
  142. ->expects($this->once())
  143. ->method('uninstall')
  144. ->with($this->repository, $initial);
  145. $bundleInstaller
  146. ->expects($this->once())
  147. ->method('install')
  148. ->with($this->repository, $target);
  149. $manager->update($this->repository, $operation);
  150. }
  151. public function testUninstall()
  152. {
  153. $installer = $this->createInstallerMock();
  154. $manager = new InstallationManager('vendor');
  155. $manager->addInstaller($installer);
  156. $package = $this->createPackageMock();
  157. $operation = new UninstallOperation($package, 'test');
  158. $package
  159. ->expects($this->once())
  160. ->method('getType')
  161. ->will($this->returnValue('library'));
  162. $installer
  163. ->expects($this->once())
  164. ->method('uninstall')
  165. ->with($this->repository, $package);
  166. $installer
  167. ->expects($this->once())
  168. ->method('supports')
  169. ->with('library')
  170. ->will($this->returnValue(true));
  171. $manager->uninstall($this->repository, $operation);
  172. }
  173. private function createInstallerMock()
  174. {
  175. return $this->getMockBuilder('Composer\Installer\InstallerInterface')
  176. ->getMock();
  177. }
  178. private function createPackageMock()
  179. {
  180. return $this->getMockBuilder('Composer\Package\PackageInterface')
  181. ->getMock();
  182. }
  183. }