GitHubDriverTest.php 12 KB

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