GitDownloaderTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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\Test\TestCase;
  15. use Composer\Util\Filesystem;
  16. use Composer\Util\Platform;
  17. use Prophecy\Argument;
  18. class GitDownloaderTest extends TestCase
  19. {
  20. /** @var Filesystem */
  21. private $fs;
  22. /** @var string */
  23. private $workingDir;
  24. protected function setUp()
  25. {
  26. $this->skipIfNotExecutable('git');
  27. $this->fs = new Filesystem;
  28. $this->workingDir = $this->getUniqueTmpDirectory();
  29. }
  30. protected function tearDown()
  31. {
  32. if (is_dir($this->workingDir)) {
  33. $this->fs->removeDirectory($this->workingDir);
  34. }
  35. // reset the static version cache
  36. $refl = new \ReflectionProperty('Composer\Util\Git', 'version');
  37. $refl->setAccessible(true);
  38. $refl->setValue(null, null);
  39. }
  40. protected function setupConfig($config = null)
  41. {
  42. if (!$config) {
  43. $config = new Config();
  44. }
  45. if (!$config->has('home')) {
  46. $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true));
  47. $config->merge(array('config' => array('home' => $tmpDir)));
  48. }
  49. return $config;
  50. }
  51. protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null)
  52. {
  53. $io = $io ?: $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  54. $executor = $executor ?: $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  55. $filesystem = $filesystem ?: $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  56. $config = $this->setupConfig($config);
  57. return new GitDownloader($io, $config, $executor, $filesystem);
  58. }
  59. /**
  60. * @expectedException \InvalidArgumentException
  61. */
  62. public function testDownloadForPackageWithoutSourceReference()
  63. {
  64. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  65. $packageMock->expects($this->once())
  66. ->method('getSourceReference')
  67. ->will($this->returnValue(null));
  68. $downloader = $this->getDownloaderMock();
  69. $downloader->download($packageMock, '/path');
  70. $downloader->prepare('install', $packageMock, '/path');
  71. $downloader->install($packageMock, '/path');
  72. $downloader->cleanup('install', $packageMock, '/path');
  73. }
  74. public function testDownload()
  75. {
  76. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  77. $packageMock->expects($this->any())
  78. ->method('getSourceReference')
  79. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  80. $packageMock->expects($this->any())
  81. ->method('getSourceUrls')
  82. ->will($this->returnValue(array('https://example.com/composer/composer')));
  83. $packageMock->expects($this->any())
  84. ->method('getSourceUrl')
  85. ->will($this->returnValue('https://example.com/composer/composer'));
  86. $packageMock->expects($this->any())
  87. ->method('getPrettyVersion')
  88. ->will($this->returnValue('dev-master'));
  89. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  90. $processExecutor->expects($this->at(0))
  91. ->method('execute')
  92. ->with($this->equalTo($this->winCompat('git --version')))
  93. ->will($this->returnCallback(function ($command, &$output = null) {
  94. $output = 'git version 1.0.0';
  95. return 0;
  96. }));
  97. $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");
  98. $processExecutor->expects($this->at(1))
  99. ->method('execute')
  100. ->with($this->equalTo($expectedGitCommand))
  101. ->will($this->returnValue(0));
  102. $processExecutor->expects($this->at(2))
  103. ->method('execute')
  104. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  105. ->will($this->returnValue(0));
  106. $processExecutor->expects($this->at(3))
  107. ->method('execute')
  108. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  109. ->will($this->returnValue(0));
  110. $processExecutor->expects($this->at(4))
  111. ->method('execute')
  112. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  113. ->will($this->returnValue(0));
  114. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  115. $downloader->download($packageMock, 'composerPath');
  116. $downloader->prepare('install', $packageMock, 'composerPath');
  117. $downloader->install($packageMock, 'composerPath');
  118. $downloader->cleanup('install', $packageMock, 'composerPath');
  119. }
  120. public function testDownloadWithCache()
  121. {
  122. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  123. $packageMock->expects($this->any())
  124. ->method('getSourceReference')
  125. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  126. $packageMock->expects($this->any())
  127. ->method('getSourceUrls')
  128. ->will($this->returnValue(array('https://example.com/composer/composer')));
  129. $packageMock->expects($this->any())
  130. ->method('getSourceUrl')
  131. ->will($this->returnValue('https://example.com/composer/composer'));
  132. $packageMock->expects($this->any())
  133. ->method('getPrettyVersion')
  134. ->will($this->returnValue('dev-master'));
  135. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  136. $processExecutor->expects($this->at(0))
  137. ->method('execute')
  138. ->with($this->equalTo($this->winCompat('git --version')))
  139. ->will($this->returnCallback(function ($command, &$output = null) {
  140. $output = 'git version 2.3.1';
  141. return 0;
  142. }));
  143. $config = new Config;
  144. $this->setupConfig($config);
  145. $cachePath = $config->get('cache-vcs-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', 'https://example.com/composer/composer').'/';
  146. $expectedGitCommand = $this->winCompat(sprintf("git clone --mirror 'https://example.com/composer/composer' '%s'", $cachePath));
  147. $processExecutor->expects($this->at(1))
  148. ->method('execute')
  149. ->with($this->equalTo($expectedGitCommand))
  150. ->will($this->returnCallback(function () use ($cachePath) {
  151. @mkdir($cachePath, 0777, true);
  152. return 0;
  153. }));
  154. $expectedGitCommand = $this->winCompat(sprintf("git clone --no-checkout '%1\$s' 'composerPath' --dissociate --reference '%1\$s' && cd 'composerPath' && git remote set-url origin 'https://example.com/composer/composer' && git remote add composer 'https://example.com/composer/composer'", $cachePath));
  155. $processExecutor->expects($this->at(2))
  156. ->method('execute')
  157. ->with($this->equalTo($expectedGitCommand))
  158. ->will($this->returnValue(0));
  159. $processExecutor->expects($this->at(3))
  160. ->method('execute')
  161. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  162. ->will($this->returnValue(0));
  163. $processExecutor->expects($this->at(4))
  164. ->method('execute')
  165. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  166. ->will($this->returnValue(0));
  167. $processExecutor->expects($this->at(5))
  168. ->method('execute')
  169. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  170. ->will($this->returnValue(0));
  171. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  172. $downloader->download($packageMock, 'composerPath');
  173. $downloader->prepare('install', $packageMock, 'composerPath');
  174. $downloader->install($packageMock, 'composerPath');
  175. $downloader->cleanup('install', $packageMock, 'composerPath');
  176. @rmdir($cachePath);
  177. }
  178. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  179. {
  180. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  181. $packageMock->expects($this->any())
  182. ->method('getSourceReference')
  183. ->will($this->returnValue('ref'));
  184. $packageMock->expects($this->any())
  185. ->method('getSourceUrls')
  186. ->will($this->returnValue(array('https://github.com/mirrors/composer', 'https://github.com/composer/composer')));
  187. $packageMock->expects($this->any())
  188. ->method('getSourceUrl')
  189. ->will($this->returnValue('https://github.com/composer/composer'));
  190. $packageMock->expects($this->any())
  191. ->method('getPrettyVersion')
  192. ->will($this->returnValue('1.0.0'));
  193. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  194. $processExecutor->expects($this->at(0))
  195. ->method('execute')
  196. ->with($this->equalTo($this->winCompat('git --version')))
  197. ->will($this->returnCallback(function ($command, &$output = null) {
  198. $output = 'git version 1.0.0';
  199. return 0;
  200. }));
  201. $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");
  202. $processExecutor->expects($this->at(1))
  203. ->method('execute')
  204. ->with($this->equalTo($expectedGitCommand))
  205. ->will($this->returnValue(1));
  206. $processExecutor->expects($this->at(2))
  207. ->method('getErrorOutput')
  208. ->with()
  209. ->will($this->returnValue('Error1'));
  210. $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");
  211. $processExecutor->expects($this->at(3))
  212. ->method('execute')
  213. ->with($this->equalTo($expectedGitCommand))
  214. ->will($this->returnValue(0));
  215. $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'");
  216. $processExecutor->expects($this->at(4))
  217. ->method('execute')
  218. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  219. ->will($this->returnValue(0));
  220. $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  221. $processExecutor->expects($this->at(5))
  222. ->method('execute')
  223. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  224. ->will($this->returnValue(0));
  225. $processExecutor->expects($this->at(6))
  226. ->method('execute')
  227. ->with($this->equalTo('git branch -r'))
  228. ->will($this->returnValue(0));
  229. $processExecutor->expects($this->at(7))
  230. ->method('execute')
  231. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  232. ->will($this->returnValue(0));
  233. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  234. $downloader->download($packageMock, 'composerPath');
  235. $downloader->prepare('install', $packageMock, 'composerPath');
  236. $downloader->install($packageMock, 'composerPath');
  237. $downloader->cleanup('install', $packageMock, 'composerPath');
  238. }
  239. public function pushUrlProvider()
  240. {
  241. return array(
  242. // ssh proto should use git@ all along
  243. array(array('ssh'), 'git@github.com:composer/composer', 'git@github.com:composer/composer.git'),
  244. // auto-proto uses git@ by default for push url, but not fetch
  245. array(array('https', 'ssh', 'git'), 'https://github.com/composer/composer', 'git@github.com:composer/composer.git'),
  246. // if restricted to https then push url is not overwritten to git@
  247. array(array('https'), 'https://github.com/composer/composer', 'https://github.com/composer/composer.git'),
  248. );
  249. }
  250. /**
  251. * @dataProvider pushUrlProvider
  252. */
  253. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocols, $url, $pushUrl)
  254. {
  255. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  256. $packageMock->expects($this->any())
  257. ->method('getSourceReference')
  258. ->will($this->returnValue('ref'));
  259. $packageMock->expects($this->any())
  260. ->method('getSourceUrls')
  261. ->will($this->returnValue(array('https://github.com/composer/composer')));
  262. $packageMock->expects($this->any())
  263. ->method('getSourceUrl')
  264. ->will($this->returnValue('https://github.com/composer/composer'));
  265. $packageMock->expects($this->any())
  266. ->method('getPrettyVersion')
  267. ->will($this->returnValue('1.0.0'));
  268. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  269. $processExecutor->expects($this->at(0))
  270. ->method('execute')
  271. ->with($this->equalTo($this->winCompat('git --version')))
  272. ->will($this->returnCallback(function ($command, &$output = null) {
  273. $output = 'git version 1.0.0';
  274. return 0;
  275. }));
  276. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer");
  277. $processExecutor->expects($this->at(1))
  278. ->method('execute')
  279. ->with($this->equalTo($expectedGitCommand))
  280. ->will($this->returnValue(0));
  281. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  282. $processExecutor->expects($this->at(2))
  283. ->method('execute')
  284. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  285. ->will($this->returnValue(0));
  286. $processExecutor->expects($this->exactly(5))
  287. ->method('execute')
  288. ->will($this->returnValue(0));
  289. $config = new Config();
  290. $config->merge(array('config' => array('github-protocols' => $protocols)));
  291. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  292. $downloader->download($packageMock, 'composerPath');
  293. $downloader->prepare('install', $packageMock, 'composerPath');
  294. $downloader->install($packageMock, 'composerPath');
  295. $downloader->cleanup('install', $packageMock, 'composerPath');
  296. }
  297. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  298. {
  299. $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");
  300. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  301. $packageMock->expects($this->any())
  302. ->method('getSourceReference')
  303. ->will($this->returnValue('ref'));
  304. $packageMock->expects($this->any())
  305. ->method('getSourceUrls')
  306. ->will($this->returnValue(array('https://example.com/composer/composer')));
  307. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  308. $processExecutor->expects($this->at(0))
  309. ->method('execute')
  310. ->with($this->equalTo($this->winCompat('git --version')))
  311. ->will($this->returnCallback(function ($command, &$output = null) {
  312. $output = 'git version 1.0.0';
  313. return 0;
  314. }));
  315. $processExecutor->expects($this->at(1))
  316. ->method('execute')
  317. ->with($this->equalTo($expectedGitCommand))
  318. ->will($this->returnValue(1));
  319. // not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe
  320. try {
  321. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  322. $downloader->download($packageMock, 'composerPath');
  323. $downloader->prepare('install', $packageMock, 'composerPath');
  324. $downloader->install($packageMock, 'composerPath');
  325. $downloader->cleanup('install', $packageMock, 'composerPath');
  326. $this->fail('This test should throw');
  327. } catch (\RuntimeException $e) {
  328. if ('RuntimeException' !== get_class($e)) {
  329. throw $e;
  330. }
  331. $this->assertEquals('RuntimeException', get_class($e));
  332. }
  333. }
  334. /**
  335. * @expectedException \InvalidArgumentException
  336. */
  337. public function testUpdateforPackageWithoutSourceReference()
  338. {
  339. $initialPackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  340. $sourcePackageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  341. $sourcePackageMock->expects($this->once())
  342. ->method('getSourceReference')
  343. ->will($this->returnValue(null));
  344. $downloader = $this->getDownloaderMock();
  345. $downloader->download($sourcePackageMock, '/path', $initialPackageMock);
  346. $downloader->prepare('update', $sourcePackageMock, '/path', $initialPackageMock);
  347. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  348. $downloader->cleanup('update', $sourcePackageMock, '/path', $initialPackageMock);
  349. }
  350. public function testUpdate()
  351. {
  352. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  353. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  354. $packageMock->expects($this->any())
  355. ->method('getSourceReference')
  356. ->will($this->returnValue('ref'));
  357. $packageMock->expects($this->any())
  358. ->method('getSourceUrls')
  359. ->will($this->returnValue(array('https://github.com/composer/composer')));
  360. $packageMock->expects($this->any())
  361. ->method('getVersion')
  362. ->will($this->returnValue('1.0.0.0'));
  363. $process = $this->prophesize('Composer\Util\ProcessExecutor');
  364. $process->execute($this->winCompat('git --version'), Argument::cetera())->willReturn(0);
  365. $process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0);
  366. $process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0);
  367. $process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0);
  368. $process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0);
  369. $process->execute($expectedGitUpdateCommand, null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled();
  370. $process->execute($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"), null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled();
  371. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  372. $downloader = $this->getDownloaderMock(null, new Config(), $process->reveal());
  373. $downloader->download($packageMock, $this->workingDir, $packageMock);
  374. $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
  375. $downloader->update($packageMock, $packageMock, $this->workingDir);
  376. $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
  377. }
  378. public function testUpdateWithNewRepoUrl()
  379. {
  380. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  381. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  382. $packageMock->expects($this->any())
  383. ->method('getSourceReference')
  384. ->will($this->returnValue('ref'));
  385. $packageMock->expects($this->any())
  386. ->method('getSourceUrls')
  387. ->will($this->returnValue(array('https://github.com/composer/composer')));
  388. $packageMock->expects($this->any())
  389. ->method('getSourceUrl')
  390. ->will($this->returnValue('https://github.com/composer/composer'));
  391. $packageMock->expects($this->any())
  392. ->method('getVersion')
  393. ->will($this->returnValue('1.0.0.0'));
  394. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  395. $processExecutor->expects($this->at(0))
  396. ->method('execute')
  397. ->with($this->equalTo($this->winCompat("git --version")))
  398. ->will($this->returnValue(0));
  399. $processExecutor->expects($this->at(1))
  400. ->method('execute')
  401. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  402. ->will($this->returnValue(0));
  403. $processExecutor->expects($this->at(2))
  404. ->method('execute')
  405. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  406. ->will($this->returnValue(0));
  407. $processExecutor->expects($this->at(3))
  408. ->method('execute')
  409. ->with($this->equalTo($this->winCompat("git remote -v")))
  410. ->will($this->returnValue(0));
  411. $processExecutor->expects($this->at(4))
  412. ->method('execute')
  413. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  414. ->will($this->returnValue(0));
  415. $processExecutor->expects($this->at(5))
  416. ->method('execute')
  417. ->with($this->equalTo('git branch -r'))
  418. ->will($this->returnValue(0));
  419. $processExecutor->expects($this->at(6))
  420. ->method('execute')
  421. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  422. ->will($this->returnValue(0));
  423. $processExecutor->expects($this->at(7))
  424. ->method('execute')
  425. ->with($this->equalTo($this->winCompat("git remote -v")))
  426. ->will($this->returnCallback(function ($cmd, &$output, $cwd) {
  427. $output = 'origin https://github.com/old/url (fetch)
  428. origin https://github.com/old/url (push)
  429. composer https://github.com/old/url (fetch)
  430. composer https://github.com/old/url (push)
  431. ';
  432. return 0;
  433. }));
  434. $processExecutor->expects($this->at(8))
  435. ->method('execute')
  436. ->with($this->equalTo($this->winCompat("git remote set-url origin 'https://github.com/composer/composer'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  437. ->will($this->returnValue(0));
  438. $processExecutor->expects($this->at(9))
  439. ->method('execute')
  440. ->with($this->equalTo($this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  441. ->will($this->returnValue(0));
  442. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  443. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  444. $downloader->download($packageMock, $this->workingDir, $packageMock);
  445. $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
  446. $downloader->update($packageMock, $packageMock, $this->workingDir);
  447. $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
  448. }
  449. /**
  450. * @group failing
  451. */
  452. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  453. {
  454. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  455. $expectedGitUpdateCommand2 = $this->winCompat("git remote set-url composer 'git@github.com:composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  456. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  457. $packageMock->expects($this->any())
  458. ->method('getSourceReference')
  459. ->will($this->returnValue('ref'));
  460. $packageMock->expects($this->any())
  461. ->method('getSourceUrls')
  462. ->will($this->returnValue(array('https://github.com/composer/composer')));
  463. $packageMock->expects($this->any())
  464. ->method('getVersion')
  465. ->will($this->returnValue('1.0.0.0'));
  466. $process = $this->prophesize('Composer\Util\ProcessExecutor');
  467. $process->execute($this->winCompat('git --version'), Argument::cetera())->willReturn(0);
  468. $process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0);
  469. $process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0);
  470. $process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0);
  471. $process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0);
  472. $process->execute($expectedGitUpdateCommand, null, $this->winCompat($this->workingDir))->willReturn(1)->shouldBeCalled();
  473. $process->execute($expectedGitUpdateCommand2, null, $this->winCompat($this->workingDir))->willReturn(1)->shouldBeCalled();
  474. $process->getErrorOutput()->willReturn('');
  475. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  476. // not using PHPUnit's expected exception because Prophecy exceptions extend from RuntimeException too so it is not safe
  477. try {
  478. $downloader = $this->getDownloaderMock(null, new Config(), $process->reveal());
  479. $downloader->download($packageMock, $this->workingDir, $packageMock);
  480. $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
  481. $downloader->update($packageMock, $packageMock, $this->workingDir);
  482. $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
  483. $this->fail('This test should throw');
  484. } catch (\RuntimeException $e) {
  485. if ('RuntimeException' !== get_class($e)) {
  486. throw $e;
  487. }
  488. $this->assertEquals('RuntimeException', get_class($e));
  489. }
  490. }
  491. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  492. {
  493. $expectedFirstGitUpdateCommand = $this->winCompat("git remote set-url composer '".(Platform::isWindows() ? 'C:\\' : '/')."' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  494. $expectedSecondGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git rev-parse --quiet --verify 'ref^{commit}' || (git fetch composer && git fetch --tags composer)");
  495. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  496. $packageMock->expects($this->any())
  497. ->method('getSourceReference')
  498. ->will($this->returnValue('ref'));
  499. $packageMock->expects($this->any())
  500. ->method('getVersion')
  501. ->will($this->returnValue('1.0.0.0'));
  502. $packageMock->expects($this->any())
  503. ->method('getSourceUrls')
  504. ->will($this->returnValue(array(Platform::isWindows() ? 'C:\\' : '/', 'https://github.com/composer/composer')));
  505. $process = $this->prophesize('Composer\Util\ProcessExecutor');
  506. $process->execute($this->winCompat('git --version'), Argument::cetera())->willReturn(0);
  507. $process->execute($this->winCompat('git show-ref --head -d'), Argument::cetera())->willReturn(0);
  508. $process->execute($this->winCompat('git status --porcelain --untracked-files=no'), Argument::cetera())->willReturn(0);
  509. $process->execute($this->winCompat('git remote -v'), Argument::cetera())->willReturn(0);
  510. $process->execute($this->winCompat('git branch -r'), Argument::cetera())->willReturn(0);
  511. $process->execute($expectedFirstGitUpdateCommand, Argument::cetera())->willReturn(1)->shouldBeCalled();
  512. $process->execute($expectedSecondGitUpdateCommand, Argument::cetera())->willReturn(0)->shouldBeCalled();
  513. $process->execute($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --"), null, $this->winCompat($this->workingDir))->willReturn(0)->shouldBeCalled();
  514. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  515. $downloader = $this->getDownloaderMock(null, new Config(), $process->reveal());
  516. $downloader->download($packageMock, $this->workingDir, $packageMock);
  517. $downloader->prepare('update', $packageMock, $this->workingDir, $packageMock);
  518. $downloader->update($packageMock, $packageMock, $this->workingDir);
  519. $downloader->cleanup('update', $packageMock, $this->workingDir, $packageMock);
  520. }
  521. public function testDowngradeShowsAppropriateMessage()
  522. {
  523. $oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  524. $oldPackage->expects($this->any())
  525. ->method('getVersion')
  526. ->will($this->returnValue('1.2.0.0'));
  527. $oldPackage->expects($this->any())
  528. ->method('getFullPrettyVersion')
  529. ->will($this->returnValue('1.2.0'));
  530. $oldPackage->expects($this->any())
  531. ->method('getSourceReference')
  532. ->will($this->returnValue('ref'));
  533. $oldPackage->expects($this->any())
  534. ->method('getSourceUrls')
  535. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  536. $newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  537. $newPackage->expects($this->any())
  538. ->method('getSourceReference')
  539. ->will($this->returnValue('ref'));
  540. $newPackage->expects($this->any())
  541. ->method('getSourceUrls')
  542. ->will($this->returnValue(array('https://github.com/composer/composer')));
  543. $newPackage->expects($this->any())
  544. ->method('getVersion')
  545. ->will($this->returnValue('1.0.0.0'));
  546. $newPackage->expects($this->any())
  547. ->method('getFullPrettyVersion')
  548. ->will($this->returnValue('1.0.0'));
  549. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  550. $processExecutor->expects($this->any())
  551. ->method('execute')
  552. ->will($this->returnValue(0));
  553. $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  554. $ioMock->expects($this->at(0))
  555. ->method('writeError')
  556. ->with($this->stringContains('Downgrading'));
  557. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  558. $downloader = $this->getDownloaderMock($ioMock, null, $processExecutor);
  559. $downloader->download($newPackage, $this->workingDir, $oldPackage);
  560. $downloader->prepare('update', $newPackage, $this->workingDir, $oldPackage);
  561. $downloader->update($oldPackage, $newPackage, $this->workingDir);
  562. $downloader->cleanup('update', $newPackage, $this->workingDir, $oldPackage);
  563. }
  564. public function testNotUsingDowngradingWithReferences()
  565. {
  566. $oldPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  567. $oldPackage->expects($this->any())
  568. ->method('getVersion')
  569. ->will($this->returnValue('dev-ref'));
  570. $oldPackage->expects($this->any())
  571. ->method('getSourceReference')
  572. ->will($this->returnValue('ref'));
  573. $oldPackage->expects($this->any())
  574. ->method('getSourceUrls')
  575. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  576. $newPackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  577. $newPackage->expects($this->any())
  578. ->method('getSourceReference')
  579. ->will($this->returnValue('ref'));
  580. $newPackage->expects($this->any())
  581. ->method('getSourceUrls')
  582. ->will($this->returnValue(array('https://github.com/composer/composer')));
  583. $newPackage->expects($this->any())
  584. ->method('getVersion')
  585. ->will($this->returnValue('dev-ref2'));
  586. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  587. $processExecutor->expects($this->any())
  588. ->method('execute')
  589. ->will($this->returnValue(0));
  590. $ioMock = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  591. $ioMock->expects($this->at(0))
  592. ->method('writeError')
  593. ->with($this->stringContains('Updating'));
  594. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  595. $downloader = $this->getDownloaderMock($ioMock, null, $processExecutor);
  596. $downloader->download($newPackage, $this->workingDir, $oldPackage);
  597. $downloader->prepare('update', $newPackage, $this->workingDir, $oldPackage);
  598. $downloader->update($oldPackage, $newPackage, $this->workingDir);
  599. $downloader->cleanup('update', $newPackage, $this->workingDir, $oldPackage);
  600. }
  601. public function testRemove()
  602. {
  603. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  604. $packageMock = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
  605. $processExecutor = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  606. $processExecutor->expects($this->any())
  607. ->method('execute')
  608. ->with($this->equalTo($expectedGitResetCommand))
  609. ->will($this->returnValue(0));
  610. $filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  611. $filesystem->expects($this->any())
  612. ->method('removeDirectory')
  613. ->with($this->equalTo('composerPath'))
  614. ->will($this->returnValue(true));
  615. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  616. $downloader->prepare('uninstall', $packageMock, 'composerPath');
  617. $downloader->remove($packageMock, 'composerPath');
  618. $downloader->cleanup('uninstall', $packageMock, 'composerPath');
  619. }
  620. public function testGetInstallationSource()
  621. {
  622. $downloader = $this->getDownloaderMock();
  623. $this->assertEquals('source', $downloader->getInstallationSource());
  624. }
  625. private function winCompat($cmd)
  626. {
  627. if (Platform::isWindows()) {
  628. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  629. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  630. return str_replace('""', '', strtr($cmd, "'", '"'));
  631. }
  632. return $cmd;
  633. }
  634. }