GitHubDriverTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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\Test\TestCase;
  15. use Composer\Util\Filesystem;
  16. use Composer\Util\Http\Response;
  17. use Composer\Config;
  18. class GitHubDriverTest extends TestCase
  19. {
  20. private $home;
  21. private $config;
  22. public function setUp()
  23. {
  24. $this->home = $this->getUniqueTmpDirectory();
  25. $this->config = new Config();
  26. $this->config->merge(array(
  27. 'config' => array(
  28. 'home' => $this->home,
  29. ),
  30. ));
  31. }
  32. public function tearDown()
  33. {
  34. $fs = new Filesystem;
  35. $fs->removeDirectory($this->home);
  36. }
  37. public function testPrivateRepository()
  38. {
  39. $repoUrl = 'http://github.com/composer/packagist';
  40. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  41. $repoSshUrl = 'git@github.com:composer/packagist.git';
  42. $identifier = 'v0.0.0';
  43. $sha = 'SOMESHA';
  44. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  45. $io->expects($this->any())
  46. ->method('isInteractive')
  47. ->will($this->returnValue(true));
  48. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  49. ->setConstructorArgs(array($io, $this->config))
  50. ->getMock();
  51. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock();
  52. $process->expects($this->any())
  53. ->method('execute')
  54. ->will($this->returnValue(1));
  55. $httpDownloader->expects($this->at(0))
  56. ->method('get')
  57. ->with($this->equalTo($repoApiUrl))
  58. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  59. $io->expects($this->once())
  60. ->method('askAndHideAnswer')
  61. ->with($this->equalTo('Token (hidden): '))
  62. ->will($this->returnValue('sometoken'));
  63. $io->expects($this->any())
  64. ->method('setAuthentication')
  65. ->with($this->equalTo('github.com'), $this->matchesRegularExpression('{sometoken}'), $this->matchesRegularExpression('{x-oauth-basic}'));
  66. $httpDownloader->expects($this->at(1))
  67. ->method('get')
  68. ->with($this->equalTo($url = 'https://api.github.com/'))
  69. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{}')));
  70. $httpDownloader->expects($this->at(2))
  71. ->method('get')
  72. ->with($this->equalTo($url = $repoApiUrl))
  73. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"master_branch": "test_master", "private": true, "owner": {"login": "composer"}, "name": "packagist"}')));
  74. $configSource = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  75. $authConfigSource = $this->getMockBuilder('Composer\Config\ConfigSourceInterface')->getMock();
  76. $this->config->setConfigSource($configSource);
  77. $this->config->setAuthConfigSource($authConfigSource);
  78. $repoConfig = array(
  79. 'url' => $repoUrl,
  80. );
  81. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process);
  82. $gitHubDriver->initialize();
  83. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  84. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  85. $dist = $gitHubDriver->getDist($sha);
  86. $this->assertEquals('zip', $dist['type']);
  87. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  88. $this->assertEquals('SOMESHA', $dist['reference']);
  89. $source = $gitHubDriver->getSource($sha);
  90. $this->assertEquals('git', $source['type']);
  91. $this->assertEquals($repoSshUrl, $source['url']);
  92. $this->assertEquals('SOMESHA', $source['reference']);
  93. }
  94. public function testPublicRepository()
  95. {
  96. $repoUrl = 'http://github.com/composer/packagist';
  97. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  98. $identifier = 'v0.0.0';
  99. $sha = 'SOMESHA';
  100. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  101. $io->expects($this->any())
  102. ->method('isInteractive')
  103. ->will($this->returnValue(true));
  104. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  105. ->setConstructorArgs(array($io, $this->config))
  106. ->getMock();
  107. $httpDownloader->expects($this->at(0))
  108. ->method('get')
  109. ->with($this->equalTo($repoApiUrl))
  110. ->will($this->returnValue(new Response(array('url' => $repoApiUrl), 200, array(), '{"master_branch": "test_master", "owner": {"login": "composer"}, "name": "packagist"}')));
  111. $repoConfig = array(
  112. 'url' => $repoUrl,
  113. );
  114. $repoUrl = 'https://github.com/composer/packagist.git';
  115. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process);
  119. $gitHubDriver->initialize();
  120. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  121. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  122. $dist = $gitHubDriver->getDist($sha);
  123. $this->assertEquals('zip', $dist['type']);
  124. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  125. $this->assertEquals($sha, $dist['reference']);
  126. $source = $gitHubDriver->getSource($sha);
  127. $this->assertEquals('git', $source['type']);
  128. $this->assertEquals($repoUrl, $source['url']);
  129. $this->assertEquals($sha, $source['reference']);
  130. }
  131. public function testPublicRepository2()
  132. {
  133. $repoUrl = 'http://github.com/composer/packagist';
  134. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  135. $identifier = 'feature/3.2-foo';
  136. $sha = 'SOMESHA';
  137. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  138. $io->expects($this->any())
  139. ->method('isInteractive')
  140. ->will($this->returnValue(true));
  141. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  142. ->setConstructorArgs(array($io, $this->config))
  143. ->getMock();
  144. $httpDownloader->expects($this->at(0))
  145. ->method('get')
  146. ->with($this->equalTo($url = $repoApiUrl))
  147. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"master_branch": "test_master", "owner": {"login": "composer"}, "name": "packagist"}')));
  148. $httpDownloader->expects($this->at(1))
  149. ->method('get')
  150. ->with($this->equalTo($url = 'https://api.github.com/repos/composer/packagist/contents/composer.json?ref=feature%2F3.2-foo'))
  151. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"encoding":"base64","content":"'.base64_encode('{"support": {"source": "'.$repoUrl.'" }}').'"}')));
  152. $httpDownloader->expects($this->at(2))
  153. ->method('get')
  154. ->with($this->equalTo($url = 'https://api.github.com/repos/composer/packagist/commits/feature%2F3.2-foo'))
  155. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"commit": {"committer":{ "date": "2012-09-10"}}}')));
  156. $httpDownloader->expects($this->at(3))
  157. ->method('get')
  158. ->with($this->equalTo($url = 'https://api.github.com/repos/composer/packagist/contents/.github/FUNDING.yml'))
  159. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"encoding": "base64", "content": "'.base64_encode("custom: https://example.com").'"}')));
  160. $repoConfig = array(
  161. 'url' => $repoUrl,
  162. );
  163. $repoUrl = 'https://github.com/composer/packagist.git';
  164. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config,$httpDownloader, $process);
  168. $gitHubDriver->initialize();
  169. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  170. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  171. $dist = $gitHubDriver->getDist($sha);
  172. $this->assertEquals('zip', $dist['type']);
  173. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  174. $this->assertEquals($sha, $dist['reference']);
  175. $source = $gitHubDriver->getSource($sha);
  176. $this->assertEquals('git', $source['type']);
  177. $this->assertEquals($repoUrl, $source['url']);
  178. $this->assertEquals($sha, $source['reference']);
  179. $data = $gitHubDriver->getComposerInformation($identifier);
  180. $this->assertArrayNotHasKey('abandoned', $data);
  181. }
  182. public function testPublicRepositoryArchived()
  183. {
  184. $repoUrl = 'http://github.com/composer/packagist';
  185. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  186. $identifier = 'v0.0.0';
  187. $sha = 'SOMESHA';
  188. $composerJsonUrl = 'https://api.github.com/repos/composer/packagist/contents/composer.json?ref=' . $sha;
  189. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  190. $io->expects($this->any())
  191. ->method('isInteractive')
  192. ->will($this->returnValue(true));
  193. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  194. ->disableOriginalConstructor()
  195. ->getMock();
  196. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  197. ->setConstructorArgs(array($io, $this->config))
  198. ->getMock();
  199. $httpDownloader->expects($this->at(0))
  200. ->method('get')
  201. ->with($this->equalTo($repoApiUrl))
  202. ->will($this->returnValue(new Response(array('url' => $repoApiUrl), 200, array(), '{"master_branch": "test_master", "owner": {"login": "composer"}, "name": "packagist", "archived": true}')));
  203. $httpDownloader->expects($this->at(1))
  204. ->method('get')
  205. ->with($this->equalTo($composerJsonUrl))
  206. ->will($this->returnValue(new Response(array('url' => $composerJsonUrl), 200, array(), '{"encoding": "base64", "content": "' . base64_encode('{"name": "composer/packagist"}') . '"}')));
  207. $httpDownloader->expects($this->at(2))
  208. ->method('get')
  209. ->with($this->equalTo($url = 'https://api.github.com/repos/composer/packagist/commits/'.$sha))
  210. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"commit": {"committer":{ "date": "2012-09-10"}}}')));
  211. $httpDownloader->expects($this->at(3))
  212. ->method('get')
  213. ->with($this->equalTo($url = 'https://api.github.com/repos/composer/packagist/contents/.github/FUNDING.yml'))
  214. ->will($this->returnValue(new Response(array('url' => $url), 200, array(), '{"encoding": "base64", "content": "'.base64_encode("custom: https://example.com").'"}')));
  215. $repoConfig = array(
  216. 'url' => $repoUrl,
  217. );
  218. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process);
  219. $gitHubDriver->initialize();
  220. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  221. $data = $gitHubDriver->getComposerInformation($sha);
  222. $this->assertTrue($data['abandoned']);
  223. }
  224. public function testPrivateRepositoryNoInteraction()
  225. {
  226. $repoUrl = 'http://github.com/composer/packagist';
  227. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  228. $repoSshUrl = 'git@github.com:composer/packagist.git';
  229. $identifier = 'v0.0.0';
  230. $sha = 'SOMESHA';
  231. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  232. ->disableOriginalConstructor()
  233. ->getMock();
  234. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  235. $io->expects($this->any())
  236. ->method('isInteractive')
  237. ->will($this->returnValue(false));
  238. $httpDownloader = $this->getMockBuilder('Composer\Util\HttpDownloader')
  239. ->setConstructorArgs(array($io, $this->config))
  240. ->getMock();
  241. $httpDownloader->expects($this->at(0))
  242. ->method('get')
  243. ->with($this->equalTo($repoApiUrl))
  244. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  245. // clean local clone if present
  246. $fs = new Filesystem();
  247. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  248. $process->expects($this->at(0))
  249. ->method('execute')
  250. ->with($this->equalTo('git config github.accesstoken'))
  251. ->will($this->returnValue(1));
  252. $process->expects($this->at(1))
  253. ->method('execute')
  254. ->with($this->stringContains($repoSshUrl))
  255. ->will($this->returnValue(0));
  256. $process->expects($this->at(2))
  257. ->method('execute')
  258. ->with($this->stringContains('git show-ref --tags'));
  259. $process->expects($this->at(3))
  260. ->method('splitLines')
  261. ->will($this->returnValue(array($sha.' refs/tags/'.$identifier)));
  262. $process->expects($this->at(4))
  263. ->method('execute')
  264. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  265. $process->expects($this->at(5))
  266. ->method('splitLines')
  267. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  268. $process->expects($this->at(6))
  269. ->method('execute')
  270. ->with($this->stringContains('git branch --no-color'));
  271. $process->expects($this->at(7))
  272. ->method('splitLines')
  273. ->will($this->returnValue(array('* test_master')));
  274. $repoConfig = array(
  275. 'url' => $repoUrl,
  276. );
  277. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $httpDownloader, $process);
  278. $gitHubDriver->initialize();
  279. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  280. $dist = $gitHubDriver->getDist($sha);
  281. $this->assertEquals('zip', $dist['type']);
  282. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  283. $this->assertEquals($sha, $dist['reference']);
  284. $source = $gitHubDriver->getSource($identifier);
  285. $this->assertEquals('git', $source['type']);
  286. $this->assertEquals($repoSshUrl, $source['url']);
  287. $this->assertEquals($identifier, $source['reference']);
  288. $source = $gitHubDriver->getSource($sha);
  289. $this->assertEquals('git', $source['type']);
  290. $this->assertEquals($repoSshUrl, $source['url']);
  291. $this->assertEquals($sha, $source['reference']);
  292. }
  293. protected function setAttribute($object, $attribute, $value)
  294. {
  295. $attr = new \ReflectionProperty($object, $attribute);
  296. $attr->setAccessible(true);
  297. $attr->setValue($object, $value);
  298. }
  299. }