InstallationManagerTest.php 5.3 KB

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