GitHubDriverTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, function() use ($remoteFilesystem) { return $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, function() use ($remoteFilesystem) {
  91. return $remoteFilesystem;
  92. });
  93. $gitHubDriver->initialize();
  94. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  95. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  96. $dist = $gitHubDriver->getDist($identifier);
  97. $this->assertEquals('zip', $dist['type']);
  98. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  99. $this->assertEquals($identifier, $dist['reference']);
  100. $source = $gitHubDriver->getSource($identifier);
  101. $this->assertEquals('git', $source['type']);
  102. $this->assertEquals($repoUrl, $source['url']);
  103. $this->assertEquals($identifier, $source['reference']);
  104. $dist = $gitHubDriver->getDist($sha);
  105. $this->assertEquals('zip', $dist['type']);
  106. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  107. $this->assertEquals($identifier, $dist['reference']);
  108. $source = $gitHubDriver->getSource($sha);
  109. $this->assertEquals('git', $source['type']);
  110. $this->assertEquals($repoUrl, $source['url']);
  111. $this->assertEquals($identifier, $source['reference']);
  112. }
  113. public function testPrivateRepositoryNoInteraction()
  114. {
  115. $repoUrl = 'http://github.com/composer/packagist';
  116. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  117. $repoSshUrl = 'git@github.com:composer/packagist.git';
  118. $identifier = 'v0.0.0';
  119. $sha = 'SOMESHA';
  120. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $io = $this->getMock('Composer\IO\IOInterface');
  124. $io->expects($this->any())
  125. ->method('isInteractive')
  126. ->will($this->returnValue(false));
  127. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  128. ->setConstructorArgs(array($io))
  129. ->getMock();
  130. $remoteFilesystem->expects($this->at(0))
  131. ->method('getContents')
  132. ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false))
  133. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  134. $process->expects($this->at(0))
  135. ->method('execute')
  136. ->with($this->stringContains('git fetch origin'));
  137. $process->expects($this->at(1))
  138. ->method('execute')
  139. ->with($this->stringContains('git tag'));
  140. $process->expects($this->at(2))
  141. ->method('splitLines')
  142. ->will($this->returnValue(array($identifier)));
  143. $process->expects($this->at(3))
  144. ->method('execute')
  145. ->with($this->stringContains('git branch'));
  146. $process->expects($this->at(4))
  147. ->method('splitLines')
  148. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  149. $process->expects($this->at(5))
  150. ->method('execute')
  151. ->with($this->stringContains('git branch'));
  152. $process->expects($this->at(6))
  153. ->method('splitLines')
  154. ->will($this->returnValue(array(' upstream/HEAD -> upstream/test_master')));
  155. $gitHubDriver = new GitHubDriver($repoUrl, $io, $process, function() use ($remoteFilesystem) {
  156. return $remoteFilesystem;
  157. });
  158. $gitHubDriver->initialize();
  159. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  160. // Dist is not available for GitDriver
  161. $dist = $gitHubDriver->getDist($identifier);
  162. $this->assertNull($dist);
  163. $source = $gitHubDriver->getSource($identifier);
  164. $this->assertEquals('git', $source['type']);
  165. $this->assertEquals($repoSshUrl, $source['url']);
  166. $this->assertEquals($identifier, $source['reference']);
  167. // Dist is not available for GitDriver
  168. $dist = $gitHubDriver->getDist($sha);
  169. $this->assertNull($dist);
  170. $source = $gitHubDriver->getSource($sha);
  171. $this->assertEquals('git', $source['type']);
  172. $this->assertEquals($repoSshUrl, $source['url']);
  173. $this->assertEquals($sha, $source['reference']);
  174. }
  175. protected function setAttribute($object, $attribute, $value)
  176. {
  177. $attr = new \ReflectionProperty($object, $attribute);
  178. $attr->setAccessible(true);
  179. $attr->setValue($object, $value);
  180. }
  181. }