GitHubDriverTest.php 14 KB

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