GitHubDriverTest.php 9.3 KB

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