GitDownloaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(1))
  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(2))
  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(3))
  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 add composer 'https://github.com/composer/composer'")))
  138. ->will($this->returnValue(0));
  139. $processExecutor->expects($this->at(2))
  140. ->method('execute')
  141. ->with($this->equalTo($expectedGitUpdateCommand))
  142. ->will($this->returnValue(0));
  143. $downloader = $this->getDownloaderMock(null, $processExecutor);
  144. $downloader->update($packageMock, $packageMock, 'composerPath');
  145. }
  146. /**
  147. * @expectedException \RuntimeException
  148. */
  149. public function testUpdateThrowsRuntimeExceptionIfGitCommandFails()
  150. {
  151. $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'");
  152. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  153. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  154. $packageMock->expects($this->any())
  155. ->method('getSourceReference')
  156. ->will($this->returnValue('ref'));
  157. $packageMock->expects($this->any())
  158. ->method('getSourceUrl')
  159. ->will($this->returnValue('https://github.com/composer/composer'));
  160. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  161. $processExecutor->expects($this->at(0))
  162. ->method('execute')
  163. ->with($this->equalTo($expectedGitResetCommand))
  164. ->will($this->returnValue(0));
  165. $processExecutor->expects($this->at(1))
  166. ->method('execute')
  167. ->with($this->equalTo($this->getCmd("cd 'composerPath' && git remote add composer 'https://github.com/composer/composer'")))
  168. ->will($this->returnValue(0));
  169. $processExecutor->expects($this->at(2))
  170. ->method('execute')
  171. ->with($this->equalTo($expectedGitUpdateCommand))
  172. ->will($this->returnValue(1));
  173. $downloader = $this->getDownloaderMock(null, $processExecutor);
  174. $downloader->update($packageMock, $packageMock, 'composerPath');
  175. }
  176. public function testRemove()
  177. {
  178. $expectedGitResetCommand = $this->getCmd("cd 'composerPath' && git status --porcelain --untracked-files=no");
  179. $packageMock = $this->getMock('Composer\Package\PackageInterface');
  180. $processExecutor = $this->getMock('Composer\Util\ProcessExecutor');
  181. $processExecutor->expects($this->any())
  182. ->method('execute')
  183. ->with($this->equalTo($expectedGitResetCommand))
  184. ->will($this->returnValue(0));
  185. $filesystem = $this->getMock('Composer\Util\Filesystem');
  186. $filesystem->expects($this->any())
  187. ->method('removeDirectory')
  188. ->with($this->equalTo('composerPath'))
  189. ->will($this->returnValue(true));
  190. $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem);
  191. $downloader->remove($packageMock, 'composerPath');
  192. }
  193. public function testGetInstallationSource()
  194. {
  195. $downloader = $this->getDownloaderMock();
  196. $this->assertEquals('source', $downloader->getInstallationSource());
  197. }
  198. private function getCmd($cmd)
  199. {
  200. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  201. return strtr($cmd, "'", '"');
  202. }
  203. return $cmd;
  204. }
  205. }