GitDownloaderTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\Downloader;
  12. use Composer\Downloader\GitDownloader;
  13. class GitDownloaderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @expectedException \InvalidArgumentException
  17. */
  18. public function testDownloadForPackageWithoutSourceReference()
  19. {
  20. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  21. $packageMock->expects($this->once())
  22. ->method('getSourceReference')
  23. ->will($this->returnValue(null));
  24. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'));
  25. $downloader->download($packageMock, '/path');
  26. }
  27. public function testDownload()
  28. {
  29. $expectedGitCommand = $this->getCmd('git clone \'https://github.com/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\'');
  30. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  31. $packageMock->expects($this->any())
  32. ->method('getSourceReference')
  33. ->will($this->returnValue('ref'));
  34. $packageMock->expects($this->once())
  35. ->method('getSourceUrl')
  36. ->will($this->returnValue('https://github.com/l3l0/composer'));
  37. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  38. $processExecutor->expects($this->once())
  39. ->method('execute')
  40. ->with($this->equalTo($expectedGitCommand))
  41. ->will($this->returnValue(0));
  42. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
  43. $downloader->download($packageMock, 'composerPath');
  44. }
  45. /**
  46. * @expectedException \RuntimeException
  47. */
  48. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  49. {
  50. $expectedGitCommand = $this->getCmd('git clone \'https://github.com/l3l0/composer\' \'composerPath\' && cd \'composerPath\' && git checkout \'ref\' && git reset --hard \'ref\'');
  51. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  52. $packageMock->expects($this->any())
  53. ->method('getSourceReference')
  54. ->will($this->returnValue('ref'));
  55. $packageMock->expects($this->once())
  56. ->method('getSourceUrl')
  57. ->will($this->returnValue('https://github.com/l3l0/composer'));
  58. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  59. $processExecutor->expects($this->once())
  60. ->method('execute')
  61. ->with($this->equalTo($expectedGitCommand))
  62. ->will($this->returnValue(1));
  63. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
  64. $downloader->download($packageMock, 'composerPath');
  65. }
  66. /**
  67. * @expectedException \InvalidArgumentException
  68. */
  69. public function testUpdateforPackageWithoutSourceReference()
  70. {
  71. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  72. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  73. $sourcePackageMock->expects($this->once())
  74. ->method('getSourceReference')
  75. ->will($this->returnValue(null));
  76. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'));
  77. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  78. }
  79. public function testUpdate()
  80. {
  81. $expectedGitUpdateCommand = $this->getCmd('cd \'composerPath\' && git fetch && git checkout \'ref\' && git reset --hard \'ref\'');
  82. $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain');
  83. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  84. $packageMock->expects($this->any())
  85. ->method('getSourceReference')
  86. ->will($this->returnValue('ref'));
  87. $packageMock->expects($this->any())
  88. ->method('getSourceUrl')
  89. ->will($this->returnValue('https://github.com/l3l0/composer'));
  90. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  91. $processExecutor->expects($this->at(0))
  92. ->method('execute')
  93. ->with($this->equalTo($expectedGitResetCommand))
  94. ->will($this->returnValue(0));
  95. $processExecutor->expects($this->at(1))
  96. ->method('execute')
  97. ->with($this->equalTo($expectedGitUpdateCommand))
  98. ->will($this->returnValue(0));
  99. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
  100. $downloader->update($packageMock, $packageMock, 'composerPath');
  101. }
  102. /**
  103. * @expectedException \RuntimeException
  104. */
  105. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  106. {
  107. $expectedGitUpdateCommand = $this->getCmd('cd \'composerPath\' && git fetch && git checkout \'ref\' && git reset --hard \'ref\'');
  108. $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain');
  109. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  110. $packageMock->expects($this->any())
  111. ->method('getSourceReference')
  112. ->will($this->returnValue('ref'));
  113. $packageMock->expects($this->any())
  114. ->method('getSourceUrl')
  115. ->will($this->returnValue('https://github.com/l3l0/composer'));
  116. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  117. $processExecutor->expects($this->at(0))
  118. ->method('execute')
  119. ->with($this->equalTo($expectedGitResetCommand))
  120. ->will($this->returnValue(0));
  121. $processExecutor->expects($this->at(1))
  122. ->method('execute')
  123. ->with($this->equalTo($expectedGitUpdateCommand))
  124. ->will($this->returnValue(1));
  125. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor);
  126. $downloader->update($packageMock, $packageMock, 'composerPath');
  127. }
  128. public function testRemove()
  129. {
  130. $expectedGitResetCommand = $this->getCmd('cd \'composerPath\' && git status --porcelain');
  131. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  132. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  133. $processExecutor->expects($this->any())
  134. ->method('execute')
  135. ->with($this->equalTo($expectedGitResetCommand))
  136. ->will($this->returnValue(0));
  137. $filesystem = $this->getMock('Composer\Util\Filesystem');
  138. $filesystem->expects($this->any())
  139. ->method('removeDirectory')
  140. ->with($this->equalTo('composerPath'));
  141. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'), $processExecutor, $filesystem);
  142. $downloader->remove($packageMock, 'composerPath');
  143. }
  144. public function testGetInstallationSource()
  145. {
  146. $downloader = new GitDownloader($this->getMock('Composer\IO\IOInterface'));
  147. $this->assertEquals('source', $downloader->getInstallationSource());
  148. }
  149. private function getCmd($cmd)
  150. {
  151. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  152. return strtr($cmd, "'", '"');
  153. }
  154. return $cmd;
  155. }
  156. }