PluginInstallerTest.php 5.8 KB

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