GitHubDriverTest.php 8.8 KB

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