GitDownloaderTest.php 15 KB

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