PluginInstallerTest.php 6.0 KB

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