GitLabDriverTest.php 8.4 KB

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