GitLabDriverTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Repository\Vcs\GitLabDriver;
  13. use Composer\Config;
  14. /**
  15. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  16. */
  17. class GitLabDriverTest extends \PHPUnit_Framework_TestCase
  18. {
  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. 'gitlab-domains' => array('mycompany.com/gitlab', 'gitlab.com')
  26. ),
  27. ));
  28. $this->io = $this->prophesize('Composer\IO\IOInterface');
  29. $this->process = $this->prophesize('Composer\Util\ProcessExecutor');
  30. $this->remoteFilesystem = $this->prophesize('Composer\Util\RemoteFilesystem');
  31. }
  32. public function getInitializeUrls()
  33. {
  34. return array(
  35. array('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject'),
  36. array('http://gitlab.com/mygroup/myproject', 'http://gitlab.com/api/v3/projects/mygroup%2Fmyproject'),
  37. array('git@gitlab.com:mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject'),
  38. );
  39. }
  40. /**
  41. * @dataProvider getInitializeUrls
  42. */
  43. public function testInitialize($url, $apiUrl)
  44. {
  45. // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project
  46. $projectData = <<<JSON
  47. {
  48. "id": 17,
  49. "default_branch": "mymaster",
  50. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  51. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  52. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  53. "name": "My Project",
  54. "name_with_namespace": "My Group / My Project",
  55. "path": "myproject",
  56. "path_with_namespace": "mygroup/myproject",
  57. "web_url": "https://gitlab.com/mygroup/myproject"
  58. }
  59. JSON;
  60. $this->remoteFilesystem
  61. ->getContents('gitlab.com', $apiUrl, false)
  62. ->willReturn($projectData)
  63. ->shouldBeCalledTimes(1)
  64. ;
  65. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  66. $driver->initialize();
  67. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  68. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  69. $this->assertEquals('git@gitlab.com:mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  70. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  71. return $driver;
  72. }
  73. public function testGetDist()
  74. {
  75. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  76. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  77. $expected = array(
  78. 'type' => 'zip',
  79. 'url' => 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/archive.zip?sha='.$reference,
  80. 'reference' => $reference,
  81. 'shasum' => '',
  82. );
  83. $this->assertEquals($expected, $driver->getDist($reference));
  84. }
  85. public function testGetSource()
  86. {
  87. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  88. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  89. $expected = array(
  90. 'type' => 'git',
  91. 'url' => 'git@gitlab.com:mygroup/myproject.git',
  92. 'reference' => $reference,
  93. );
  94. $this->assertEquals($expected, $driver->getSource($reference));
  95. }
  96. public function testGetTags()
  97. {
  98. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  99. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/tags';
  100. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-tags
  101. $tagData = <<<JSON
  102. [
  103. {
  104. "name": "v1.0.0",
  105. "commit": {
  106. "id": "092ed2c762bbae331e3f51d4a17f67310bf99a81",
  107. "committed_date": "2012-05-28T04:42:42-07:00"
  108. }
  109. },
  110. {
  111. "name": "v2.0.0",
  112. "commit": {
  113. "id": "8e8f60b3ec86d63733db3bd6371117a758027ec6",
  114. "committed_date": "2014-07-06T12:59:11.000+02:00"
  115. }
  116. }
  117. ]
  118. JSON;
  119. $this->remoteFilesystem
  120. ->getContents('gitlab.com', $apiUrl, false)
  121. ->willReturn($tagData)
  122. ->shouldBeCalledTimes(1)
  123. ;
  124. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  125. $expected = array(
  126. 'v1.0.0' => '092ed2c762bbae331e3f51d4a17f67310bf99a81',
  127. 'v2.0.0' => '8e8f60b3ec86d63733db3bd6371117a758027ec6',
  128. );
  129. $this->assertEquals($expected, $driver->getTags());
  130. $this->assertEquals($expected, $driver->getTags(), 'Tags are cached');
  131. }
  132. public function testGetBranches()
  133. {
  134. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  135. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/branches';
  136. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  137. $branchData = <<<JSON
  138. [
  139. {
  140. "name": "mymaster",
  141. "commit": {
  142. "id": "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  143. "committed_date": "2013-01-03T21:04:07.000+01:00"
  144. }
  145. },
  146. {
  147. "name": "staging",
  148. "commit": {
  149. "id": "502cffe49f136443f2059803f2e7192d1ac066cd",
  150. "committed_date": "2013-03-09T16:35:23.000+01:00"
  151. }
  152. }
  153. ]
  154. JSON;
  155. $this->remoteFilesystem
  156. ->getContents('gitlab.com', $apiUrl, false)
  157. ->willReturn($branchData)
  158. ->shouldBeCalledTimes(1)
  159. ;
  160. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  161. $expected = array(
  162. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  163. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  164. );
  165. $this->assertEquals($expected, $driver->getBranches());
  166. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  167. }
  168. /**
  169. * @dataProvider dataForTestSupports
  170. */
  171. public function testSupports($url, $expected)
  172. {
  173. $this->assertSame($expected, GitLabDriver::supports($this->io->reveal(), $this->config, $url));
  174. }
  175. public function dataForTestSupports()
  176. {
  177. return array(
  178. array('http://gitlab.com/foo/bar', true),
  179. array('http://gitlab.com/foo/bar/', true),
  180. array('http://gitlab.com/foo/bar.git', true),
  181. array('http://gitlab.com/foo/bar.baz.git', true),
  182. array('https://gitlab.com/foo/bar', extension_loaded('openssl')), // Platform requirement
  183. array('git@gitlab.com:foo/bar.git', extension_loaded('openssl')),
  184. array('git@example.com:foo/bar.git', false),
  185. array('http://example.com/foo/bar', false),
  186. array('https://mycompany.com/gitlab/mygroup/myproject', true),
  187. );
  188. }
  189. public function testGitlabSubDirectory()
  190. {
  191. $url = 'https://mycompany.com/gitlab/mygroup/myproject';
  192. $apiUrl = 'https://mycompany.com/gitlab/api/v3/projects/mygroup%2Fmyproject';
  193. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  194. $driver->initialize();
  195. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  196. }
  197. }