GitHubDriverTest.php 12 KB

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