PluginInstallerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Composer;
  13. use Composer\Config;
  14. use Composer\Installer\PluginInstaller;
  15. use Composer\Package\Loader\JsonLoader;
  16. use Composer\Package\Loader\ArrayLoader;
  17. use Composer\Package\PackageInterface;
  18. use Composer\Autoload\AutoloadGenerator;
  19. class PluginInstallerTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected $composer;
  22. protected $packages;
  23. protected $im;
  24. protected $pm;
  25. protected $repository;
  26. protected $io;
  27. protected $autoloadGenerator;
  28. protected function setUp()
  29. {
  30. $loader = new JsonLoader(new ArrayLoader());
  31. $this->packages = array();
  32. for ($i = 1; $i <= 4; $i++) {
  33. $this->packages[] = $loader->load(__DIR__.'/Fixtures/installer-v'.$i.'/composer.json');
  34. }
  35. $dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->pm = $this->getMockBuilder('Composer\Plugin\PluginManager')
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
  45. $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $rm->expects($this->any())
  49. ->method('getLocalRepository')
  50. ->will($this->returnValue($this->repository));
  51. $this->io = $this->getMock('Composer\IO\IOInterface');
  52. $dispatcher = $this->getMockBuilder('Composer\Script\EventDispatcher')->disableOriginalConstructor()->getMock();
  53. $this->autoloadGenerator = new AutoloadGenerator($dispatcher);
  54. $this->composer = new Composer();
  55. $config = new Config();
  56. $this->composer->setConfig($config);
  57. $this->composer->setDownloadManager($dm);
  58. $this->composer->setInstallationManager($this->im);
  59. $this->composer->setPluginManager($this->pm);
  60. $this->composer->setRepositoryManager($rm);
  61. $this->composer->setAutoloadGenerator($this->autoloadGenerator);
  62. $config->merge(array(
  63. 'config' => array(
  64. 'vendor-dir' => __DIR__.'/Fixtures/',
  65. 'bin-dir' => __DIR__.'/Fixtures/bin',
  66. ),
  67. ));
  68. }
  69. public function testInstallNewPlugin()
  70. {
  71. $this->repository
  72. ->expects($this->once())
  73. ->method('getPackages')
  74. ->will($this->returnValue(array()));
  75. $installer = new PluginInstallerMock($this->io, $this->composer);
  76. $test = $this;
  77. $this->pm
  78. ->expects($this->once())
  79. ->method('addPlugin')
  80. ->will($this->returnCallback(function ($installer) use ($test) {
  81. $test->assertEquals('installer-v1', $installer->version);
  82. }));
  83. $installer->install($this->repository, $this->packages[0]);
  84. }
  85. public function testInstallMultiplePlugins()
  86. {
  87. $this->repository
  88. ->expects($this->once())
  89. ->method('getPackages')
  90. ->will($this->returnValue(array()));
  91. $installer = new PluginInstallerMock($this->io, $this->composer);
  92. $test = $this;
  93. $this->pm
  94. ->expects($this->at(0))
  95. ->method('addPlugin')
  96. ->will($this->returnCallback(function ($plugin) use ($test) {
  97. $test->assertEquals('plugin1', $plugin->name);
  98. $test->assertEquals('installer-v4', $plugin->version);
  99. }));
  100. $this->pm
  101. ->expects($this->at(1))
  102. ->method('addPlugin')
  103. ->will($this->returnCallback(function ($plugin) use ($test) {
  104. $test->assertEquals('plugin2', $plugin->name);
  105. $test->assertEquals('installer-v4', $plugin->version);
  106. }));
  107. $installer->install($this->repository, $this->packages[3]);
  108. }
  109. public function testUpgradeWithNewClassName()
  110. {
  111. $this->repository
  112. ->expects($this->once())
  113. ->method('getPackages')
  114. ->will($this->returnValue(array($this->packages[0])));
  115. $this->repository
  116. ->expects($this->exactly(2))
  117. ->method('hasPackage')
  118. ->will($this->onConsecutiveCalls(true, false));
  119. $installer = new PluginInstallerMock($this->io, $this->composer);
  120. $test = $this;
  121. $this->pm
  122. ->expects($this->once())
  123. ->method('addPlugin')
  124. ->will($this->returnCallback(function ($plugin) use ($test) {
  125. $test->assertEquals('installer-v2', $plugin->version);
  126. }));
  127. $installer->update($this->repository, $this->packages[0], $this->packages[1]);
  128. }
  129. public function testUpgradeWithSameClassName()
  130. {
  131. $this->repository
  132. ->expects($this->once())
  133. ->method('getPackages')
  134. ->will($this->returnValue(array($this->packages[1])));
  135. $this->repository
  136. ->expects($this->exactly(2))
  137. ->method('hasPackage')
  138. ->will($this->onConsecutiveCalls(true, false));
  139. $installer = new PluginInstallerMock($this->io, $this->composer);
  140. $test = $this;
  141. $this->pm
  142. ->expects($this->once())
  143. ->method('addPlugin')
  144. ->will($this->returnCallback(function ($plugin) use ($test) {
  145. $test->assertEquals('installer-v3', $plugin->version);
  146. }));
  147. $installer->update($this->repository, $this->packages[1], $this->packages[2]);
  148. }
  149. }
  150. class PluginInstallerMock extends PluginInstaller
  151. {
  152. public function getInstallPath(PackageInterface $package)
  153. {
  154. $version = $package->getVersion();
  155. return __DIR__.'/Fixtures/installer-v'.$version[0].'/';
  156. }
  157. }