GitTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Composer\Test\Util;
  3. use Composer\Config;
  4. use Composer\IO\IOInterface;
  5. use Composer\Util\Filesystem;
  6. use Composer\Util\Git;
  7. use Composer\Util\ProcessExecutor;
  8. use PHPUnit\Framework\TestCase;
  9. class GitTest extends TestCase
  10. {
  11. /** @var Git */
  12. private $git;
  13. /** @var IOInterface&\PHPUnit_Framework_MockObject_MockObject */
  14. private $io;
  15. /** @var Config&\PHPUnit_Framework_MockObject_MockObject */
  16. private $config;
  17. /** @var ProcessExecutor&\PHPUnit_Framework_MockObject_MockObject */
  18. private $process;
  19. /** @var Filesystem&\PHPUnit_Framework_MockObject_MockObject */
  20. private $fs;
  21. protected function setUp()
  22. {
  23. $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  24. $this->config = $this->getMockBuilder('Composer\Config')->disableOriginalConstructor()->getMock();
  25. $this->process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->disableOriginalConstructor()->getMock();
  26. $this->fs = $this->getMockBuilder('Composer\Util\Filesystem')->disableOriginalConstructor()->getMock();
  27. $this->git = new Git($this->io, $this->config, $this->process, $this->fs);
  28. }
  29. /**
  30. * @dataProvider publicGithubNoCredentialsProvider
  31. */
  32. public function testRunCommandPublicGitHubRepositoryNotInitialClone($protocol, $expectedUrl)
  33. {
  34. $that = $this;
  35. $commandCallable = function ($url) use ($that, $expectedUrl) {
  36. $that->assertSame($expectedUrl, $url);
  37. return 'git command';
  38. };
  39. $this->mockConfig($protocol);
  40. $this->process
  41. ->expects($this->once())
  42. ->method('execute')
  43. ->with($this->equalTo('git command'))
  44. ->willReturn(0);
  45. $this->git->runCommand($commandCallable, 'https://github.com/acme/repo', null, true);
  46. }
  47. public function publicGithubNoCredentialsProvider()
  48. {
  49. return array(
  50. array('ssh', 'git@github.com:acme/repo'),
  51. array('https', 'https://github.com/acme/repo'),
  52. );
  53. }
  54. /**
  55. * @expectedException \RuntimeException
  56. */
  57. public function testRunCommandPrivateGitHubRepositoryNotInitialCloneNotInteractiveWithoutAuthentication()
  58. {
  59. $that = $this;
  60. $commandCallable = function ($url) use ($that) {
  61. $that->assertSame('https://github.com/acme/repo', $url);
  62. return 'git command';
  63. };
  64. $this->mockConfig('https');
  65. $this->process
  66. ->method('execute')
  67. ->willReturnMap(array(
  68. array('git command', null, null, 1),
  69. array('git --version', null, null, 0),
  70. ));
  71. $this->git->runCommand($commandCallable, 'https://github.com/acme/repo', null, true);
  72. }
  73. /**
  74. * @dataProvider privateGithubWithCredentialsProvider
  75. */
  76. public function testRunCommandPrivateGitHubRepositoryNotInitialCloneNotInteractiveWithAuthentication($gitUrl, $protocol, $gitHubToken, $expectedUrl)
  77. {
  78. $commandCallable = function ($url) use ($expectedUrl) {
  79. if ($url !== $expectedUrl) {
  80. return 'git command failing';
  81. }
  82. return 'git command ok';
  83. };
  84. $this->mockConfig($protocol);
  85. $this->process
  86. ->method('execute')
  87. ->willReturnMap(array(
  88. array('git command failing', null, null, 1),
  89. array('git command ok', null, null, 0),
  90. ));
  91. $this->io
  92. ->method('isInteractive')
  93. ->willReturn(false);
  94. $this->io
  95. ->method('hasAuthentication')
  96. ->with($this->equalTo('github.com'))
  97. ->willReturn(true);
  98. $this->io
  99. ->method('getAuthentication')
  100. ->with($this->equalTo('github.com'))
  101. ->willReturn(array('username' => 'token', 'password' => $gitHubToken));
  102. $this->git->runCommand($commandCallable, $gitUrl, null, true);
  103. }
  104. public function privateGithubWithCredentialsProvider()
  105. {
  106. return array(
  107. array('git@github.com:acme/repo.git', 'ssh', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git'),
  108. array('https://github.com/acme/repo', 'https', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git'),
  109. );
  110. }
  111. private function mockConfig($protocol)
  112. {
  113. $this->config
  114. ->method('get')
  115. ->willReturnMap(array(
  116. array('github-domains', 0, array('github.com')),
  117. array('github-protocols', 0, array($protocol)),
  118. ));
  119. }
  120. }