PluginInstallerTest.php 5.4 KB

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