GitDownloaderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. use Composer\Config;
  14. class GitDownloaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
  17. {
  18. $io = $io ?: $this->getMock('Composer\IO\IOInterface');
  19. $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
  20. $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
  21. if (!$config) {
  22. $config = $this->getMock('Composer\Config');
  23. $config->expects($this->any())
  24. ->method('has')
  25. ->will($this->returnValue(false));
  26. }
  27. return new GitDownloader($io, $config, $executor, $filesystem);
  28. }
  29. /**
  30. * @expectedException \InvalidArgumentException
  31. */
  32. public function testDownloadForPackageWithoutSourceReference()
  33. {
  34. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  35. $packageMock->expects($this->once())
  36. ->method('getSourceReference')
  37. ->will($this->returnValue(null));
  38. $downloader = $this->getDownloaderMock();
  39. $downloader->download($packageMock, '/path');
  40. }
  41. public function testDownload()
  42. {
  43. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  44. $packageMock->expects($this->any())
  45. ->method('getSourceReference')
  46. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  47. $packageMock->expects($this->any())
  48. ->method('getSourceUrl')
  49. ->will($this->returnValue('https://example.com/composer/composer'));
  50. $packageMock->expects($this->any())
  51. ->method('getPrettyVersion')
  52. ->will($this->returnValue('dev-master'));
  53. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  54. $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");
  55. $processExecutor->expects($this->at(0))
  56. ->method('execute')
  57. ->with($this->equalTo($expectedGitCommand))
  58. ->will($this->returnValue(0));
  59. $processExecutor->expects($this->at(1))
  60. ->method('execute')
  61. ->with($this->equalTo($this->getCmd("git branch -r")), $this->equalTo(null), $this->equalTo('composerPath'))
  62. ->will($this->returnValue(0));
  63. $processExecutor->expects($this->at(2))
  64. ->method('execute')
  65. ->with($this->equalTo($this->getCmd("git checkout 'master'")), $this->equalTo(null), $this->equalTo('composerPath'))
  66. ->will($this->returnValue(0));
  67. $processExecutor->expects($this->at(3))
  68. ->method('execute')
  69. ->with($this->equalTo($this->getCmd("git reset --hard '1234567890123456789012345678901234567890'")), $this->equalTo(null), $this->equalTo('composerPath'))
  70. ->will($this->returnValue(0));
  71. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  72. $downloader->download($packageMock, 'composerPath');
  73. }
  74. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  75. {
  76. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  77. $packageMock->expects($this->any())
  78. ->method('getSourceReference')
  79. ->will($this->returnValue('ref'));
  80. $packageMock->expects($this->any())
  81. ->method('getSourceUrl')
  82. ->will($this->returnValue('https://github.com/composer/composer'));
  83. $packageMock->expects($this->any())
  84. ->method('getPrettyVersion')
  85. ->will($this->returnValue('1.0.0'));
  86. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  87. $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");
  88. $processExecutor->expects($this->at(0))
  89. ->method('execute')
  90. ->with($this->equalTo($expectedGitCommand))
  91. ->will($this->returnValue(1));
  92. $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");
  93. $processExecutor->expects($this->at(2))
  94. ->method('execute')
  95. ->with($this->equalTo($expectedGitCommand))
  96. ->will($this->returnValue(1));
  97. $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");
  98. $processExecutor->expects($this->at(4))
  99. ->method('execute')
  100. ->with($this->equalTo($expectedGitCommand))
  101. ->will($this->returnValue(0));
  102. $expectedGitCommand = $this->getCmd("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  103. $processExecutor->expects($this->at(5))
  104. ->method('execute')
  105. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo('composerPath'))
  106. ->will($this->returnValue(0));
  107. $processExecutor->expects($this->at(6))
  108. ->method('execute')
  109. ->with($this->equalTo('git branch -r'))
  110. ->will($this->returnValue(0));
  111. $processExecutor->expects($this->at(7))
  112. ->method('execute')
  113. ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
  114. ->will($this->returnValue(0));
  115. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  116. $downloader->download($packageMock, 'composerPath');
  117. }
  118. public function testDownloadUsesCustomVariousProtocolsForGithub()
  119. {
  120. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  121. $packageMock->expects($this->any())
  122. ->method('getSourceReference')
  123. ->will($this->returnValue('ref'));
  124. $packageMock->expects($this->any())
  125. ->method('getSourceUrl')
  126. ->will($this->returnValue('https://github.com/composer/composer'));
  127. $packageMock->expects($this->any())
  128. ->method('getPrettyVersion')
  129. ->will($this->returnValue('1.0.0'));
  130. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  131. $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");
  132. $processExecutor->expects($this->at(0))
  133. ->method('execute')
  134. ->with($this->equalTo($expectedGitCommand))
  135. ->will($this->returnValue(0));
  136. $processExecutor->expects($this->exactly(4))
  137. ->method('execute')
  138. ->will($this->returnValue(0));
  139. $config = new Config();
  140. $config->merge(array('config' => array('github-protocols' => array('http'))));
  141. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  142. $downloader->download($packageMock, 'composerPath');
  143. }
  144. /**
  145. * @expectedException \RuntimeException
  146. */
  147. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  148. {
  149. $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");
  150. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  151. $packageMock->expects($this->any())
  152. ->method('getSourceReference')
  153. ->will($this->returnValue('ref'));
  154. $packageMock->expects($this->any())
  155. ->method('getSourceUrl')
  156. ->will($this->returnValue('https://example.com/composer/composer'));
  157. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  158. $processExecutor->expects($this->at(0))
  159. ->method('execute')
  160. ->with($this->equalTo($expectedGitCommand))
  161. ->will($this->returnValue(1));
  162. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  163. $downloader->download($packageMock, 'composerPath');
  164. }
  165. /**
  166. * @expectedException \InvalidArgumentException
  167. */
  168. public function testUpdateforPackageWithoutSourceReference()
  169. {
  170. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  171. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  172. $sourcePackageMock->expects($this->once())
  173. ->method('getSourceReference')
  174. ->will($this->returnValue(null));
  175. $downloader = $this->getDownloaderMock();
  176. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  177. }
  178. public function testUpdate()
  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. $packageMock->expects($this->any())
  190. ->method('getPrettyVersion')
  191. ->will($this->returnValue('1.0.0'));
  192. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  193. $processExecutor->expects($this->at(0))
  194. ->method('execute')
  195. ->with($this->equalTo($expectedGitResetCommand))
  196. ->will($this->returnValue(0));
  197. $processExecutor->expects($this->at(1))
  198. ->method('execute')
  199. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote -v")))
  200. ->will($this->returnValue(0));
  201. $processExecutor->expects($this->at(2))
  202. ->method('execute')
  203. ->with($this->equalTo($expectedGitUpdateCommand))
  204. ->will($this->returnValue(0));
  205. $processExecutor->expects($this->at(3))
  206. ->method('execute')
  207. ->with($this->equalTo('git branch -r'))
  208. ->will($this->returnValue(0));
  209. $processExecutor->expects($this->at(4))
  210. ->method('execute')
  211. ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath'))
  212. ->will($this->returnValue(0));
  213. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  214. $downloader->update($packageMock, $packageMock, 'composerPath');
  215. }
  216. /**
  217. * @expectedException \RuntimeException
  218. */
  219. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  220. {
  221. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  222. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  223. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  224. $packageMock->expects($this->any())
  225. ->method('getSourceReference')
  226. ->will($this->returnValue('ref'));
  227. $packageMock->expects($this->any())
  228. ->method('getSourceUrl')
  229. ->will($this->returnValue('https://github.com/composer/composer'));
  230. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  231. $processExecutor->expects($this->at(0))
  232. ->method('execute')
  233. ->with($this->equalTo($expectedGitResetCommand))
  234. ->will($this->returnValue(0));
  235. $processExecutor->expects($this->at(1))
  236. ->method('execute')
  237. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote -v")))
  238. ->will($this->returnValue(0));
  239. $processExecutor->expects($this->at(2))
  240. ->method('execute')
  241. ->with($this->equalTo($expectedGitUpdateCommand))
  242. ->will($this->returnValue(1));
  243. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  244. $downloader->update($packageMock, $packageMock, 'composerPath');
  245. }
  246. public function testRemove()
  247. {
  248. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  249. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  250. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  251. $processExecutor->expects($this->any())
  252. ->method('execute')
  253. ->with($this->equalTo($expectedGitResetCommand))
  254. ->will($this->returnValue(0));
  255. $filesystem = $this->getMock('Composer\Util\Filesystem');
  256. $filesystem->expects($this->any())
  257. ->method('removeDirectory')
  258. ->with($this->equalTo('composerPath'))
  259. ->will($this->returnValue(true));
  260. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  261. $downloader->remove($packageMock, 'composerPath');
  262. }
  263. public function testGetInstallationSource()
  264. {
  265. $downloader = $this->getDownloaderMock();
  266. $this->assertEquals('source', $downloader->getInstallationSource());
  267. }
  268. private function getCmd($cmd)
  269. {
  270. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  271. return strtr($cmd, "'", '"');
  272. }
  273. return $cmd;
  274. }
  275. }