GitDownloaderTest.php 15 KB

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