GitHubDriverTest.php 12 KB

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