InstallationManagerTest.php 7.6 KB

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