GitLabDriverTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "public": false,
  64. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  65. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  66. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  67. "name": "My Project",
  68. "name_with_namespace": "My Group / My Project",
  69. "path": "myproject",
  70. "path_with_namespace": "mygroup/myproject",
  71. "web_url": "https://gitlab.com/mygroup/myproject"
  72. }
  73. JSON;
  74. $this->remoteFilesystem
  75. ->getContents('gitlab.com', $apiUrl, false)
  76. ->willReturn($projectData)
  77. ->shouldBeCalledTimes(1)
  78. ;
  79. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  80. $driver->initialize();
  81. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  82. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  83. $this->assertEquals('git@gitlab.com:mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  84. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  85. return $driver;
  86. }
  87. /**
  88. * @dataProvider getInitializeUrls
  89. */
  90. public function testInitializePublicProject($url, $apiUrl)
  91. {
  92. // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project
  93. $projectData = <<<JSON
  94. {
  95. "id": 17,
  96. "default_branch": "mymaster",
  97. "public": true,
  98. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  99. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  100. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  101. "name": "My Project",
  102. "name_with_namespace": "My Group / My Project",
  103. "path": "myproject",
  104. "path_with_namespace": "mygroup/myproject",
  105. "web_url": "https://gitlab.com/mygroup/myproject"
  106. }
  107. JSON;
  108. $this->remoteFilesystem
  109. ->getContents('gitlab.com', $apiUrl, false)
  110. ->willReturn($projectData)
  111. ->shouldBeCalledTimes(1)
  112. ;
  113. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  114. $driver->initialize();
  115. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  116. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  117. $this->assertEquals('https://gitlab.com/mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  118. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  119. return $driver;
  120. }
  121. public function testGetDist()
  122. {
  123. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  124. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  125. $expected = array(
  126. 'type' => 'zip',
  127. 'url' => 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/archive.zip?sha='.$reference,
  128. 'reference' => $reference,
  129. 'shasum' => '',
  130. );
  131. $this->assertEquals($expected, $driver->getDist($reference));
  132. }
  133. public function testGetSource()
  134. {
  135. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  136. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  137. $expected = array(
  138. 'type' => 'git',
  139. 'url' => 'git@gitlab.com:mygroup/myproject.git',
  140. 'reference' => $reference,
  141. );
  142. $this->assertEquals($expected, $driver->getSource($reference));
  143. }
  144. public function testGetSource_GivenPublicProject()
  145. {
  146. $driver = $this->testInitializePublicProject('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject', true);
  147. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  148. $expected = array(
  149. 'type' => 'git',
  150. 'url' => 'https://gitlab.com/mygroup/myproject.git',
  151. 'reference' => $reference,
  152. );
  153. $this->assertEquals($expected, $driver->getSource($reference));
  154. }
  155. public function testGetTags()
  156. {
  157. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  158. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/tags';
  159. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-tags
  160. $tagData = <<<JSON
  161. [
  162. {
  163. "name": "v1.0.0",
  164. "commit": {
  165. "id": "092ed2c762bbae331e3f51d4a17f67310bf99a81",
  166. "committed_date": "2012-05-28T04:42:42-07:00"
  167. }
  168. },
  169. {
  170. "name": "v2.0.0",
  171. "commit": {
  172. "id": "8e8f60b3ec86d63733db3bd6371117a758027ec6",
  173. "committed_date": "2014-07-06T12:59:11.000+02:00"
  174. }
  175. }
  176. ]
  177. JSON;
  178. $this->remoteFilesystem
  179. ->getContents('gitlab.com', $apiUrl, false)
  180. ->willReturn($tagData)
  181. ->shouldBeCalledTimes(1)
  182. ;
  183. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  184. $expected = array(
  185. 'v1.0.0' => '092ed2c762bbae331e3f51d4a17f67310bf99a81',
  186. 'v2.0.0' => '8e8f60b3ec86d63733db3bd6371117a758027ec6',
  187. );
  188. $this->assertEquals($expected, $driver->getTags());
  189. $this->assertEquals($expected, $driver->getTags(), 'Tags are cached');
  190. }
  191. public function testGetBranches()
  192. {
  193. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  194. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/branches';
  195. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  196. $branchData = <<<JSON
  197. [
  198. {
  199. "name": "mymaster",
  200. "commit": {
  201. "id": "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  202. "committed_date": "2013-01-03T21:04:07.000+01:00"
  203. }
  204. },
  205. {
  206. "name": "staging",
  207. "commit": {
  208. "id": "502cffe49f136443f2059803f2e7192d1ac066cd",
  209. "committed_date": "2013-03-09T16:35:23.000+01:00"
  210. }
  211. }
  212. ]
  213. JSON;
  214. $this->remoteFilesystem
  215. ->getContents('gitlab.com', $apiUrl, false)
  216. ->willReturn($branchData)
  217. ->shouldBeCalledTimes(1)
  218. ;
  219. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  220. $expected = array(
  221. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  222. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  223. );
  224. $this->assertEquals($expected, $driver->getBranches());
  225. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  226. }
  227. /**
  228. * @dataProvider dataForTestSupports
  229. */
  230. public function testSupports($url, $expected)
  231. {
  232. $this->assertSame($expected, GitLabDriver::supports($this->io->reveal(), $this->config, $url));
  233. }
  234. public function dataForTestSupports()
  235. {
  236. return array(
  237. array('http://gitlab.com/foo/bar', true),
  238. array('http://gitlab.com/foo/bar/', true),
  239. array('http://gitlab.com/foo/bar.git', true),
  240. array('http://gitlab.com/foo/bar.baz.git', true),
  241. array('https://gitlab.com/foo/bar', extension_loaded('openssl')), // Platform requirement
  242. array('git@gitlab.com:foo/bar.git', extension_loaded('openssl')),
  243. array('git@example.com:foo/bar.git', false),
  244. array('http://example.com/foo/bar', false),
  245. array('http://mycompany.com/gitlab/mygroup/myproject', true),
  246. array('https://mycompany.com/gitlab/mygroup/myproject', extension_loaded('openssl')),
  247. );
  248. }
  249. public function testGitlabSubDirectory()
  250. {
  251. $url = 'https://mycompany.com/gitlab/mygroup/myproject';
  252. $apiUrl = 'https://mycompany.com/gitlab/api/v3/projects/mygroup%2Fmyproject';
  253. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  254. $driver->initialize();
  255. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  256. }
  257. }