GitDownloaderTest.php 26 KB

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