GitHubDriverTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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->once())
  59. ->method('setAuthorization')
  60. ->with($this->equalTo('github.com'), 'someuser', 'somepassword');
  61. $remoteFilesystem->expects($this->at(1))
  62. ->method('getContents')
  63. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  64. ->will($this->returnValue('{"master_branch": "test_master"}'));
  65. $repoConfig = array(
  66. 'url' => $repoUrl,
  67. );
  68. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  69. $gitHubDriver->initialize();
  70. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  71. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  72. $dist = $gitHubDriver->getDist($identifier);
  73. $this->assertEquals('zip', $dist['type']);
  74. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  75. $this->assertEquals('v0.0.0', $dist['reference']);
  76. $source = $gitHubDriver->getSource($identifier);
  77. $this->assertEquals('git', $source['type']);
  78. $this->assertEquals($repoSshUrl, $source['url']);
  79. $this->assertEquals('v0.0.0', $source['reference']);
  80. $dist = $gitHubDriver->getDist($sha);
  81. $this->assertEquals('zip', $dist['type']);
  82. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  83. $this->assertEquals('v0.0.0', $dist['reference']);
  84. $source = $gitHubDriver->getSource($sha);
  85. $this->assertEquals('git', $source['type']);
  86. $this->assertEquals($repoSshUrl, $source['url']);
  87. $this->assertEquals('v0.0.0', $source['reference']);
  88. }
  89. public function testPublicRepository()
  90. {
  91. $repoUrl = 'http://github.com/composer/packagist';
  92. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  93. $identifier = 'v0.0.0';
  94. $sha = 'SOMESHA';
  95. $io = $this->getMock('Composer\IO\IOInterface');
  96. $io->expects($this->any())
  97. ->method('isInteractive')
  98. ->will($this->returnValue(true));
  99. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  100. ->setConstructorArgs(array($io))
  101. ->getMock();
  102. $remoteFilesystem->expects($this->at(0))
  103. ->method('getContents')
  104. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  105. ->will($this->returnValue('{"master_branch": "test_master"}'));
  106. $repoConfig = array(
  107. 'url' => $repoUrl,
  108. );
  109. $gitHubDriver = new GitHubDriver($repoConfig, $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. $repoConfig = array(
  156. 'url' => $repoUrl,
  157. );
  158. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, null, $remoteFilesystem);
  159. $gitHubDriver->initialize();
  160. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  161. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  162. $dist = $gitHubDriver->getDist($identifier);
  163. $this->assertEquals('zip', $dist['type']);
  164. $this->assertEquals('https://github.com/composer/packagist/zipball/feature/3.2-foo', $dist['url']);
  165. $this->assertEquals($identifier, $dist['reference']);
  166. $source = $gitHubDriver->getSource($identifier);
  167. $this->assertEquals('git', $source['type']);
  168. $this->assertEquals($repoUrl, $source['url']);
  169. $this->assertEquals($identifier, $source['reference']);
  170. $dist = $gitHubDriver->getDist($sha);
  171. $this->assertEquals('zip', $dist['type']);
  172. $this->assertEquals('https://github.com/composer/packagist/zipball/feature/3.2-foo', $dist['url']);
  173. $this->assertEquals($identifier, $dist['reference']);
  174. $source = $gitHubDriver->getSource($sha);
  175. $this->assertEquals('git', $source['type']);
  176. $this->assertEquals($repoUrl, $source['url']);
  177. $this->assertEquals($identifier, $source['reference']);
  178. $gitHubDriver->getComposerInformation($identifier);
  179. }
  180. public function testPrivateRepositoryNoInteraction()
  181. {
  182. $repoUrl = 'http://github.com/composer/packagist';
  183. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  184. $repoSshUrl = 'git@github.com:composer/packagist.git';
  185. $identifier = 'v0.0.0';
  186. $sha = 'SOMESHA';
  187. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. $io = $this->getMock('Composer\IO\IOInterface');
  191. $io->expects($this->any())
  192. ->method('isInteractive')
  193. ->will($this->returnValue(false));
  194. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  195. ->setConstructorArgs(array($io))
  196. ->getMock();
  197. $remoteFilesystem->expects($this->at(0))
  198. ->method('getContents')
  199. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  200. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  201. // clean local clone if present
  202. $fs = new Filesystem();
  203. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  204. $process->expects($this->at(0))
  205. ->method('execute')
  206. ->with($this->stringContains($repoSshUrl))
  207. ->will($this->returnValue(0));
  208. $process->expects($this->at(1))
  209. ->method('execute')
  210. ->with($this->stringContains('git tag'));
  211. $process->expects($this->at(2))
  212. ->method('splitLines')
  213. ->will($this->returnValue(array($identifier)));
  214. $process->expects($this->at(3))
  215. ->method('execute')
  216. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  217. $process->expects($this->at(4))
  218. ->method('splitLines')
  219. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  220. $process->expects($this->at(5))
  221. ->method('execute')
  222. ->with($this->stringContains('git branch --no-color'));
  223. $process->expects($this->at(6))
  224. ->method('splitLines')
  225. ->will($this->returnValue(array('* test_master')));
  226. $repoConfig = array(
  227. 'url' => $repoUrl,
  228. );
  229. $gitHubDriver = new GitHubDriver($repoConfig, $io, $this->config, $process, $remoteFilesystem);
  230. $gitHubDriver->initialize();
  231. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  232. // Dist is not available for GitDriver
  233. $dist = $gitHubDriver->getDist($identifier);
  234. $this->assertNull($dist);
  235. $source = $gitHubDriver->getSource($identifier);
  236. $this->assertEquals('git', $source['type']);
  237. $this->assertEquals($repoSshUrl, $source['url']);
  238. $this->assertEquals($identifier, $source['reference']);
  239. // Dist is not available for GitDriver
  240. $dist = $gitHubDriver->getDist($sha);
  241. $this->assertNull($dist);
  242. $source = $gitHubDriver->getSource($sha);
  243. $this->assertEquals('git', $source['type']);
  244. $this->assertEquals($repoSshUrl, $source['url']);
  245. $this->assertEquals($sha, $source['reference']);
  246. }
  247. protected function setAttribute($object, $attribute, $value)
  248. {
  249. $attr = new \ReflectionProperty($object, $attribute);
  250. $attr->setAccessible(true);
  251. $attr->setValue($object, $value);
  252. }
  253. }