GitHubDriverTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. $gitHubDriver = new GitHubDriver($repoUrl, $io, $this->config, $process, $remoteFilesystem);
  72. $gitHubDriver->initialize();
  73. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  74. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  75. $dist = $gitHubDriver->getDist($identifier);
  76. $this->assertEquals('zip', $dist['type']);
  77. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  78. $this->assertEquals('v0.0.0', $dist['reference']);
  79. $source = $gitHubDriver->getSource($identifier);
  80. $this->assertEquals('git', $source['type']);
  81. $this->assertEquals($repoSshUrl, $source['url']);
  82. $this->assertEquals('v0.0.0', $source['reference']);
  83. $dist = $gitHubDriver->getDist($sha);
  84. $this->assertEquals('zip', $dist['type']);
  85. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  86. $this->assertEquals('v0.0.0', $dist['reference']);
  87. $source = $gitHubDriver->getSource($sha);
  88. $this->assertEquals('git', $source['type']);
  89. $this->assertEquals($repoSshUrl, $source['url']);
  90. $this->assertEquals('v0.0.0', $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. $gitHubDriver = new GitHubDriver($repoUrl, $io, $this->config, null, $remoteFilesystem);
  110. $gitHubDriver->initialize();
  111. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  112. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  113. $dist = $gitHubDriver->getDist($identifier);
  114. $this->assertEquals('zip', $dist['type']);
  115. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  116. $this->assertEquals($identifier, $dist['reference']);
  117. $source = $gitHubDriver->getSource($identifier);
  118. $this->assertEquals('git', $source['type']);
  119. $this->assertEquals($repoUrl, $source['url']);
  120. $this->assertEquals($identifier, $source['reference']);
  121. $dist = $gitHubDriver->getDist($sha);
  122. $this->assertEquals('zip', $dist['type']);
  123. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  124. $this->assertEquals($identifier, $dist['reference']);
  125. $source = $gitHubDriver->getSource($sha);
  126. $this->assertEquals('git', $source['type']);
  127. $this->assertEquals($repoUrl, $source['url']);
  128. $this->assertEquals($identifier, $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://raw.github.com/composer/packagist/feature%2F3.2-foo/composer.json'), $this->equalTo(false))
  150. ->will($this->returnValue('{"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. $gitHubDriver = new GitHubDriver($repoUrl, $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($identifier);
  160. $this->assertEquals('zip', $dist['type']);
  161. $this->assertEquals('https://github.com/composer/packagist/zipball/feature/3.2-foo', $dist['url']);
  162. $this->assertEquals($identifier, $dist['reference']);
  163. $source = $gitHubDriver->getSource($identifier);
  164. $this->assertEquals('git', $source['type']);
  165. $this->assertEquals($repoUrl, $source['url']);
  166. $this->assertEquals($identifier, $source['reference']);
  167. $dist = $gitHubDriver->getDist($sha);
  168. $this->assertEquals('zip', $dist['type']);
  169. $this->assertEquals('https://github.com/composer/packagist/zipball/feature/3.2-foo', $dist['url']);
  170. $this->assertEquals($identifier, $dist['reference']);
  171. $source = $gitHubDriver->getSource($sha);
  172. $this->assertEquals('git', $source['type']);
  173. $this->assertEquals($repoUrl, $source['url']);
  174. $this->assertEquals($identifier, $source['reference']);
  175. $gitHubDriver->getComposerInformation($identifier);
  176. }
  177. public function testPrivateRepositoryNoInteraction()
  178. {
  179. $repoUrl = 'http://github.com/composer/packagist';
  180. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  181. $repoSshUrl = 'git@github.com:composer/packagist.git';
  182. $identifier = 'v0.0.0';
  183. $sha = 'SOMESHA';
  184. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  185. ->disableOriginalConstructor()
  186. ->getMock();
  187. $io = $this->getMock('Composer\IO\IOInterface');
  188. $io->expects($this->any())
  189. ->method('isInteractive')
  190. ->will($this->returnValue(false));
  191. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  192. ->setConstructorArgs(array($io))
  193. ->getMock();
  194. $remoteFilesystem->expects($this->at(0))
  195. ->method('getContents')
  196. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  197. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  198. // clean local clone if present
  199. $fs = new Filesystem();
  200. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  201. $process->expects($this->at(0))
  202. ->method('execute')
  203. ->with($this->stringContains($repoSshUrl))
  204. ->will($this->returnValue(0));
  205. $process->expects($this->at(1))
  206. ->method('execute')
  207. ->with($this->stringContains('git tag'));
  208. $process->expects($this->at(2))
  209. ->method('splitLines')
  210. ->will($this->returnValue(array($identifier)));
  211. $process->expects($this->at(3))
  212. ->method('execute')
  213. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  214. $process->expects($this->at(4))
  215. ->method('splitLines')
  216. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  217. $process->expects($this->at(5))
  218. ->method('execute')
  219. ->with($this->stringContains('git branch --no-color'));
  220. $process->expects($this->at(6))
  221. ->method('splitLines')
  222. ->will($this->returnValue(array('* test_master')));
  223. $gitHubDriver = new GitHubDriver($repoUrl, $io, $this->config, $process, $remoteFilesystem);
  224. $gitHubDriver->initialize();
  225. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  226. // Dist is not available for GitDriver
  227. $dist = $gitHubDriver->getDist($identifier);
  228. $this->assertNull($dist);
  229. $source = $gitHubDriver->getSource($identifier);
  230. $this->assertEquals('git', $source['type']);
  231. $this->assertEquals($repoSshUrl, $source['url']);
  232. $this->assertEquals($identifier, $source['reference']);
  233. // Dist is not available for GitDriver
  234. $dist = $gitHubDriver->getDist($sha);
  235. $this->assertNull($dist);
  236. $source = $gitHubDriver->getSource($sha);
  237. $this->assertEquals('git', $source['type']);
  238. $this->assertEquals($repoSshUrl, $source['url']);
  239. $this->assertEquals($sha, $source['reference']);
  240. }
  241. protected function setAttribute($object, $attribute, $value)
  242. {
  243. $attr = new \ReflectionProperty($object, $attribute);
  244. $attr->setAccessible(true);
  245. $attr->setValue($object, $value);
  246. }
  247. }