GitHubDriverTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\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->getMock('Composer\IO\IOInterface');
  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->getMock('Composer\Util\ProcessExecutor');
  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->getMock('Composer\Config\ConfigSourceInterface');
  74. $authConfigSource = $this->getMock('Composer\Config\ConfigSourceInterface');
  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->getMock('Composer\IO\IOInterface');
  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->getMock('Composer\IO\IOInterface');
  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. $gitHubDriver->getComposerInformation($identifier);
  169. }
  170. public function testPrivateRepositoryNoInteraction()
  171. {
  172. $repoUrl = 'http://github.com/composer/packagist';
  173. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  174. $repoSshUrl = 'git@github.com:composer/packagist.git';
  175. $identifier = 'v0.0.0';
  176. $sha = 'SOMESHA';
  177. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  178. ->disableOriginalConstructor()
  179. ->getMock();
  180. $io = $this->getMock('Composer\IO\IOInterface');
  181. $io->expects($this->any())
  182. ->method('isInteractive')
  183. ->will($this->returnValue(false));
  184. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  185. ->setConstructorArgs(array($io))
  186. ->getMock();
  187. $remoteFilesystem->expects($this->at(0))
  188. ->method('getContents')
  189. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  190. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  191. // clean local clone if present
  192. $fs = new Filesystem();
  193. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  194. $process->expects($this->at(0))
  195. ->method('execute')
  196. ->with($this->equalTo('git config github.accesstoken'))
  197. ->will($this->returnValue(1));
  198. $process->expects($this->at(1))
  199. ->method('execute')
  200. ->with($this->stringContains($repoSshUrl))
  201. ->will($this->returnValue(0));
  202. $process->expects($this->at(2))
  203. ->method('execute')
  204. ->with($this->stringContains('git show-ref --tags'));
  205. $process->expects($this->at(3))
  206. ->method('splitLines')
  207. ->will($this->returnValue(array($sha.' refs/tags/'.$identifier)));
  208. $process->expects($this->at(4))
  209. ->method('execute')
  210. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  211. $process->expects($this->at(5))
  212. ->method('splitLines')
  213. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  214. $process->expects($this->at(6))
  215. ->method('execute')
  216. ->with($this->stringContains('git branch --no-color'));
  217. $process->expects($this->at(7))
  218. ->method('splitLines')
  219. ->will($this->returnValue(array('* test_master')));
  220. $repoConfig = array(
  221. 'url' => $repoUrl,
  222. );
  223. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  224. $gitHubDriver->initialize();
  225. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  226. $dist = $gitHubDriver->getDist($sha);
  227. $this->assertEquals('zip', $dist['type']);
  228. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  229. $this->assertEquals($sha, $dist['reference']);
  230. $source = $gitHubDriver->getSource($identifier);
  231. $this->assertEquals('git', $source['type']);
  232. $this->assertEquals($repoSshUrl, $source['url']);
  233. $this->assertEquals($identifier, $source['reference']);
  234. $source = $gitHubDriver->getSource($sha);
  235. $this->assertEquals('git', $source['type']);
  236. $this->assertEquals($repoSshUrl, $source['url']);
  237. $this->assertEquals($sha, $source['reference']);
  238. }
  239. protected function setAttribute($object, $attribute, $value)
  240. {
  241. $attr = new \ReflectionProperty($object, $attribute);
  242. $attr->setAccessible(true);
  243. $attr->setValue($object, $value);
  244. }
  245. }