GitDownloaderTest.php 16 KB

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