InstallerInstallerTest.php 5.1 KB

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