InstallationManagerTest.php 7.2 KB

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