GitTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Composer\Test\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. ->expects($this->atLeast(2))
  87. ->method('execute')
  88. ->willReturnMap(array(
  89. array('git command failing', null, null, 1),
  90. array('git command ok', null, null, 0),
  91. ));
  92. $this->io
  93. ->method('isInteractive')
  94. ->willReturn(false);
  95. $this->io
  96. ->expects($this->atLeastOnce())
  97. ->method('hasAuthentication')
  98. ->with($this->equalTo('github.com'))
  99. ->willReturn(true);
  100. $this->io
  101. ->expects($this->atLeastOnce())
  102. ->method('getAuthentication')
  103. ->with($this->equalTo('github.com'))
  104. ->willReturn(array('username' => 'token', 'password' => $gitHubToken));
  105. $this->git->runCommand($commandCallable, $gitUrl, null, true);
  106. }
  107. public function privateGithubWithCredentialsProvider()
  108. {
  109. return array(
  110. array('git@github.com:acme/repo.git', 'ssh', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git'),
  111. array('https://github.com/acme/repo', 'https', 'MY_GITHUB_TOKEN', 'https://token:MY_GITHUB_TOKEN@github.com/acme/repo.git'),
  112. );
  113. }
  114. private function mockConfig($protocol)
  115. {
  116. $this->config
  117. ->method('get')
  118. ->willReturnMap(array(
  119. array('github-domains', 0, array('github.com')),
  120. array('github-protocols', 0, array($protocol)),
  121. ));
  122. }
  123. }