PluginInstallerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 'bin-dir' => __DIR__.'/Fixtures/bin',
  60. ),
  61. ));
  62. }
  63. public function testInstallNewPlugin()
  64. {
  65. $this->repository
  66. ->expects($this->once())
  67. ->method('getPackages')
  68. ->will($this->returnValue(array()));
  69. $installer = new PluginInstaller($this->io, $this->composer);
  70. $this->pm->loadInstalledPlugins();
  71. $installer->install($this->repository, $this->packages[0]);
  72. $plugins = $this->pm->getPlugins();
  73. $this->assertEquals('installer-v1', $plugins[0]->version);
  74. }
  75. public function testInstallMultiplePlugins()
  76. {
  77. $this->repository
  78. ->expects($this->once())
  79. ->method('getPackages')
  80. ->will($this->returnValue(array()));
  81. $installer = new PluginInstaller($this->io, $this->composer);
  82. $this->pm->loadInstalledPlugins();
  83. $installer->install($this->repository, $this->packages[3]);
  84. $plugins = $this->pm->getPlugins();
  85. $this->assertEquals('plugin1', $plugins[0]->name);
  86. $this->assertEquals('installer-v4', $plugins[0]->version);
  87. $this->assertEquals('plugin2', $plugins[1]->name);
  88. $this->assertEquals('installer-v4', $plugins[1]->version);
  89. }
  90. public function testUpgradeWithNewClassName()
  91. {
  92. $this->repository
  93. ->expects($this->once())
  94. ->method('getPackages')
  95. ->will($this->returnValue(array($this->packages[0])));
  96. $this->repository
  97. ->expects($this->exactly(2))
  98. ->method('hasPackage')
  99. ->will($this->onConsecutiveCalls(true, false));
  100. $installer = new PluginInstaller($this->io, $this->composer);
  101. $this->pm->loadInstalledPlugins();
  102. $installer->update($this->repository, $this->packages[0], $this->packages[1]);
  103. $plugins = $this->pm->getPlugins();
  104. $this->assertEquals('installer-v2', $plugins[1]->version);
  105. }
  106. public function testUpgradeWithSameClassName()
  107. {
  108. $this->repository
  109. ->expects($this->once())
  110. ->method('getPackages')
  111. ->will($this->returnValue(array($this->packages[1])));
  112. $this->repository
  113. ->expects($this->exactly(2))
  114. ->method('hasPackage')
  115. ->will($this->onConsecutiveCalls(true, false));
  116. $installer = new PluginInstaller($this->io, $this->composer);
  117. $this->pm->loadInstalledPlugins();
  118. $installer->update($this->repository, $this->packages[1], $this->packages[2]);
  119. $plugins = $this->pm->getPlugins();
  120. $this->assertEquals('installer-v3', $plugins[1]->version);
  121. }
  122. }