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\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($sha);
  84. $this->assertEquals('zip', $dist['type']);
  85. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  86. $this->assertEquals('SOMESHA', $dist['reference']);
  87. $source = $gitHubDriver->getSource($sha);
  88. $this->assertEquals('git', $source['type']);
  89. $this->assertEquals($repoSshUrl, $source['url']);
  90. $this->assertEquals('SOMESHA', $source['reference']);
  91. }
  92. public function testPublicRepository()
  93. {
  94. $repoUrl = 'http://github.com/composer/packagist';
  95. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  96. $identifier = 'v0.0.0';
  97. $sha = 'SOMESHA';
  98. $io = $this->getMock('Composer\IO\IOInterface');
  99. $io->expects($this->any())
  100. ->method('isInteractive')
  101. ->will($this->returnValue(true));
  102. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  103. ->setConstructorArgs(array($io))
  104. ->getMock();
  105. $remoteFilesystem->expects($this->at(0))
  106. ->method('getContents')
  107. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  108. ->will($this->returnValue('{"master_branch": "test_master"}'));
  109. $repoConfig = array(
  110. 'url' => $repoUrl,
  111. );
  112. $repoUrl = 'https://github.com/composer/packagist.git';
  113. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  114. $gitHubDriver->initialize();
  115. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  116. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  117. $dist = $gitHubDriver->getDist($sha);
  118. $this->assertEquals('zip', $dist['type']);
  119. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  120. $this->assertEquals($sha, $dist['reference']);
  121. $source = $gitHubDriver->getSource($sha);
  122. $this->assertEquals('git', $source['type']);
  123. $this->assertEquals($repoUrl, $source['url']);
  124. $this->assertEquals($sha, $source['reference']);
  125. }
  126. public function testPublicRepository2()
  127. {
  128. $repoUrl = 'http://github.com/composer/packagist';
  129. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  130. $identifier = 'feature/3.2-foo';
  131. $sha = 'SOMESHA';
  132. $io = $this->getMock('Composer\IO\IOInterface');
  133. $io->expects($this->any())
  134. ->method('isInteractive')
  135. ->will($this->returnValue(true));
  136. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  137. ->setConstructorArgs(array($io))
  138. ->getMock();
  139. $remoteFilesystem->expects($this->at(0))
  140. ->method('getContents')
  141. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  142. ->will($this->returnValue('{"master_branch": "test_master"}'));
  143. $remoteFilesystem->expects($this->at(1))
  144. ->method('getContents')
  145. ->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))
  146. ->will($this->returnValue('{"encoding":"base64","content":"'.base64_encode('{"support": {"source": "'.$repoUrl.'" }}').'"}'));
  147. $remoteFilesystem->expects($this->at(2))
  148. ->method('getContents')
  149. ->with($this->equalTo('github.com'), $this->equalTo('https://api.github.com/repos/composer/packagist/commits/feature%2F3.2-foo'), $this->equalTo(false))
  150. ->will($this->returnValue('{"commit": {"committer":{ "date": "2012-09-10"}}}'));
  151. $repoConfig = array(
  152. 'url' => $repoUrl,
  153. );
  154. $repoUrl = 'https://github.com/composer/packagist.git';
  155. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  156. $gitHubDriver->initialize();
  157. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  158. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  159. $dist = $gitHubDriver->getDist($sha);
  160. $this->assertEquals('zip', $dist['type']);
  161. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  162. $this->assertEquals($sha, $dist['reference']);
  163. $source = $gitHubDriver->getSource($sha);
  164. $this->assertEquals('git', $source['type']);
  165. $this->assertEquals($repoUrl, $source['url']);
  166. $this->assertEquals($sha, $source['reference']);
  167. $gitHubDriver->getComposerInformation($identifier);
  168. }
  169. public function testPrivateRepositoryNoInteraction()
  170. {
  171. $repoUrl = 'http://github.com/composer/packagist';
  172. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  173. $repoSshUrl = 'git@github.com:composer/packagist.git';
  174. $identifier = 'v0.0.0';
  175. $sha = 'SOMESHA';
  176. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $io = $this->getMock('Composer\IO\IOInterface');
  180. $io->expects($this->any())
  181. ->method('isInteractive')
  182. ->will($this->returnValue(false));
  183. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  184. ->setConstructorArgs(array($io))
  185. ->getMock();
  186. $remoteFilesystem->expects($this->at(0))
  187. ->method('getContents')
  188. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  189. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  190. // clean local clone if present
  191. $fs = new Filesystem();
  192. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  193. $process->expects($this->at(0))
  194. ->method('execute')
  195. ->with($this->equalTo('git config github.accesstoken'))
  196. ->will($this->returnValue(1));
  197. $process->expects($this->at(1))
  198. ->method('execute')
  199. ->with($this->stringContains($repoSshUrl))
  200. ->will($this->returnValue(0));
  201. $process->expects($this->at(2))
  202. ->method('execute')
  203. ->with($this->stringContains('git show-ref --tags'));
  204. $process->expects($this->at(3))
  205. ->method('splitLines')
  206. ->will($this->returnValue(array($sha.' refs/tags/'.$identifier)));
  207. $process->expects($this->at(4))
  208. ->method('execute')
  209. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  210. $process->expects($this->at(5))
  211. ->method('splitLines')
  212. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  213. $process->expects($this->at(6))
  214. ->method('execute')
  215. ->with($this->stringContains('git branch --no-color'));
  216. $process->expects($this->at(7))
  217. ->method('splitLines')
  218. ->will($this->returnValue(array('* test_master')));
  219. $repoConfig = array(
  220. 'url' => $repoUrl,
  221. );
  222. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  223. $gitHubDriver->initialize();
  224. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  225. $dist = $gitHubDriver->getDist($sha);
  226. $this->assertEquals('zip', $dist['type']);
  227. $this->assertEquals('https://api.github.com/repos/composer/packagist/zipball/SOMESHA', $dist['url']);
  228. $this->assertEquals($sha, $dist['reference']);
  229. $source = $gitHubDriver->getSource($identifier);
  230. $this->assertEquals('git', $source['type']);
  231. $this->assertEquals($repoSshUrl, $source['url']);
  232. $this->assertEquals($identifier, $source['reference']);
  233. $source = $gitHubDriver->getSource($sha);
  234. $this->assertEquals('git', $source['type']);
  235. $this->assertEquals($repoSshUrl, $source['url']);
  236. $this->assertEquals($sha, $source['reference']);
  237. }
  238. protected function setAttribute($object, $attribute, $value)
  239. {
  240. $attr = new \ReflectionProperty($object, $attribute);
  241. $attr->setAccessible(true);
  242. $attr->setValue($object, $value);
  243. }
  244. }