GitDownloaderTest.php 14 KB

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