GitHubDriverTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. $remoteFilesystem->expects($this->at(0))
  43. ->method('getContents')
  44. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  45. ->will($this->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  46. $io->expects($this->once())
  47. ->method('ask')
  48. ->with($this->equalTo('Username: '))
  49. ->will($this->returnValue('someuser'));
  50. $io->expects($this->once())
  51. ->method('askAndHideAnswer')
  52. ->with($this->equalTo('Password: '))
  53. ->will($this->returnValue('somepassword'));
  54. $io->expects($this->once())
  55. ->method('setAuthorization')
  56. ->with($this->equalTo('github.com'), 'someuser', 'somepassword');
  57. $remoteFilesystem->expects($this->at(1))
  58. ->method('getContents')
  59. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  60. ->will($this->returnValue('{"master_branch": "test_master"}'));
  61. $gitHubDriver = new GitHubDriver($repoUrl, $io, $this->config, null, $remoteFilesystem);
  62. $gitHubDriver->initialize();
  63. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  64. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  65. $dist = $gitHubDriver->getDist($identifier);
  66. $this->assertEquals('zip', $dist['type']);
  67. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  68. $this->assertEquals('v0.0.0', $dist['reference']);
  69. $source = $gitHubDriver->getSource($identifier);
  70. $this->assertEquals('git', $source['type']);
  71. $this->assertEquals($repoSshUrl, $source['url']);
  72. $this->assertEquals('v0.0.0', $source['reference']);
  73. $dist = $gitHubDriver->getDist($sha);
  74. $this->assertEquals('zip', $dist['type']);
  75. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  76. $this->assertEquals('v0.0.0', $dist['reference']);
  77. $source = $gitHubDriver->getSource($sha);
  78. $this->assertEquals('git', $source['type']);
  79. $this->assertEquals($repoSshUrl, $source['url']);
  80. $this->assertEquals('v0.0.0', $source['reference']);
  81. }
  82. public function testPublicRepository()
  83. {
  84. $repoUrl = 'http://github.com/composer/packagist';
  85. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  86. $identifier = 'v0.0.0';
  87. $sha = 'SOMESHA';
  88. $io = $this->getMock('Composer\IO\IOInterface');
  89. $io->expects($this->any())
  90. ->method('isInteractive')
  91. ->will($this->returnValue(true));
  92. $remoteFilesystem = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  93. ->setConstructorArgs(array($io))
  94. ->getMock();
  95. $remoteFilesystem->expects($this->at(0))
  96. ->method('getContents')
  97. ->with($this->equalTo('github.com'), $this->equalTo($repoApiUrl), $this->equalTo(false))
  98. ->will($this->returnValue('{"master_branch": "test_master"}'));
  99. $gitHubDriver = new GitHubDriver($repoUrl, $io, $this->config, null, $remoteFilesystem);
  100. $gitHubDriver->initialize();
  101. $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha));
  102. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  103. $dist = $gitHubDriver->getDist($identifier);
  104. $this->assertEquals('zip', $dist['type']);
  105. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  106. $this->assertEquals($identifier, $dist['reference']);
  107. $source = $gitHubDriver->getSource($identifier);
  108. $this->assertEquals('git', $source['type']);
  109. $this->assertEquals($repoUrl, $source['url']);
  110. $this->assertEquals($identifier, $source['reference']);
  111. $dist = $gitHubDriver->getDist($sha);
  112. $this->assertEquals('zip', $dist['type']);
  113. $this->assertEquals('https://github.com/composer/packagist/zipball/v0.0.0', $dist['url']);
  114. $this->assertEquals($identifier, $dist['reference']);
  115. $source = $gitHubDriver->getSource($sha);
  116. $this->assertEquals('git', $source['type']);
  117. $this->assertEquals($repoUrl, $source['url']);
  118. $this->assertEquals($identifier, $source['reference']);
  119. }
  120. public function testPrivateRepositoryNoInteraction()
  121. {
  122. $repoUrl = 'http://github.com/composer/packagist';
  123. $repoApiUrl = 'https://api.github.com/repos/composer/packagist';
  124. $repoSshUrl = 'git@github.com:composer/packagist.git';
  125. $identifier = 'v0.0.0';
  126. $sha = 'SOMESHA';
  127. $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $io = $this->getMock('Composer\IO\IOInterface');
  131. $io->expects($this->any())
  132. ->method('isInteractive')
  133. ->will($this->returnValue(false));
  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->throwException(new TransportException('HTTP/1.1 404 Not Found', 404)));
  141. // clean local clone if present
  142. $fs = new Filesystem();
  143. $fs->removeDirectory(sys_get_temp_dir() . '/composer-test');
  144. $process->expects($this->at(0))
  145. ->method('execute')
  146. ->with($this->stringContains($repoSshUrl))
  147. ->will($this->returnValue(0));
  148. $process->expects($this->at(1))
  149. ->method('execute')
  150. ->with($this->stringContains('git tag'));
  151. $process->expects($this->at(2))
  152. ->method('splitLines')
  153. ->will($this->returnValue(array($identifier)));
  154. $process->expects($this->at(3))
  155. ->method('execute')
  156. ->with($this->stringContains('git branch --no-color --no-abbrev -v'));
  157. $process->expects($this->at(4))
  158. ->method('splitLines')
  159. ->will($this->returnValue(array(' test_master edf93f1fccaebd8764383dc12016d0a1a9672d89 Fix test & behavior')));
  160. $process->expects($this->at(5))
  161. ->method('execute')
  162. ->with($this->stringContains('git branch --no-color'));
  163. $process->expects($this->at(6))
  164. ->method('splitLines')
  165. ->will($this->returnValue(array('* test_master')));
  166. $gitHubDriver = new GitHubDriver($repoUrl, $io, $this->config, $process, $remoteFilesystem);
  167. $gitHubDriver->initialize();
  168. $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier());
  169. // Dist is not available for GitDriver
  170. $dist = $gitHubDriver->getDist($identifier);
  171. $this->assertNull($dist);
  172. $source = $gitHubDriver->getSource($identifier);
  173. $this->assertEquals('git', $source['type']);
  174. $this->assertEquals($repoSshUrl, $source['url']);
  175. $this->assertEquals($identifier, $source['reference']);
  176. // Dist is not available for GitDriver
  177. $dist = $gitHubDriver->getDist($sha);
  178. $this->assertNull($dist);
  179. $source = $gitHubDriver->getSource($sha);
  180. $this->assertEquals('git', $source['type']);
  181. $this->assertEquals($repoSshUrl, $source['url']);
  182. $this->assertEquals($sha, $source['reference']);
  183. }
  184. protected function setAttribute($object, $attribute, $value)
  185. {
  186. $attr = new \ReflectionProperty($object, $attribute);
  187. $attr->setAccessible(true);
  188. $attr->setValue($object, $value);
  189. }
  190. }