GitHubDriverTest.php 13 KB

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