GitDownloaderTest.php 14 KB

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