GitHubDriverTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 tearDown()
  29. {
  30. $fs = new Filesystem;
  31. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  32. }
  33. public function testPrivateRepository()
  34. {
  35. $repoUrl = 'http://github.com/composer/packagist';
  36. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  37. $repoSshUrl = 'git@github.com:composer/packagist.git';
  38. $identifier = 'v0.0.0';
  39. $sha = 'SOMESHA';
  40. $io = $this->getMock('Composer\IO\IOInterface');
  41. $io->expects($this->any())
  42. ->method('isInteractive')
  43. ->will($this->returnValue(true));
  44. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  45. ->setConstructorArgs(array($io))
  46. ->getMock();
  47. $process = $this->getMock('Composer\Util\ProcessExecutor');
  48. $process->expects($this->any())
  49. ->method('execute')
  50. ->will($this->returnValue(1));
  51. $remoteFilesystem->expects($this->at(0))
  52. ->method('getContents')
  53. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  54. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  55. $io->expects($this->once())
  56. ->method('askAndHideAnswer')
  57. ->with($this->equalTo('Token (hidden): '))
  58. ->will($this->returnValue('sometoken'));
  59. $io->expects($this->any())
  60. ->method('setAuthentication')
  61. ->with($this->equalTo('github.com'), $this->matchesRegularExpression('{sometoken}'), $this->matchesRegularExpression('{x-oauth-basic}'));
  62. $remoteFilesystem->expects($this->at(1))
  63. ->method('getContents')
  64. ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/rate_limit'), $this->equalTo(false))
  65. ->will($this->returnValue('{}'));
  66. $remoteFilesystem->expects($this->at(2))
  67. ->method('getContents')
  68. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  69. ->will($this->returnValue('{"master_branch": "test_master", "private": true, "owner": {"login": "composer"}, "name": "packagist"}'));
  70. $configSource = $this->getMock('Composer\Config\ConfigSourceInterface');
  71. $authConfigSource = $this->getMock('Composer\Config\ConfigSourceInterface');
  72. $this->config->setConfigSource($configSource);
  73. $this->config->setAuthConfigSource($authConfigSource);
  74. $repoConfig = array(
  75. 'url' => $repoUrl,
  76. );
  77. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  78. $gitHubDriver->initialize();
  79. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  80. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  81. $dist = $gitHubDriver->getDist($sha);
  82. $this->assertEquals('zip', $dist['type']);
  83. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  84. $this->assertEquals('SOMESHA', $dist['reference']);
  85. $source = $gitHubDriver->getSource($sha);
  86. $this->assertEquals('git', $source['type']);
  87. $this->assertEquals($repoSshUrl, $source['url']);
  88. $this->assertEquals('SOMESHA', $source['reference']);
  89. }
  90. public function testPublicRepository()
  91. {
  92. $repoUrl = 'http://github.com/composer/packagist';
  93. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  94. $identifier = 'v0.0.0';
  95. $sha = 'SOMESHA';
  96. $io = $this->getMock('Composer\IO\IOInterface');
  97. $io->expects($this->any())
  98. ->method('isInteractive')
  99. ->will($this->returnValue(true));
  100. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  101. ->setConstructorArgs(array($io))
  102. ->getMock();
  103. $remoteFilesystem->expects($this->at(0))
  104. ->method('getContents')
  105. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  106. ->will($this->returnValue('{"master_branch": "test_master", "owner": {"login": "composer"}, "name": "packagist"}'));
  107. $repoConfig = array(
  108. 'url' => $repoUrl,
  109. );
  110. $repoUrl = 'https://github.com/composer/packagist.git';
  111. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  112. $gitHubDriver->initialize();
  113. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  114. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  115. $dist = $gitHubDriver->getDist($sha);
  116. $this->assertEquals('zip', $dist['type']);
  117. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  118. $this->assertEquals($sha, $dist['reference']);
  119. $source = $gitHubDriver->getSource($sha);
  120. $this->assertEquals('git', $source['type']);
  121. $this->assertEquals($repoUrl, $source['url']);
  122. $this->assertEquals($sha, $source['reference']);
  123. }
  124. public function testPublicRepository2()
  125. {
  126. $repoUrl = 'http://github.com/composer/packagist';
  127. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  128. $identifier = 'feature/3.2-foo';
  129. $sha = 'SOMESHA';
  130. $io = $this->getMock('Composer\IO\IOInterface');
  131. $io->expects($this->any())
  132. ->method('isInteractive')
  133. ->will($this->returnValue(true));
  134. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  135. ->setConstructorArgs(array($io))
  136. ->getMock();
  137. $remoteFilesystem->expects($this->at(0))
  138. ->method('getContents')
  139. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  140. ->will($this->returnValue('{"master_branch": "test_master", "owner": {"login": "composer"}, "name": "packagist"}'));
  141. $remoteFilesystem->expects($this->at(1))
  142. ->method('getContents')
  143. ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/repos/composer/packagist/contents/composer.json?ref=feature%2F3.2-foo'), $this->equalTo(false))
  144. ->will($this->returnValue('{"encoding":"base64","content":"'.base64_encode('{"support": {"source": "'.$repoUrl.'" }}').'"}'));
  145. $remoteFilesystem->expects($this->at(2))
  146. ->method('getContents')
  147. ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/repos/composer/packagist/commits/feature%2F3.2-foo'), $this->equalTo(false))
  148. ->will($this->returnValue('{"commit": {"committer":{ "date": "2012-09-10"}}}'));
  149. $repoConfig = array(
  150. 'url' => $repoUrl,
  151. );
  152. $repoUrl = 'https://github.com/composer/packagist.git';
  153. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  154. $gitHubDriver->initialize();
  155. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  156. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  157. $dist = $gitHubDriver->getDist($sha);
  158. $this->assertEquals('zip', $dist['type']);
  159. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  160. $this->assertEquals($sha, $dist['reference']);
  161. $source = $gitHubDriver->getSource($sha);
  162. $this->assertEquals('git', $source['type']);
  163. $this->assertEquals($repoUrl, $source['url']);
  164. $this->assertEquals($sha, $source['reference']);
  165. $gitHubDriver->getComposerInformation($identifier);
  166. }
  167. public function testPrivateRepositoryNoInteraction()
  168. {
  169. $repoUrl = 'http://github.com/composer/packagist';
  170. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  171. $repoSshUrl = 'git@github.com:composer/packagist.git';
  172. $identifier = 'v0.0.0';
  173. $sha = 'SOMESHA';
  174. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $io = $this->getMock('Composer\IO\IOInterface');
  178. $io->expects($this->any())
  179. ->method('isInteractive')
  180. ->will($this->returnValue(false));
  181. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  182. ->setConstructorArgs(array($io))
  183. ->getMock();
  184. $remoteFilesystem->expects($this->at(0))
  185. ->method('getContents')
  186. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  187. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  188. // clean local clone if present
  189. $fs = new Filesystem();
  190. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  191. $process->expects($this->at(0))
  192. ->method('execute')
  193. ->with($this->equalTo('git config github.accesstoken'))
  194. ->will($this->returnValue(1));
  195. $process->expects($this->at(1))
  196. ->method('execute')
  197. ->with($this->stringContains($repoSshUrl))
  198. ->will($this->returnValue(0));
  199. $process->expects($this->at(2))
  200. ->method('execute')
  201. ->with($this->stringContains('git show-ref --tags'));
  202. $process->expects($this->at(3))
  203. ->method('splitLines')
  204. ->will($this->returnValue(array($sha.' refs/tags/'.$identifier)));
  205. $process->expects($this->at(4))
  206. ->method('execute')
  207. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  208. $process->expects($this->at(5))
  209. ->method('splitLines')
  210. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  211. $process->expects($this->at(6))
  212. ->method('execute')
  213. ->with($this->stringContains('git branch --no-color'));
  214. $process->expects($this->at(7))
  215. ->method('splitLines')
  216. ->will($this->returnValue(array('* test_master')));
  217. $repoConfig = array(
  218. 'url' => $repoUrl,
  219. );
  220. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  221. $gitHubDriver->initialize();
  222. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  223. $dist = $gitHubDriver->getDist($sha);
  224. $this->assertEquals('zip', $dist['type']);
  225. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  226. $this->assertEquals($sha, $dist['reference']);
  227. $source = $gitHubDriver->getSource($identifier);
  228. $this->assertEquals('git', $source['type']);
  229. $this->assertEquals($repoSshUrl, $source['url']);
  230. $this->assertEquals($identifier, $source['reference']);
  231. $source = $gitHubDriver->getSource($sha);
  232. $this->assertEquals('git', $source['type']);
  233. $this->assertEquals($repoSshUrl, $source['url']);
  234. $this->assertEquals($sha, $source['reference']);
  235. }
  236. protected function setAttribute($object, $attribute, $value)
  237. {
  238. $attr = new \ReflectionProperty($object, $attribute);
  239. $attr->setAccessible(true);
  240. $attr->setValue($object, $value);
  241. }
  242. }