GitDownloaderTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. if (!$config->has('home')) {
  43. $tmpDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest-'.md5(uniqid('', true));
  44. $config->merge(array('config' => array('home' => $tmpDir)));
  45. }
  46. return new GitDownloader($io, $config, $executor, $filesystem);
  47. }
  48. /**
  49. * @expectedException \InvalidArgumentException
  50. */
  51. public function testDownloadForPackageWithoutSourceReference()
  52. {
  53. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  54. $packageMock->expects($this->once())
  55. ->method('getSourceReference')
  56. ->will($this->returnValue(null));
  57. $downloader = $this->getDownloaderMock();
  58. $downloader->download($packageMock, '/path');
  59. }
  60. public function testDownload()
  61. {
  62. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  63. $packageMock->expects($this->any())
  64. ->method('getSourceReference')
  65. ->will($this->returnValue('1234567890123456789012345678901234567890'));
  66. $packageMock->expects($this->any())
  67. ->method('getSourceUrls')
  68. ->will($this->returnValue(array('https://example.com/composer/composer')));
  69. $packageMock->expects($this->any())
  70. ->method('getSourceUrl')
  71. ->will($this->returnValue('https://example.com/composer/composer'));
  72. $packageMock->expects($this->any())
  73. ->method('getPrettyVersion')
  74. ->will($this->returnValue('dev-master'));
  75. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  76. $processExecutor->expects($this->at(0))
  77. ->method('execute')
  78. ->with($this->equalTo($this->winCompat('git --version')))
  79. ->will($this->returnCallback(function($command, &$output = null) {
  80. $output = 'git version 1.0.0';
  81. return 0;
  82. }));
  83. $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");
  84. $processExecutor->expects($this->at(1))
  85. ->method('execute')
  86. ->with($this->equalTo($expectedGitCommand))
  87. ->will($this->returnValue(0));
  88. $processExecutor->expects($this->at(2))
  89. ->method('execute')
  90. ->with($this->equalTo($this->winCompat("git branch -r")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  91. ->will($this->returnValue(0));
  92. $processExecutor->expects($this->at(3))
  93. ->method('execute')
  94. ->with($this->equalTo($this->winCompat("git checkout 'master' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  95. ->will($this->returnValue(0));
  96. $processExecutor->expects($this->at(4))
  97. ->method('execute')
  98. ->with($this->equalTo($this->winCompat("git reset --hard '1234567890123456789012345678901234567890' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  99. ->will($this->returnValue(0));
  100. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  101. $downloader->download($packageMock, 'composerPath');
  102. }
  103. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  104. {
  105. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  106. $packageMock->expects($this->any())
  107. ->method('getSourceReference')
  108. ->will($this->returnValue('ref'));
  109. $packageMock->expects($this->any())
  110. ->method('getSourceUrls')
  111. ->will($this->returnValue(array('https://github.com/mirrors/composer', 'https://github.com/composer/composer')));
  112. $packageMock->expects($this->any())
  113. ->method('getSourceUrl')
  114. ->will($this->returnValue('https://github.com/composer/composer'));
  115. $packageMock->expects($this->any())
  116. ->method('getPrettyVersion')
  117. ->will($this->returnValue('1.0.0'));
  118. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  119. $processExecutor->expects($this->at(0))
  120. ->method('execute')
  121. ->with($this->equalTo($this->winCompat('git --version')))
  122. ->will($this->returnCallback(function($command, &$output = null) {
  123. $output = 'git version 1.0.0';
  124. return 0;
  125. }));
  126. $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");
  127. $processExecutor->expects($this->at(1))
  128. ->method('execute')
  129. ->with($this->equalTo($expectedGitCommand))
  130. ->will($this->returnValue(1));
  131. $processExecutor->expects($this->at(2))
  132. ->method('getErrorOutput')
  133. ->with()
  134. ->will($this->returnValue('Error1'));
  135. $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");
  136. $processExecutor->expects($this->at(3))
  137. ->method('execute')
  138. ->with($this->equalTo($expectedGitCommand))
  139. ->will($this->returnValue(0));
  140. $expectedGitCommand = $this->winCompat("git remote set-url origin 'https://github.com/composer/composer'");
  141. $processExecutor->expects($this->at(4))
  142. ->method('execute')
  143. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  144. ->will($this->returnValue(0));
  145. $expectedGitCommand = $this->winCompat("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  146. $processExecutor->expects($this->at(5))
  147. ->method('execute')
  148. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  149. ->will($this->returnValue(0));
  150. $processExecutor->expects($this->at(6))
  151. ->method('execute')
  152. ->with($this->equalTo('git branch -r'))
  153. ->will($this->returnValue(0));
  154. $processExecutor->expects($this->at(7))
  155. ->method('execute')
  156. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  157. ->will($this->returnValue(0));
  158. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  159. $downloader->download($packageMock, 'composerPath');
  160. }
  161. public function pushUrlProvider()
  162. {
  163. return array(
  164. // ssh proto should use git@ all along
  165. array(array('ssh'), 'git@github.com:composer/composer', 'git@github.com:composer/composer.git'),
  166. // auto-proto uses git@ by default for push url, but not fetch
  167. array(array('https', 'ssh', 'git'), 'https://github.com/composer/composer', 'git@github.com:composer/composer.git'),
  168. // if restricted to https then push url is not overwritten to git@
  169. array(array('https'), 'https://github.com/composer/composer', 'https://github.com/composer/composer.git'),
  170. );
  171. }
  172. /**
  173. * @dataProvider pushUrlProvider
  174. */
  175. public function testDownloadAndSetPushUrlUseCustomVariousProtocolsForGithub($protocols, $url, $pushUrl)
  176. {
  177. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  178. $packageMock->expects($this->any())
  179. ->method('getSourceReference')
  180. ->will($this->returnValue('ref'));
  181. $packageMock->expects($this->any())
  182. ->method('getSourceUrls')
  183. ->will($this->returnValue(array('https://github.com/composer/composer')));
  184. $packageMock->expects($this->any())
  185. ->method('getSourceUrl')
  186. ->will($this->returnValue('https://github.com/composer/composer'));
  187. $packageMock->expects($this->any())
  188. ->method('getPrettyVersion')
  189. ->will($this->returnValue('1.0.0'));
  190. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  191. $processExecutor->expects($this->at(0))
  192. ->method('execute')
  193. ->with($this->equalTo($this->winCompat('git --version')))
  194. ->will($this->returnCallback(function($command, &$output = null) {
  195. $output = 'git version 1.0.0';
  196. return 0;
  197. }));
  198. $expectedGitCommand = $this->winCompat("git clone --no-checkout '{$url}' 'composerPath' && cd 'composerPath' && git remote add composer '{$url}' && git fetch composer");
  199. $processExecutor->expects($this->at(1))
  200. ->method('execute')
  201. ->with($this->equalTo($expectedGitCommand))
  202. ->will($this->returnValue(0));
  203. $expectedGitCommand = $this->winCompat("git remote set-url --push origin '{$pushUrl}'");
  204. $processExecutor->expects($this->at(2))
  205. ->method('execute')
  206. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo($this->winCompat('composerPath')))
  207. ->will($this->returnValue(0));
  208. $processExecutor->expects($this->exactly(5))
  209. ->method('execute')
  210. ->will($this->returnValue(0));
  211. $config = new Config();
  212. $config->merge(array('config' => array('github-protocols' => $protocols)));
  213. $downloader = $this->getDownloaderMock(null, $config, $processExecutor);
  214. $downloader->download($packageMock, 'composerPath');
  215. }
  216. /**
  217. * @expectedException \RuntimeException
  218. */
  219. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  220. {
  221. $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");
  222. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  223. $packageMock->expects($this->any())
  224. ->method('getSourceReference')
  225. ->will($this->returnValue('ref'));
  226. $packageMock->expects($this->any())
  227. ->method('getSourceUrls')
  228. ->will($this->returnValue(array('https://example.com/composer/composer')));
  229. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  230. $processExecutor->expects($this->at(0))
  231. ->method('execute')
  232. ->with($this->equalTo($this->winCompat('git --version')))
  233. ->will($this->returnCallback(function($command, &$output = null) {
  234. $output = 'git version 1.0.0';
  235. return 0;
  236. }));
  237. $processExecutor->expects($this->at(1))
  238. ->method('execute')
  239. ->with($this->equalTo($expectedGitCommand))
  240. ->will($this->returnValue(1));
  241. $downloader = $this->getDownloaderMock(null, null, $processExecutor);
  242. $downloader->download($packageMock, 'composerPath');
  243. }
  244. /**
  245. * @expectedException \InvalidArgumentException
  246. */
  247. public function testUpdateforPackageWithoutSourceReference()
  248. {
  249. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  250. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  251. $sourcePackageMock->expects($this->once())
  252. ->method('getSourceReference')
  253. ->will($this->returnValue(null));
  254. $downloader = $this->getDownloaderMock();
  255. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  256. }
  257. public function testUpdate()
  258. {
  259. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  260. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  261. $packageMock->expects($this->any())
  262. ->method('getSourceReference')
  263. ->will($this->returnValue('ref'));
  264. $packageMock->expects($this->any())
  265. ->method('getSourceUrls')
  266. ->will($this->returnValue(array('https://github.com/composer/composer')));
  267. $packageMock->expects($this->any())
  268. ->method('getPrettyVersion')
  269. ->will($this->returnValue('1.0.0'));
  270. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  271. $processExecutor->expects($this->at(0))
  272. ->method('execute')
  273. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  274. ->will($this->returnValue(0));
  275. $processExecutor->expects($this->at(1))
  276. ->method('execute')
  277. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  278. ->will($this->returnValue(0));
  279. $processExecutor->expects($this->at(2))
  280. ->method('execute')
  281. ->with($this->equalTo($this->winCompat("git remote -v")))
  282. ->will($this->returnValue(0));
  283. $processExecutor->expects($this->at(3))
  284. ->method('execute')
  285. ->with($this->equalTo($this->winCompat("git remote -v")))
  286. ->will($this->returnValue(0));
  287. $processExecutor->expects($this->at(4))
  288. ->method('execute')
  289. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  290. ->will($this->returnValue(0));
  291. $processExecutor->expects($this->at(5))
  292. ->method('execute')
  293. ->with($this->equalTo('git branch -r'))
  294. ->will($this->returnValue(0));
  295. $processExecutor->expects($this->at(6))
  296. ->method('execute')
  297. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  298. ->will($this->returnValue(0));
  299. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  300. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  301. $downloader->update($packageMock, $packageMock, $this->workingDir);
  302. }
  303. public function testUpdateWithNewRepoUrl()
  304. {
  305. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  306. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  307. $packageMock->expects($this->any())
  308. ->method('getSourceReference')
  309. ->will($this->returnValue('ref'));
  310. $packageMock->expects($this->any())
  311. ->method('getSourceUrls')
  312. ->will($this->returnValue(array('https://github.com/composer/composer')));
  313. $packageMock->expects($this->any())
  314. ->method('getSourceUrl')
  315. ->will($this->returnValue('https://github.com/composer/composer'));
  316. $packageMock->expects($this->any())
  317. ->method('getPrettyVersion')
  318. ->will($this->returnValue('1.0.0'));
  319. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  320. $processExecutor->expects($this->at(0))
  321. ->method('execute')
  322. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  323. ->will($this->returnValue(0));
  324. $processExecutor->expects($this->at(1))
  325. ->method('execute')
  326. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  327. ->will($this->returnValue(0));
  328. $processExecutor->expects($this->at(2))
  329. ->method('execute')
  330. ->with($this->equalTo($this->winCompat("git remote -v")))
  331. ->will($this->returnCallback(function ($cmd, &$output, $cwd) {
  332. $output = 'origin https://github.com/old/url (fetch)
  333. origin https://github.com/old/url (push)
  334. composer https://github.com/old/url (fetch)
  335. composer https://github.com/old/url (push)
  336. ';
  337. return 0;
  338. }));
  339. $processExecutor->expects($this->at(3))
  340. ->method('execute')
  341. ->with($this->equalTo($this->winCompat("git remote -v")))
  342. ->will($this->returnValue(0));
  343. $processExecutor->expects($this->at(4))
  344. ->method('execute')
  345. ->with($this->equalTo($this->winCompat($expectedGitUpdateCommand)), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  346. ->will($this->returnValue(0));
  347. $processExecutor->expects($this->at(5))
  348. ->method('execute')
  349. ->with($this->equalTo('git branch -r'))
  350. ->will($this->returnValue(0));
  351. $processExecutor->expects($this->at(6))
  352. ->method('execute')
  353. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  354. ->will($this->returnValue(0));
  355. $processExecutor->expects($this->at(7))
  356. ->method('execute')
  357. ->with($this->equalTo($this->winCompat("git remote set-url origin 'https://github.com/composer/composer'")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  358. ->will($this->returnValue(0));
  359. $processExecutor->expects($this->at(8))
  360. ->method('execute')
  361. ->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)))
  362. ->will($this->returnValue(0));
  363. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  364. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  365. $downloader->update($packageMock, $packageMock, $this->workingDir);
  366. }
  367. /**
  368. * @group failing
  369. * @expectedException \RuntimeException
  370. */
  371. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  372. {
  373. $expectedGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  374. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  375. $packageMock->expects($this->any())
  376. ->method('getSourceReference')
  377. ->will($this->returnValue('ref'));
  378. $packageMock->expects($this->any())
  379. ->method('getSourceUrls')
  380. ->will($this->returnValue(array('https://github.com/composer/composer')));
  381. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  382. $processExecutor->expects($this->at(0))
  383. ->method('execute')
  384. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  385. ->will($this->returnValue(0));
  386. $processExecutor->expects($this->at(1))
  387. ->method('execute')
  388. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  389. ->will($this->returnValue(0));
  390. $processExecutor->expects($this->at(2))
  391. ->method('execute')
  392. ->with($this->equalTo($this->winCompat("git remote -v")))
  393. ->will($this->returnValue(0));
  394. $processExecutor->expects($this->at(3))
  395. ->method('execute')
  396. ->with($this->equalTo($this->winCompat("git remote -v")))
  397. ->will($this->returnValue(0));
  398. $processExecutor->expects($this->at(4))
  399. ->method('execute')
  400. ->with($this->equalTo($expectedGitUpdateCommand))
  401. ->will($this->returnValue(1));
  402. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  403. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  404. $downloader->update($packageMock, $packageMock, $this->workingDir);
  405. }
  406. public function testUpdateDoesntThrowsRuntimeExceptionIfGitCommandFailsAtFirstButIsAbleToRecover()
  407. {
  408. $expectedFirstGitUpdateCommand = $this->winCompat("git remote set-url composer '' && git fetch composer && git fetch --tags composer");
  409. $expectedSecondGitUpdateCommand = $this->winCompat("git remote set-url composer 'https://github.com/composer/composer' && git fetch composer && git fetch --tags composer");
  410. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  411. $packageMock->expects($this->any())
  412. ->method('getSourceReference')
  413. ->will($this->returnValue('ref'));
  414. $packageMock->expects($this->any())
  415. ->method('getSourceUrls')
  416. ->will($this->returnValue(array('/foo/bar', 'https://github.com/composer/composer')));
  417. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  418. $processExecutor->expects($this->at(0))
  419. ->method('execute')
  420. ->with($this->equalTo($this->winCompat("git show-ref --head -d")))
  421. ->will($this->returnValue(0));
  422. $processExecutor->expects($this->at(1))
  423. ->method('execute')
  424. ->with($this->equalTo($this->winCompat("git status --porcelain --untracked-files=no")))
  425. ->will($this->returnValue(0));
  426. $processExecutor->expects($this->at(2))
  427. ->method('execute')
  428. ->with($this->equalTo($this->winCompat("git remote -v")))
  429. ->will($this->returnValue(0));
  430. $processExecutor->expects($this->at(3))
  431. ->method('execute')
  432. ->with($this->equalTo($this->winCompat("git remote -v")))
  433. ->will($this->returnValue(0));
  434. $processExecutor->expects($this->at(4))
  435. ->method('execute')
  436. ->with($this->equalTo($expectedFirstGitUpdateCommand))
  437. ->will($this->returnValue(1));
  438. $processExecutor->expects($this->at(6))
  439. ->method('execute')
  440. ->with($this->equalTo($this->winCompat("git --version")))
  441. ->will($this->returnValue(0));
  442. $processExecutor->expects($this->at(7))
  443. ->method('execute')
  444. ->with($this->equalTo($this->winCompat("git remote -v")))
  445. ->will($this->returnValue(0));
  446. $processExecutor->expects($this->at(8))
  447. ->method('execute')
  448. ->with($this->equalTo($this->winCompat("git remote -v")))
  449. ->will($this->returnValue(0));
  450. $processExecutor->expects($this->at(9))
  451. ->method('execute')
  452. ->with($this->equalTo($expectedSecondGitUpdateCommand))
  453. ->will($this->returnValue(0));
  454. $processExecutor->expects($this->at(11))
  455. ->method('execute')
  456. ->with($this->equalTo($this->winCompat("git checkout 'ref' -- && git reset --hard 'ref' --")), $this->equalTo(null), $this->equalTo($this->winCompat($this->workingDir)))
  457. ->will($this->returnValue(0));
  458. $this->fs->ensureDirectoryExists($this->workingDir.'/.git');
  459. $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor);
  460. $downloader->update($packageMock, $packageMock, $this->workingDir);
  461. }
  462. public function testRemove()
  463. {
  464. $expectedGitResetCommand = $this->winCompat("cd 'composerPath' && git status --porcelain --untracked-files=no");
  465. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  466. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  467. $processExecutor->expects($this->any())
  468. ->method('execute')
  469. ->with($this->equalTo($expectedGitResetCommand))
  470. ->will($this->returnValue(0));
  471. $filesystem = $this->getMock('Composer\Util\Filesystem');
  472. $filesystem->expects($this->any())
  473. ->method('removeDirectory')
  474. ->with($this->equalTo('composerPath'))
  475. ->will($this->returnValue(true));
  476. $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem);
  477. $downloader->remove($packageMock, 'composerPath');
  478. }
  479. public function testGetInstallationSource()
  480. {
  481. $downloader = $this->getDownloaderMock();
  482. $this->assertEquals('source', $downloader->getInstallationSource());
  483. }
  484. private function winCompat($cmd)
  485. {
  486. if (Platform::isWindows()) {
  487. $cmd = str_replace('cd ', 'cd /D ', $cmd);
  488. $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd);
  489. return str_replace('""', '', strtr($cmd, "'", '"'));
  490. }
  491. return $cmd;
  492. }
  493. }