InstallationManagerTest.php 6.8 KB

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