GitDownloaderTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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->winCompat("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->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  62. ->will($this->returnValue(0));
  63. $processExecutor->expects($this->at(2))
  64. ->method('execute')
  65. ->with($this->equalTo($this->winCompat("git checkout 'master'")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  66. ->will($this->returnValue(0));
  67. $processExecutor->expects($this->at(3))
  68. ->method('execute')
  69. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890'")), $this->equalTo(null), $this->equalTo($this->winCompat('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->winCompat("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->winCompat("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->winCompat("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->winCompat("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($this->winCompat('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->winCompat("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  114. ->will($this->returnValue(0));
  115. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  116. $downloader->download($packageMock, 'composerPath');
  117. }
  118. public function pushUrlProvider()
  119. {
  120. return array(
  121. array('git', 'git@github.com:composer/composer.git'),
  122. array('https', 'https://github.com/composer/composer'),
  123. array('http', 'https://github.com/composer/composer')
  124. );
  125. }
  126. /**
  127. * @dataProvider pushUrlProvider
  128. */
  129. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocol, $pushUrl)
  130. {
  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. $packageMock->expects($this->any())
  139. ->method('getPrettyVersion')
  140. ->will($this->returnValue('1.0.0'));
  141. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  142. $expectedGitCommand = $this->winCompat("git clone '{$protocol}://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer '{$protocol}://github.com/composer/composer' && git fetch composer");
  143. $processExecutor->expects($this->at(0))
  144. ->method('execute')
  145. ->with($this->equalTo($expectedGitCommand))
  146. ->will($this->returnValue(0));
  147. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  148. $processExecutor->expects($this->at(1))
  149. ->method('execute')
  150. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  151. ->will($this->returnValue(0));
  152. $processExecutor->expects($this->exactly(4))
  153. ->method('execute')
  154. ->will($this->returnValue(0));
  155. $config = new Config();
  156. $config->merge(array('config' => array('github-protocols' => array($protocol))));
  157. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  158. $downloader->download($packageMock, 'composerPath');
  159. }
  160. /**
  161. * @expectedException \RuntimeException
  162. */
  163. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  164. {
  165. $expectedGitCommand = $this->winCompat("git clone 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'https://example.com/composer/composer' && git fetch composer");
  166. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  167. $packageMock->expects($this->any())
  168. ->method('getSourceReference')
  169. ->will($this->returnValue('ref'));
  170. $packageMock->expects($this->any())
  171. ->method('getSourceUrl')
  172. ->will($this->returnValue('https://example.com/composer/composer'));
  173. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  174. $processExecutor->expects($this->at(0))
  175. ->method('execute')
  176. ->with($this->equalTo($expectedGitCommand))
  177. ->will($this->returnValue(1));
  178. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  179. $downloader->download($packageMock, 'composerPath');
  180. }
  181. /**
  182. * @expectedException \InvalidArgumentException
  183. */
  184. public function testUpdateforPackageWithoutSourceReference()
  185. {
  186. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  187. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  188. $sourcePackageMock->expects($this->once())
  189. ->method('getSourceReference')
  190. ->will($this->returnValue(null));
  191. $downloader = $this->getDownloaderMock();
  192. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  193. }
  194. public function testUpdate()
  195. {
  196. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  197. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  198. $packageMock->expects($this->any())
  199. ->method('getSourceReference')
  200. ->will($this->returnValue('ref'));
  201. $packageMock->expects($this->any())
  202. ->method('getSourceUrl')
  203. ->will($this->returnValue('https://github.com/composer/composer'));
  204. $packageMock->expects($this->any())
  205. ->method('getPrettyVersion')
  206. ->will($this->returnValue('1.0.0'));
  207. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  208. $processExecutor->expects($this->at(0))
  209. ->method('execute')
  210. ->with($this->equalTo($this->winCompat("git remote -v")))
  211. ->will($this->returnValue(0));
  212. $processExecutor->expects($this->at(1))
  213. ->method('execute')
  214. ->with($this->equalTo($expectedGitUpdateCommand))
  215. ->will($this->returnValue(0));
  216. $processExecutor->expects($this->at(2))
  217. ->method('execute')
  218. ->with($this->equalTo('git branch -r'))
  219. ->will($this->returnValue(0));
  220. $processExecutor->expects($this->at(3))
  221. ->method('execute')
  222. ->with($this->equalTo($this->winCompat("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  223. ->will($this->returnValue(0));
  224. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  225. $downloader->update($packageMock, $packageMock, 'composerPath');
  226. }
  227. /**
  228. * @expectedException \RuntimeException
  229. */
  230. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  231. {
  232. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  233. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  234. $packageMock->expects($this->any())
  235. ->method('getSourceReference')
  236. ->will($this->returnValue('ref'));
  237. $packageMock->expects($this->any())
  238. ->method('getSourceUrl')
  239. ->will($this->returnValue('https://github.com/composer/composer'));
  240. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  241. $processExecutor->expects($this->at(0))
  242. ->method('execute')
  243. ->with($this->equalTo($this->winCompat("git remote -v")))
  244. ->will($this->returnValue(0));
  245. $processExecutor->expects($this->at(1))
  246. ->method('execute')
  247. ->with($this->equalTo($expectedGitUpdateCommand))
  248. ->will($this->returnValue(1));
  249. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  250. $downloader->update($packageMock, $packageMock, 'composerPath');
  251. }
  252. public function testRemove()
  253. {
  254. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  255. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  256. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  257. $processExecutor->expects($this->any())
  258. ->method('execute')
  259. ->with($this->equalTo($expectedGitResetCommand))
  260. ->will($this->returnValue(0));
  261. $filesystem = $this->getMock('Composer\Util\Filesystem');
  262. $filesystem->expects($this->any())
  263. ->method('removeDirectory')
  264. ->with($this->equalTo('composerPath'))
  265. ->will($this->returnValue(true));
  266. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  267. $downloader->remove($packageMock, 'composerPath');
  268. }
  269. public function testGetInstallationSource()
  270. {
  271. $downloader = $this->getDownloaderMock();
  272. $this->assertEquals('source', $downloader->getInstallationSource());
  273. }
  274. private function winCompat($cmd)
  275. {
  276. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  277. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  278. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  279. return strtr($cmd, "'", '"');
  280. }
  281. return $cmd;
  282. }
  283. }