InstallationManagerTest.php 6.3 KB

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