GitDownloaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. class GitDownloaderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected function getDownloaderMock($io = null, $executor = null, $filesystem = null)
  16. {
  17. $io = $io ?: $this->getMock('Composer\IO\IOInterface');
  18. $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor');
  19. $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem');
  20. return new GitDownloader($io, $executor, $filesystem);
  21. }
  22. /**
  23. * @expectedException \InvalidArgumentException
  24. */
  25. public function testDownloadForPackageWithoutSourceReference()
  26. {
  27. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  28. $packageMock->expects($this->once())
  29. ->method('getSourceReference')
  30. ->will($this->returnValue(null));
  31. $downloader = $this->getDownloaderMock();
  32. $downloader->download($packageMock, '/path');
  33. }
  34. public function testDownload()
  35. {
  36. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  37. $packageMock->expects($this->any())
  38. ->method('getSourceReference')
  39. ->will($this->returnValue('ref'));
  40. $packageMock->expects($this->any())
  41. ->method('getSourceUrl')
  42. ->will($this->returnValue('https://example.com/composer/composer'));
  43. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  44. $expectedGitCommand = $this->getCmd("git clone 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref' && git remote add composer 'https://example.com/composer/composer'");
  45. $processExecutor->expects($this->once())
  46. ->method('execute')
  47. ->with($this->equalTo($expectedGitCommand))
  48. ->will($this->returnValue(0));
  49. $downloader = $this->getDownloaderMock(null, $processExecutor);
  50. $downloader->download($packageMock, 'composerPath');
  51. }
  52. public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub()
  53. {
  54. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  55. $packageMock->expects($this->any())
  56. ->method('getSourceReference')
  57. ->will($this->returnValue('ref'));
  58. $packageMock->expects($this->any())
  59. ->method('getSourceUrl')
  60. ->will($this->returnValue('https://github.com/composer/composer'));
  61. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  62. $expectedGitCommand = $this->getCmd("git clone 'git://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref' && git remote add composer 'git://github.com/composer/composer'");
  63. $processExecutor->expects($this->at(0))
  64. ->method('execute')
  65. ->with($this->equalTo($expectedGitCommand))
  66. ->will($this->returnValue(1));
  67. $expectedGitCommand = $this->getCmd("git clone 'https://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref' && git remote add composer 'https://github.com/composer/composer'");
  68. $processExecutor->expects($this->at(2))
  69. ->method('execute')
  70. ->with($this->equalTo($expectedGitCommand))
  71. ->will($this->returnValue(1));
  72. $expectedGitCommand = $this->getCmd("git clone 'http://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref' && git remote add composer 'http://github.com/composer/composer'");
  73. $processExecutor->expects($this->at(4))
  74. ->method('execute')
  75. ->with($this->equalTo($expectedGitCommand))
  76. ->will($this->returnValue(0));
  77. $expectedGitCommand = $this->getCmd("git remote set-url --push origin 'git@github.com:composer/composer.git'");
  78. $processExecutor->expects($this->at(5))
  79. ->method('execute')
  80. ->with($this->equalTo($expectedGitCommand), $this->equalTo(null), $this->equalTo('composerPath'))
  81. ->will($this->returnValue(0));
  82. $downloader = $this->getDownloaderMock(null, $processExecutor);
  83. $downloader->download($packageMock, 'composerPath');
  84. }
  85. /**
  86. * @expectedException \RuntimeException
  87. */
  88. public function testDownloadThrowsRuntimeExceptionIfGitCommandFails()
  89. {
  90. $expectedGitCommand = $this->getCmd("git clone 'https://example.com/composer/composer' 'composerPath' && cd 'composerPath' && git checkout 'ref' && git reset --hard 'ref' && git remote add composer 'https://example.com/composer/composer'");
  91. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  92. $packageMock->expects($this->any())
  93. ->method('getSourceReference')
  94. ->will($this->returnValue('ref'));
  95. $packageMock->expects($this->any())
  96. ->method('getSourceUrl')
  97. ->will($this->returnValue('https://example.com/composer/composer'));
  98. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  99. $processExecutor->expects($this->at(0))
  100. ->method('execute')
  101. ->with($this->equalTo($expectedGitCommand))
  102. ->will($this->returnValue(1));
  103. $downloader = $this->getDownloaderMock(null, $processExecutor);
  104. $downloader->download($packageMock, 'composerPath');
  105. }
  106. /**
  107. * @expectedException \InvalidArgumentException
  108. */
  109. public function testUpdateforPackageWithoutSourceReference()
  110. {
  111. $initialPackageMock = $this->getMock('Composer\Package\PackageInterface');
  112. $sourcePackageMock = $this->getMock('Composer\Package\PackageInterface');
  113. $sourcePackageMock->expects($this->once())
  114. ->method('getSourceReference')
  115. ->will($this->returnValue(null));
  116. $downloader = $this->getDownloaderMock();
  117. $downloader->update($initialPackageMock, $sourcePackageMock, '/path');
  118. }
  119. public function testUpdate()
  120. {
  121. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer && git checkout 'ref' && git reset --hard 'ref'");
  122. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  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('getSourceUrl')
  129. ->will($this->returnValue('https://github.com/composer/composer'));
  130. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  131. $processExecutor->expects($this->at(0))
  132. ->method('execute')
  133. ->with($this->equalTo($expectedGitResetCommand))
  134. ->will($this->returnValue(0));
  135. $processExecutor->expects($this->at(1))
  136. ->method('execute')
  137. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote -v")))
  138. ->will($this->returnValue(0));
  139. $processExecutor->expects($this->at(2))
  140. ->method('execute')
  141. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote add composer 'https://github.com/composer/composer'")))
  142. ->will($this->returnValue(0));
  143. $processExecutor->expects($this->at(3))
  144. ->method('execute')
  145. ->with($this->equalTo($expectedGitUpdateCommand))
  146. ->will($this->returnValue(0));
  147. $downloader = $this->getDownloaderMock(null, $processExecutor);
  148. $downloader->update($packageMock, $packageMock, 'composerPath');
  149. }
  150. /**
  151. * @expectedException \RuntimeException
  152. */
  153. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  154. {
  155. $expectedGitUpdateCommand = $this->getCmd("cd 'composerPath' && git remote set-url composer 'git://github.com/composer/composer' && git fetch composer && git fetch --tags composer && git checkout 'ref' && git reset --hard 'ref'");
  156. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  157. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  158. $packageMock->expects($this->any())
  159. ->method('getSourceReference')
  160. ->will($this->returnValue('ref'));
  161. $packageMock->expects($this->any())
  162. ->method('getSourceUrl')
  163. ->will($this->returnValue('https://github.com/composer/composer'));
  164. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  165. $processExecutor->expects($this->at(0))
  166. ->method('execute')
  167. ->with($this->equalTo($expectedGitResetCommand))
  168. ->will($this->returnValue(0));
  169. $processExecutor->expects($this->at(1))
  170. ->method('execute')
  171. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote -v")))
  172. ->will($this->returnValue(0));
  173. $processExecutor->expects($this->at(2))
  174. ->method('execute')
  175. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote add composer 'https://github.com/composer/composer'")))
  176. ->will($this->returnValue(0));
  177. $processExecutor->expects($this->at(3))
  178. ->method('execute')
  179. ->with($this->equalTo($expectedGitUpdateCommand))
  180. ->will($this->returnValue(1));
  181. $downloader = $this->getDownloaderMock(null, $processExecutor);
  182. $downloader->update($packageMock, $packageMock, 'composerPath');
  183. }
  184. public function testRemove()
  185. {
  186. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  187. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  188. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  189. $processExecutor->expects($this->any())
  190. ->method('execute')
  191. ->with($this->equalTo($expectedGitResetCommand))
  192. ->will($this->returnValue(0));
  193. $filesystem = $this->getMock('Composer\Util\Filesystem');
  194. $filesystem->expects($this->any())
  195. ->method('removeDirectory')
  196. ->with($this->equalTo('composerPath'))
  197. ->will($this->returnValue(true));
  198. $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem);
  199. $downloader->remove($packageMock, 'composerPath');
  200. }
  201. public function testGetInstallationSource()
  202. {
  203. $downloader = $this->getDownloaderMock();
  204. $this->assertEquals('source', $downloader->getInstallationSource());
  205. }
  206. private function getCmd($cmd)
  207. {
  208. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  209. return strtr($cmd, "'", '"');
  210. }
  211. return $cmd;
  212. }
  213. }