GitDownloaderTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  37. $packageMock->expects($this->any())
  38. ->method('getSourceReference')
  39. ->will($this->returnValue('ref'));
  40. $packageMock->expects($this->any())
  41. ->method('getSourceUrl')
  42. ->will($this->returnValue('https://example.com/composer/composer'));
  43. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  44. $expectedGitCommand = $this->getCmd("git clone 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref'");
  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 testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  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->any())
  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. $expectedGitCommand = $this->getCmd("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  78. $processExecutor->expects($this->at(3))
  79. ->method('execute')
  80. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo('composerPath'))
  81. ->will($this->returnValue(0));
  82. $downloader = $this->getDownloaderMock(null, $processExecutor);
  83. $downloader->download($packageMock, 'composerPath');
  84. }
  85. /**
  86. * @expectedException \RuntimeException
  87. */
  88. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  89. {
  90. $expectedGitCommand = $this->getCmd("git clone 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref'");
  91. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  92. $packageMock->expects($this->any())
  93. ->method('getSourceReference')
  94. ->will($this->returnValue('ref'));
  95. $packageMock->expects($this->any())
  96. ->method('getSourceUrl')
  97. ->will($this->returnValue('https://example.com/composer/composer'));
  98. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  99. $processExecutor->expects($this->once())
  100. ->method('execute')
  101. ->with($this->equalTo($expectedGitCommand))
  102. ->will($this->returnValue(1));
  103. $downloader = $this->getDownloaderMock(null, $processExecutor);
  104. $downloader->download($packageMock, 'composerPath');
  105. }
  106. /**
  107. * @expectedException \InvalidArgumentException
  108. */
  109. public function testUpdateforPackageWithoutSourceReference()
  110. {
  111. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  112. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  113. $sourcePackageMock->expects($this->once())
  114. ->method('getSourceReference')
  115. ->will($this->returnValue(null));
  116. $downloader = $this->getDownloaderMock();
  117. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  118. }
  119. public function testUpdate()
  120. {
  121. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url origin 'git://github.com/composer/composer' && git fetch origin && git fetch --tags origin && git checkout 'ref' && git reset --hard 'ref'");
  122. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain");
  123. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  124. $packageMock->expects($this->any())
  125. ->method('getSourceReference')
  126. ->will($this->returnValue('ref'));
  127. $packageMock->expects($this->any())
  128. ->method('getSourceUrl')
  129. ->will($this->returnValue('https://github.com/composer/composer'));
  130. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  131. $processExecutor->expects($this->at(0))
  132. ->method('execute')
  133. ->with($this->equalTo($expectedGitResetCommand))
  134. ->will($this->returnValue(0));
  135. $processExecutor->expects($this->at(1))
  136. ->method('execute')
  137. ->with($this->equalTo($expectedGitUpdateCommand))
  138. ->will($this->returnValue(0));
  139. $downloader = $this->getDownloaderMock(null, $processExecutor);
  140. $downloader->update($packageMock, $packageMock, 'composerPath');
  141. }
  142. /**
  143. * @expectedException \RuntimeException
  144. */
  145. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  146. {
  147. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url origin 'git://github.com/composer/composer' && git fetch origin && git fetch --tags origin && git checkout 'ref' && git reset --hard 'ref'");
  148. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain");
  149. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  150. $packageMock->expects($this->any())
  151. ->method('getSourceReference')
  152. ->will($this->returnValue('ref'));
  153. $packageMock->expects($this->any())
  154. ->method('getSourceUrl')
  155. ->will($this->returnValue('https://github.com/composer/composer'));
  156. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  157. $processExecutor->expects($this->at(0))
  158. ->method('execute')
  159. ->with($this->equalTo($expectedGitResetCommand))
  160. ->will($this->returnValue(0));
  161. $processExecutor->expects($this->at(1))
  162. ->method('execute')
  163. ->with($this->equalTo($expectedGitUpdateCommand))
  164. ->will($this->returnValue(1));
  165. $downloader = $this->getDownloaderMock(null, $processExecutor);
  166. $downloader->update($packageMock, $packageMock, 'composerPath');
  167. }
  168. public function testRemove()
  169. {
  170. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain");
  171. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  172. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  173. $processExecutor->expects($this->any())
  174. ->method('execute')
  175. ->with($this->equalTo($expectedGitResetCommand))
  176. ->will($this->returnValue(0));
  177. $filesystem = $this->getMock('Composer\Util\Filesystem');
  178. $filesystem->expects($this->any())
  179. ->method('removeDirectory')
  180. ->with($this->equalTo('composerPath'));
  181. $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem);
  182. $downloader->remove($packageMock, 'composerPath');
  183. }
  184. public function testGetInstallationSource()
  185. {
  186. $downloader = $this->getDownloaderMock();
  187. $this->assertEquals('source', $downloader->getInstallationSource());
  188. }
  189. private function getCmd($cmd)
  190. {
  191. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  192. return strtr($cmd, "'", '"');
  193. }
  194. return $cmd;
  195. }
  196. }