GitDownloaderTest.php 16 KB

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