InstallerInstallerTest.php 5.7 KB

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