GitHubDriverTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Repository\Vcs;
  12. use Composer\Downloader\TransportException;
  13. use Composer\Repository\Vcs\GitHubDriver;
  14. /**
  15. * @author Beau Simensen <beau@dflydev.com>
  16. */
  17. class GitHubDriverTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testPrivateRepository()
  20. {
  21. $repoUrl = 'http://github.com/composer/packagist';
  22. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  23. $repoSshUrl = 'git@github.com:composer/packagist.git';
  24. $identifier = 'v0.0.0';
  25. $sha = 'SOMESHA';
  26. $io = $this->getMock('Composer\IO\IOInterface');
  27. $io->expects($this->any())
  28. ->method('isInteractive')
  29. ->will($this->returnValue(true));
  30. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  31. ->setConstructorArgs(array($io))
  32. ->getMock();
  33. $remoteFilesystem->expects($this->at(0))
  34. ->method('getContents')
  35. ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
  36. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  37. $io->expects($this->once())
  38. ->method('ask')
  39. ->with($this->equalTo('Username: '))
  40. ->will($this->returnValue('someuser'));
  41. $io->expects($this->once())
  42. ->method('askAndHideAnswer')
  43. ->with($this->equalTo('Password: '))
  44. ->will($this->returnValue('somepassword'));
  45. $io->expects($this->once())
  46. ->method('setAuthorization')
  47. ->with($this->equalTo($repoUrl), 'someuser', 'somepassword');
  48. $remoteFilesystem->expects($this->at(1))
  49. ->method('getContents')
  50. ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
  51. ->will($this->returnValue('{"master_branch": "test_master"}'));
  52. $gitHubDriver = new GitHubDriver($repoUrl, $io, null, $remoteFilesystem);
  53. $gitHubDriver->initialize();
  54. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  55. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  56. $dist = $gitHubDriver->getDist($identifier);
  57. $this->assertEquals('zip', $dist['type']);
  58. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  59. $this->assertEquals('v0.0.0', $dist['reference']);
  60. $source = $gitHubDriver->getSource($identifier);
  61. $this->assertEquals('git', $source['type']);
  62. $this->assertEquals($repoSshUrl, $source['url']);
  63. $this->assertEquals('v0.0.0', $source['reference']);
  64. $dist = $gitHubDriver->getDist($sha);
  65. $this->assertEquals('zip', $dist['type']);
  66. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  67. $this->assertEquals('v0.0.0', $dist['reference']);
  68. $source = $gitHubDriver->getSource($sha);
  69. $this->assertEquals('git', $source['type']);
  70. $this->assertEquals($repoSshUrl, $source['url']);
  71. $this->assertEquals('v0.0.0', $source['reference']);
  72. }
  73. public function testPublicRepository()
  74. {
  75. $repoUrl = 'http://github.com/composer/packagist';
  76. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  77. $identifier = 'v0.0.0';
  78. $sha = 'SOMESHA';
  79. $io = $this->getMock('Composer\IO\IOInterface');
  80. $io->expects($this->any())
  81. ->method('isInteractive')
  82. ->will($this->returnValue(true));
  83. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  84. ->setConstructorArgs(array($io))
  85. ->getMock();
  86. $remoteFilesystem->expects($this->at(0))
  87. ->method('getContents')
  88. ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
  89. ->will($this->returnValue('{"master_branch": "test_master"}'));
  90. $gitHubDriver = new GitHubDriver($repoUrl, $io, null, $remoteFilesystem);
  91. $gitHubDriver->initialize();
  92. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  93. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  94. $dist = $gitHubDriver->getDist($identifier);
  95. $this->assertEquals('zip', $dist['type']);
  96. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  97. $this->assertEquals($identifier, $dist['reference']);
  98. $source = $gitHubDriver->getSource($identifier);
  99. $this->assertEquals('git', $source['type']);
  100. $this->assertEquals($repoUrl, $source['url']);
  101. $this->assertEquals($identifier, $source['reference']);
  102. $dist = $gitHubDriver->getDist($sha);
  103. $this->assertEquals('zip', $dist['type']);
  104. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  105. $this->assertEquals($identifier, $dist['reference']);
  106. $source = $gitHubDriver->getSource($sha);
  107. $this->assertEquals('git', $source['type']);
  108. $this->assertEquals($repoUrl, $source['url']);
  109. $this->assertEquals($identifier, $source['reference']);
  110. }
  111. public function testPrivateRepositoryNoInteraction()
  112. {
  113. $repoUrl = 'http://github.com/composer/packagist';
  114. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  115. $repoSshUrl = 'git@github.com:composer/packagist.git';
  116. $identifier = 'v0.0.0';
  117. $sha = 'SOMESHA';
  118. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $io = $this->getMock('Composer\IO\IOInterface');
  122. $io->expects($this->any())
  123. ->method('isInteractive')
  124. ->will($this->returnValue(false));
  125. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  126. ->setConstructorArgs(array($io))
  127. ->getMock();
  128. $remoteFilesystem->expects($this->at(0))
  129. ->method('getContents')
  130. ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
  131. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  132. $process->expects($this->at(0))
  133. ->method('execute')
  134. ->with($this->stringContains('git fetch origin'));
  135. $process->expects($this->at(1))
  136. ->method('execute')
  137. ->with($this->stringContains('git tag'));
  138. $process->expects($this->at(2))
  139. ->method('splitLines')
  140. ->will($this->returnValue(array($identifier)));
  141. $process->expects($this->at(3))
  142. ->method('execute')
  143. ->with($this->stringContains('git branch'));
  144. $process->expects($this->at(4))
  145. ->method('splitLines')
  146. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  147. $process->expects($this->at(5))
  148. ->method('execute')
  149. ->with($this->stringContains('git branch'));
  150. $process->expects($this->at(6))
  151. ->method('splitLines')
  152. ->will($this->returnValue(array(' upstream/HEAD -> upstream/test_master')));
  153. $gitHubDriver = new GitHubDriver($repoUrl, $io, $process, $remoteFilesystem);
  154. $gitHubDriver->initialize();
  155. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  156. // Dist is not available for GitDriver
  157. $dist = $gitHubDriver->getDist($identifier);
  158. $this->assertNull($dist);
  159. $source = $gitHubDriver->getSource($identifier);
  160. $this->assertEquals('git', $source['type']);
  161. $this->assertEquals($repoSshUrl, $source['url']);
  162. $this->assertEquals($identifier, $source['reference']);
  163. // Dist is not available for GitDriver
  164. $dist = $gitHubDriver->getDist($sha);
  165. $this->assertNull($dist);
  166. $source = $gitHubDriver->getSource($sha);
  167. $this->assertEquals('git', $source['type']);
  168. $this->assertEquals($repoSshUrl, $source['url']);
  169. $this->assertEquals($sha, $source['reference']);
  170. }
  171. protected function setAttribute($object, $attribute, $value)
  172. {
  173. $attr = new \ReflectionProperty($object, $attribute);
  174. $attr->setAccessible(true);
  175. $attr->setValue($object, $value);
  176. }
  177. }