GitDownloaderTest.php 34 KB

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