GitHubDriverTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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('ask')
  57. ->with($this->equalTo('Username: '))
  58. ->will($this->returnValue('someuser'));
  59. $io->expects($this->once())
  60. ->method('askAndHideAnswer')
  61. ->with($this->equalTo('Password: '))
  62. ->will($this->returnValue('somepassword'));
  63. $io->expects($this->any())
  64. ->method('setAuthentication')
  65. ->with($this->equalTo('github.com'), $this->matchesRegularExpression('{someuser|abcdef}'), $this->matchesRegularExpression('{somepassword|x-oauth-basic}'));
  66. $remoteFilesystem->expects($this->at(1))
  67. ->method('getContents')
  68. ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/authorizations'), $this->equalTo(false))
  69. ->will($this->returnValue('{"token": "abcdef"}'));
  70. $remoteFilesystem->expects($this->at(2))
  71. ->method('getContents')
  72. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  73. ->will($this->returnValue('{"master_branch": "test_master", "private": true}'));
  74. $configSource = $this->getMock('Composer\Config\ConfigSourceInterface');
  75. $this->config->setConfigSource($configSource);
  76. $repoConfig = array(
  77. 'url' => $repoUrl,
  78. );
  79. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  80. $gitHubDriver->initialize();
  81. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  82. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  83. $dist = $gitHubDriver->getDist($identifier);
  84. $this->assertEquals('zip', $dist['type']);
  85. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/v0.0.0', $dist['url']);
  86. $this->assertEquals('v0.0.0', $dist['reference']);
  87. $source = $gitHubDriver->getSource($identifier);
  88. $this->assertEquals('git', $source['type']);
  89. $this->assertEquals($repoSshUrl, $source['url']);
  90. $this->assertEquals('v0.0.0', $source['reference']);
  91. $dist = $gitHubDriver->getDist($sha);
  92. $this->assertEquals('zip', $dist['type']);
  93. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/v0.0.0', $dist['url']);
  94. $this->assertEquals('v0.0.0', $dist['reference']);
  95. $source = $gitHubDriver->getSource($sha);
  96. $this->assertEquals('git', $source['type']);
  97. $this->assertEquals($repoSshUrl, $source['url']);
  98. $this->assertEquals('v0.0.0', $source['reference']);
  99. }
  100. public function testPublicRepository()
  101. {
  102. $repoUrl = 'http://github.com/composer/packagist';
  103. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  104. $identifier = 'v0.0.0';
  105. $sha = 'SOMESHA';
  106. $io = $this->getMock('Composer\IO\IOInterface');
  107. $io->expects($this->any())
  108. ->method('isInteractive')
  109. ->will($this->returnValue(true));
  110. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  111. ->setConstructorArgs(array($io))
  112. ->getMock();
  113. $remoteFilesystem->expects($this->at(0))
  114. ->method('getContents')
  115. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  116. ->will($this->returnValue('{"master_branch": "test_master"}'));
  117. $repoConfig = array(
  118. 'url' => $repoUrl,
  119. );
  120. $repoUrl = 'https://github.com/composer/packagist.git';
  121. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  122. $gitHubDriver->initialize();
  123. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  124. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  125. $dist = $gitHubDriver->getDist($identifier);
  126. $this->assertEquals('zip', $dist['type']);
  127. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/v0.0.0', $dist['url']);
  128. $this->assertEquals($identifier, $dist['reference']);
  129. $source = $gitHubDriver->getSource($identifier);
  130. $this->assertEquals('git', $source['type']);
  131. $this->assertEquals($repoUrl, $source['url']);
  132. $this->assertEquals($identifier, $source['reference']);
  133. $dist = $gitHubDriver->getDist($sha);
  134. $this->assertEquals('zip', $dist['type']);
  135. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/v0.0.0', $dist['url']);
  136. $this->assertEquals($identifier, $dist['reference']);
  137. $source = $gitHubDriver->getSource($sha);
  138. $this->assertEquals('git', $source['type']);
  139. $this->assertEquals($repoUrl, $source['url']);
  140. $this->assertEquals($identifier, $source['reference']);
  141. }
  142. public function testPublicRepository2()
  143. {
  144. $repoUrl = 'http://github.com/composer/packagist';
  145. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  146. $identifier = 'feature/3.2-foo';
  147. $sha = 'SOMESHA';
  148. $io = $this->getMock('Composer\IO\IOInterface');
  149. $io->expects($this->any())
  150. ->method('isInteractive')
  151. ->will($this->returnValue(true));
  152. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  153. ->setConstructorArgs(array($io))
  154. ->getMock();
  155. $remoteFilesystem->expects($this->at(0))
  156. ->method('getContents')
  157. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  158. ->will($this->returnValue('{"master_branch": "test_master"}'));
  159. $remoteFilesystem->expects($this->at(1))
  160. ->method('getContents')
  161. ->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))
  162. ->will($this->returnValue('{"encoding":"base64","content":"'.base64_encode('{"support": {"source": "'.$repoUrl.'" }}').'"}'));
  163. $remoteFilesystem->expects($this->at(2))
  164. ->method('getContents')
  165. ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/repos/composer/packagist/commits/feature%2F3.2-foo'), $this->equalTo(false))
  166. ->will($this->returnValue('{"commit": {"committer":{ "date": "2012-09-10"}}}'));
  167. $repoConfig = array(
  168. 'url' => $repoUrl,
  169. );
  170. $repoUrl = 'https://github.com/composer/packagist.git';
  171. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  172. $gitHubDriver->initialize();
  173. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  174. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  175. $dist = $gitHubDriver->getDist($identifier);
  176. $this->assertEquals('zip', $dist['type']);
  177. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/feature/3.2-foo', $dist['url']);
  178. $this->assertEquals($identifier, $dist['reference']);
  179. $source = $gitHubDriver->getSource($identifier);
  180. $this->assertEquals('git', $source['type']);
  181. $this->assertEquals($repoUrl, $source['url']);
  182. $this->assertEquals($identifier, $source['reference']);
  183. $dist = $gitHubDriver->getDist($sha);
  184. $this->assertEquals('zip', $dist['type']);
  185. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/feature/3.2-foo', $dist['url']);
  186. $this->assertEquals($identifier, $dist['reference']);
  187. $source = $gitHubDriver->getSource($sha);
  188. $this->assertEquals('git', $source['type']);
  189. $this->assertEquals($repoUrl, $source['url']);
  190. $this->assertEquals($identifier, $source['reference']);
  191. $gitHubDriver->getComposerInformation($identifier);
  192. }
  193. public function testPrivateRepositoryNoInteraction()
  194. {
  195. $repoUrl = 'http://github.com/composer/packagist';
  196. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  197. $repoSshUrl = 'git@github.com:composer/packagist.git';
  198. $identifier = 'v0.0.0';
  199. $sha = 'SOMESHA';
  200. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  201. ->disableOriginalConstructor()
  202. ->getMock();
  203. $io = $this->getMock('Composer\IO\IOInterface');
  204. $io->expects($this->any())
  205. ->method('isInteractive')
  206. ->will($this->returnValue(false));
  207. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  208. ->setConstructorArgs(array($io))
  209. ->getMock();
  210. $remoteFilesystem->expects($this->at(0))
  211. ->method('getContents')
  212. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  213. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  214. // clean local clone if present
  215. $fs = new Filesystem();
  216. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  217. $process->expects($this->at(0))
  218. ->method('execute')
  219. ->with($this->equalTo('git config github.accesstoken'))
  220. ->will($this->returnValue(1));
  221. $process->expects($this->at(1))
  222. ->method('execute')
  223. ->with($this->stringContains($repoSshUrl))
  224. ->will($this->returnValue(0));
  225. $process->expects($this->at(2))
  226. ->method('execute')
  227. ->with($this->stringContains('git tag'));
  228. $process->expects($this->at(3))
  229. ->method('splitLines')
  230. ->will($this->returnValue(array($identifier)));
  231. $process->expects($this->at(4))
  232. ->method('execute')
  233. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  234. $process->expects($this->at(5))
  235. ->method('splitLines')
  236. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  237. $process->expects($this->at(6))
  238. ->method('execute')
  239. ->with($this->stringContains('git branch --no-color'));
  240. $process->expects($this->at(7))
  241. ->method('splitLines')
  242. ->will($this->returnValue(array('* test_master')));
  243. $repoConfig = array(
  244. 'url' => $repoUrl,
  245. );
  246. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  247. $gitHubDriver->initialize();
  248. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  249. // Dist is not available for GitDriver
  250. $dist = $gitHubDriver->getDist($identifier);
  251. $this->assertNull($dist);
  252. $source = $gitHubDriver->getSource($identifier);
  253. $this->assertEquals('git', $source['type']);
  254. $this->assertEquals($repoSshUrl, $source['url']);
  255. $this->assertEquals($identifier, $source['reference']);
  256. // Dist is not available for GitDriver
  257. $dist = $gitHubDriver->getDist($sha);
  258. $this->assertNull($dist);
  259. $source = $gitHubDriver->getSource($sha);
  260. $this->assertEquals('git', $source['type']);
  261. $this->assertEquals($repoSshUrl, $source['url']);
  262. $this->assertEquals($sha, $source['reference']);
  263. }
  264. protected function setAttribute($object, $attribute, $value)
  265. {
  266. $attr = new \ReflectionProperty($object, $attribute);
  267. $attr->setAccessible(true);
  268. $attr->setValue($object, $value);
  269. }
  270. }