GitDownloaderTest.php 16 KB

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