GitDownloaderTest.php 16 KB

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