GitDownloaderTest.php 15 KB

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