InstallerInstallerTest.php 5.6 KB

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