GitDownloaderTest.php 12 KB

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