PluginInstallerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/plugin-v'.$i.'/composer.json');
  34. }
  35. $dm = $this->getMockBuilder('Composer\Downloader\DownloadManager')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->repository = $this->getMock('Composer\Repository\InstalledRepositoryInterface');
  39. $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $rm->expects($this->any())
  43. ->method('getLocalRepository')
  44. ->will($this->returnValue($this->repository));
  45. $this->io = $this->getMock('Composer\IO\IOInterface');
  46. $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
  47. $this->autoloadGenerator = new AutoloadGenerator($dispatcher);
  48. $this->composer = new Composer();
  49. $config = new Config();
  50. $this->composer->setConfig($config);
  51. $this->composer->setDownloadManager($dm);
  52. $this->composer->setRepositoryManager($rm);
  53. $this->composer->setAutoloadGenerator($this->autoloadGenerator);
  54. $this->pm = new \Composer\Plugin\PluginManager($this->composer, $this->io);
  55. $this->composer->setPluginManager($this->pm);
  56. $config->merge(array(
  57. 'config' => array(
  58. 'vendor-dir' => __DIR__.'/Fixtures/',
  59. 'home' => __DIR__.'/Fixtures',
  60. 'bin-dir' => __DIR__.'/Fixtures/bin',
  61. ),
  62. ));
  63. }
  64. public function testInstallNewPlugin()
  65. {
  66. $this->repository
  67. ->expects($this->exactly(2))
  68. ->method('getPackages')
  69. ->will($this->returnValue(array()));
  70. $installer = new PluginInstaller($this->io, $this->composer);
  71. $this->pm->loadInstalledPlugins();
  72. $installer->install($this->repository, $this->packages[0]);
  73. $plugins = $this->pm->getPlugins();
  74. $this->assertEquals('installer-v1', $plugins[0]->version);
  75. }
  76. public function testInstallMultiplePlugins()
  77. {
  78. $this->repository
  79. ->expects($this->exactly(2))
  80. ->method('getPackages')
  81. ->will($this->returnValue(array()));
  82. $installer = new PluginInstaller($this->io, $this->composer);
  83. $this->pm->loadInstalledPlugins();
  84. $installer->install($this->repository, $this->packages[3]);
  85. $plugins = $this->pm->getPlugins();
  86. $this->assertEquals('plugin1', $plugins[0]->name);
  87. $this->assertEquals('installer-v4', $plugins[0]->version);
  88. $this->assertEquals('plugin2', $plugins[1]->name);
  89. $this->assertEquals('installer-v4', $plugins[1]->version);
  90. }
  91. public function testUpgradeWithNewClassName()
  92. {
  93. $this->repository
  94. ->expects($this->exactly(3))
  95. ->method('getPackages')
  96. ->will($this->returnValue(array($this->packages[0])));
  97. $this->repository
  98. ->expects($this->exactly(2))
  99. ->method('hasPackage')
  100. ->will($this->onConsecutiveCalls(true, false));
  101. $installer = new PluginInstaller($this->io, $this->composer);
  102. $this->pm->loadInstalledPlugins();
  103. $installer->update($this->repository, $this->packages[0], $this->packages[1]);
  104. $plugins = $this->pm->getPlugins();
  105. $this->assertEquals('installer-v2', $plugins[1]->version);
  106. }
  107. public function testUpgradeWithSameClassName()
  108. {
  109. $this->repository
  110. ->expects($this->exactly(3))
  111. ->method('getPackages')
  112. ->will($this->returnValue(array($this->packages[1])));
  113. $this->repository
  114. ->expects($this->exactly(2))
  115. ->method('hasPackage')
  116. ->will($this->onConsecutiveCalls(true, false));
  117. $installer = new PluginInstaller($this->io, $this->composer);
  118. $this->pm->loadInstalledPlugins();
  119. $installer->update($this->repository, $this->packages[1], $this->packages[2]);
  120. $plugins = $this->pm->getPlugins();
  121. $this->assertEquals('installer-v3', $plugins[1]->version);
  122. }
  123. }