GitDownloaderTest.php 9.3 KB

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