GitDownloaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 remote add composer 'https://example.com/composer/composer' && git fetch composer");
  45. $processExecutor->expects($this->at(0))
  46. ->method('execute')
  47. ->with($this->equalTo($expectedGitCommand))
  48. ->will($this->returnValue(0));
  49. $processExecutor->expects($this->at(1))
  50. ->method('execute')
  51. ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
  52. ->will($this->returnValue(0));
  53. $downloader = $this->getDownloaderMock(null, $processExecutor);
  54. $downloader->download($packageMock, 'composerPath');
  55. }
  56. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  57. {
  58. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  59. $packageMock->expects($this->any())
  60. ->method('getSourceReference')
  61. ->will($this->returnValue('ref'));
  62. $packageMock->expects($this->any())
  63. ->method('getSourceUrl')
  64. ->will($this->returnValue('https://github.com/composer/composer'));
  65. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  66. $expectedGitCommand = $this->getCmd("git clone 'git://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'git://github.com/composer/composer' && git fetch composer");
  67. $processExecutor->expects($this->at(0))
  68. ->method('execute')
  69. ->with($this->equalTo($expectedGitCommand))
  70. ->will($this->returnValue(1));
  71. $expectedGitCommand = $this->getCmd("git clone 'https://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://github.com/composer/composer' && git fetch composer");
  72. $processExecutor->expects($this->at(2))
  73. ->method('execute')
  74. ->with($this->equalTo($expectedGitCommand))
  75. ->will($this->returnValue(1));
  76. $expectedGitCommand = $this->getCmd("git clone 'http://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'http://github.com/composer/composer' && git fetch composer");
  77. $processExecutor->expects($this->at(4))
  78. ->method('execute')
  79. ->with($this->equalTo($expectedGitCommand))
  80. ->will($this->returnValue(0));
  81. $expectedGitCommand = $this->getCmd("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  82. $processExecutor->expects($this->at(5))
  83. ->method('execute')
  84. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo('composerPath'))
  85. ->will($this->returnValue(0));
  86. $processExecutor->expects($this->at(6))
  87. ->method('execute')
  88. ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
  89. ->will($this->returnValue(0));
  90. $downloader = $this->getDownloaderMock(null, $processExecutor);
  91. $downloader->download($packageMock, 'composerPath');
  92. }
  93. /**
  94. * @expectedException \RuntimeException
  95. */
  96. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  97. {
  98. $expectedGitCommand = $this->getCmd("git clone 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://example.com/composer/composer' && git fetch composer");
  99. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  100. $packageMock->expects($this->any())
  101. ->method('getSourceReference')
  102. ->will($this->returnValue('ref'));
  103. $packageMock->expects($this->any())
  104. ->method('getSourceUrl')
  105. ->will($this->returnValue('https://example.com/composer/composer'));
  106. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  107. $processExecutor->expects($this->at(0))
  108. ->method('execute')
  109. ->with($this->equalTo($expectedGitCommand))
  110. ->will($this->returnValue(1));
  111. $downloader = $this->getDownloaderMock(null, $processExecutor);
  112. $downloader->download($packageMock, 'composerPath');
  113. }
  114. /**
  115. * @expectedException \InvalidArgumentException
  116. */
  117. public function testUpdateforPackageWithoutSourceReference()
  118. {
  119. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  120. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  121. $sourcePackageMock->expects($this->once())
  122. ->method('getSourceReference')
  123. ->will($this->returnValue(null));
  124. $downloader = $this->getDownloaderMock();
  125. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  126. }
  127. public function testUpdate()
  128. {
  129. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  130. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  131. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  132. $packageMock->expects($this->any())
  133. ->method('getSourceReference')
  134. ->will($this->returnValue('ref'));
  135. $packageMock->expects($this->any())
  136. ->method('getSourceUrl')
  137. ->will($this->returnValue('https://github.com/composer/composer'));
  138. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  139. $processExecutor->expects($this->at(0))
  140. ->method('execute')
  141. ->with($this->equalTo($expectedGitResetCommand))
  142. ->will($this->returnValue(0));
  143. $processExecutor->expects($this->at(1))
  144. ->method('execute')
  145. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote -v")))
  146. ->will($this->returnValue(0));
  147. $processExecutor->expects($this->at(2))
  148. ->method('execute')
  149. ->with($this->equalTo($expectedGitUpdateCommand))
  150. ->will($this->returnValue(0));
  151. $processExecutor->expects($this->at(3))
  152. ->method('execute')
  153. ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
  154. ->will($this->returnValue(0));
  155. $downloader = $this->getDownloaderMock(null, $processExecutor);
  156. $downloader->update($packageMock, $packageMock, 'composerPath');
  157. }
  158. /**
  159. * @expectedException \RuntimeException
  160. */
  161. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  162. {
  163. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  164. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  165. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  166. $packageMock->expects($this->any())
  167. ->method('getSourceReference')
  168. ->will($this->returnValue('ref'));
  169. $packageMock->expects($this->any())
  170. ->method('getSourceUrl')
  171. ->will($this->returnValue('https://github.com/composer/composer'));
  172. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  173. $processExecutor->expects($this->at(0))
  174. ->method('execute')
  175. ->with($this->equalTo($expectedGitResetCommand))
  176. ->will($this->returnValue(0));
  177. $processExecutor->expects($this->at(1))
  178. ->method('execute')
  179. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote -v")))
  180. ->will($this->returnValue(0));
  181. $processExecutor->expects($this->at(2))
  182. ->method('execute')
  183. ->with($this->equalTo($expectedGitUpdateCommand))
  184. ->will($this->returnValue(1));
  185. $downloader = $this->getDownloaderMock(null, $processExecutor);
  186. $downloader->update($packageMock, $packageMock, 'composerPath');
  187. }
  188. public function testRemove()
  189. {
  190. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  191. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  192. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  193. $processExecutor->expects($this->any())
  194. ->method('execute')
  195. ->with($this->equalTo($expectedGitResetCommand))
  196. ->will($this->returnValue(0));
  197. $filesystem = $this->getMock('Composer\Util\Filesystem');
  198. $filesystem->expects($this->any())
  199. ->method('removeDirectory')
  200. ->with($this->equalTo('composerPath'))
  201. ->will($this->returnValue(true));
  202. $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem);
  203. $downloader->remove($packageMock, 'composerPath');
  204. }
  205. public function testGetInstallationSource()
  206. {
  207. $downloader = $this->getDownloaderMock();
  208. $this->assertEquals('source', $downloader->getInstallationSource());
  209. }
  210. private function getCmd($cmd)
  211. {
  212. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  213. return strtr($cmd, "'", '"');
  214. }
  215. return $cmd;
  216. }
  217. }