GitLabDriverTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. use Prophecy\Argument;
  17. /**
  18. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  19. */
  20. class GitLabDriverTest extends TestCase
  21. {
  22. private $home;
  23. private $config;
  24. private $io;
  25. private $process;
  26. private $remoteFilesystem;
  27. public function setUp()
  28. {
  29. $this->home = $this->getUniqueTmpDirectory();
  30. $this->config = new Config();
  31. $this->config->merge(array(
  32. 'config' => array(
  33. 'home' => $this->home,
  34. 'gitlab-domains' => array(
  35. 'mycompany.com/gitlab',
  36. 'othercompany.com/nested/gitlab',
  37. 'gitlab.com',
  38. ),
  39. ),
  40. ));
  41. $this->io = $this->prophesize('Composer\IO\IOInterface');
  42. $this->process = $this->prophesize('Composer\Util\ProcessExecutor');
  43. $this->remoteFilesystem = $this->prophesize('Composer\Util\RemoteFilesystem');
  44. }
  45. public function tearDown()
  46. {
  47. $fs = new Filesystem();
  48. $fs->removeDirectory($this->home);
  49. }
  50. public function getInitializeUrls()
  51. {
  52. return array(
  53. array('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject'),
  54. array('http://gitlab.com/mygroup/myproject', 'http://gitlab.com/api/v3/projects/mygroup%2Fmyproject'),
  55. array('git@gitlab.com:mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject'),
  56. );
  57. }
  58. /**
  59. * @dataProvider getInitializeUrls
  60. */
  61. public function testInitialize($url, $apiUrl)
  62. {
  63. // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project
  64. $projectData = <<<JSON
  65. {
  66. "id": 17,
  67. "default_branch": "mymaster",
  68. "public": false,
  69. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  70. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  71. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  72. "name": "My Project",
  73. "name_with_namespace": "My Group / My Project",
  74. "path": "myproject",
  75. "path_with_namespace": "mygroup/myproject",
  76. "web_url": "https://gitlab.com/mygroup/myproject"
  77. }
  78. JSON;
  79. $this->remoteFilesystem
  80. ->getContents('gitlab.com', $apiUrl, false, array())
  81. ->willReturn($projectData)
  82. ->shouldBeCalledTimes(1)
  83. ;
  84. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  85. $driver->initialize();
  86. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  87. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  88. $this->assertEquals('git@gitlab.com:mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  89. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  90. return $driver;
  91. }
  92. /**
  93. * @dataProvider getInitializeUrls
  94. */
  95. public function testInitializePublicProject($url, $apiUrl)
  96. {
  97. // @link http://doc.gitlab.com/ce/api/projects.html#get-single-project
  98. $projectData = <<<JSON
  99. {
  100. "id": 17,
  101. "default_branch": "mymaster",
  102. "public": true,
  103. "http_url_to_repo": "https://gitlab.com/mygroup/myproject.git",
  104. "ssh_url_to_repo": "git@gitlab.com:mygroup/myproject.git",
  105. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  106. "name": "My Project",
  107. "name_with_namespace": "My Group / My Project",
  108. "path": "myproject",
  109. "path_with_namespace": "mygroup/myproject",
  110. "web_url": "https://gitlab.com/mygroup/myproject"
  111. }
  112. JSON;
  113. $this->remoteFilesystem
  114. ->getContents('gitlab.com', $apiUrl, false, array())
  115. ->willReturn($projectData)
  116. ->shouldBeCalledTimes(1)
  117. ;
  118. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  119. $driver->initialize();
  120. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  121. $this->assertEquals('mymaster', $driver->getRootIdentifier(), 'Root identifier is the default branch in GitLab');
  122. $this->assertEquals('https://gitlab.com/mygroup/myproject.git', $driver->getRepositoryUrl(), 'The repository URL is the SSH one by default');
  123. $this->assertEquals('https://gitlab.com/mygroup/myproject', $driver->getUrl());
  124. return $driver;
  125. }
  126. public function testGetDist()
  127. {
  128. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  129. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  130. $expected = array(
  131. 'type' => 'zip',
  132. 'url' => 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/archive.zip?sha='.$reference,
  133. 'reference' => $reference,
  134. 'shasum' => '',
  135. );
  136. $this->assertEquals($expected, $driver->getDist($reference));
  137. }
  138. public function testGetSource()
  139. {
  140. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  141. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  142. $expected = array(
  143. 'type' => 'git',
  144. 'url' => 'git@gitlab.com:mygroup/myproject.git',
  145. 'reference' => $reference,
  146. );
  147. $this->assertEquals($expected, $driver->getSource($reference));
  148. }
  149. public function testGetSource_GivenPublicProject()
  150. {
  151. $driver = $this->testInitializePublicProject('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject', true);
  152. $reference = 'c3ebdbf9cceddb82cd2089aaef8c7b992e536363';
  153. $expected = array(
  154. 'type' => 'git',
  155. 'url' => 'https://gitlab.com/mygroup/myproject.git',
  156. 'reference' => $reference,
  157. );
  158. $this->assertEquals($expected, $driver->getSource($reference));
  159. }
  160. public function testGetTags()
  161. {
  162. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  163. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/tags';
  164. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-tags
  165. $tagData = <<<JSON
  166. [
  167. {
  168. "name": "v1.0.0",
  169. "commit": {
  170. "id": "092ed2c762bbae331e3f51d4a17f67310bf99a81",
  171. "committed_date": "2012-05-28T04:42:42-07:00"
  172. }
  173. },
  174. {
  175. "name": "v2.0.0",
  176. "commit": {
  177. "id": "8e8f60b3ec86d63733db3bd6371117a758027ec6",
  178. "committed_date": "2014-07-06T12:59:11.000+02:00"
  179. }
  180. }
  181. ]
  182. JSON;
  183. $this->remoteFilesystem
  184. ->getContents('gitlab.com', $apiUrl, false, array())
  185. ->willReturn($tagData)
  186. ->shouldBeCalledTimes(1)
  187. ;
  188. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  189. $expected = array(
  190. 'v1.0.0' => '092ed2c762bbae331e3f51d4a17f67310bf99a81',
  191. 'v2.0.0' => '8e8f60b3ec86d63733db3bd6371117a758027ec6',
  192. );
  193. $this->assertEquals($expected, $driver->getTags());
  194. $this->assertEquals($expected, $driver->getTags(), 'Tags are cached');
  195. }
  196. public function testGetBranches()
  197. {
  198. $driver = $this->testInitialize('https://gitlab.com/mygroup/myproject', 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject');
  199. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmyproject/repository/branches';
  200. // @link http://doc.gitlab.com/ce/api/repositories.html#list-project-repository-branches
  201. $branchData = <<<JSON
  202. [
  203. {
  204. "name": "mymaster",
  205. "commit": {
  206. "id": "97eda36b5c1dd953a3792865c222d4e85e5f302e",
  207. "committed_date": "2013-01-03T21:04:07.000+01:00"
  208. }
  209. },
  210. {
  211. "name": "staging",
  212. "commit": {
  213. "id": "502cffe49f136443f2059803f2e7192d1ac066cd",
  214. "committed_date": "2013-03-09T16:35:23.000+01:00"
  215. }
  216. }
  217. ]
  218. JSON;
  219. $this->remoteFilesystem
  220. ->getContents('gitlab.com', $apiUrl, false, array())
  221. ->willReturn($branchData)
  222. ->shouldBeCalledTimes(1)
  223. ;
  224. $driver->setRemoteFilesystem($this->remoteFilesystem->reveal());
  225. $expected = array(
  226. 'mymaster' => '97eda36b5c1dd953a3792865c222d4e85e5f302e',
  227. 'staging' => '502cffe49f136443f2059803f2e7192d1ac066cd',
  228. );
  229. $this->assertEquals($expected, $driver->getBranches());
  230. $this->assertEquals($expected, $driver->getBranches(), 'Branches are cached');
  231. }
  232. /**
  233. * @dataProvider dataForTestSupports
  234. */
  235. public function testSupports($url, $expected)
  236. {
  237. $this->assertSame($expected, GitLabDriver::supports($this->io->reveal(), $this->config, $url));
  238. }
  239. public function dataForTestSupports()
  240. {
  241. return array(
  242. array('http://gitlab.com/foo/bar', true),
  243. array('http://gitlab.com/foo/bar/', true),
  244. array('http://gitlab.com/foo/bar.git', true),
  245. array('http://gitlab.com/foo/bar.baz.git', true),
  246. array('https://gitlab.com/foo/bar', extension_loaded('openssl')), // Platform requirement
  247. array('git@gitlab.com:foo/bar.git', extension_loaded('openssl')),
  248. array('git@example.com:foo/bar.git', false),
  249. array('http://example.com/foo/bar', false),
  250. array('http://mycompany.com/gitlab/mygroup/myproject', true),
  251. array('https://mycompany.com/gitlab/mygroup/myproject', extension_loaded('openssl')),
  252. array('http://othercompany.com/nested/gitlab/mygroup/myproject', true),
  253. array('https://othercompany.com/nested/gitlab/mygroup/myproject', extension_loaded('openssl')),
  254. array('http://gitlab.com/mygroup/mysubgroup/mysubsubgroup/myproject', true),
  255. array('https://gitlab.com/mygroup/mysubgroup/mysubsubgroup/myproject', extension_loaded('openssl')),
  256. );
  257. }
  258. public function testGitlabSubDirectory()
  259. {
  260. $url = 'https://mycompany.com/gitlab/mygroup/my-pro.ject';
  261. $apiUrl = 'https://mycompany.com/gitlab/api/v3/projects/mygroup%2Fmy-pro%2Eject';
  262. $projectData = <<<JSON
  263. {
  264. "id": 17,
  265. "default_branch": "mymaster",
  266. "public": false,
  267. "http_url_to_repo": "https://gitlab.com/gitlab/mygroup/my-pro.ject",
  268. "ssh_url_to_repo": "git@gitlab.com:mygroup/my-pro.ject.git",
  269. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  270. "name": "My Project",
  271. "name_with_namespace": "My Group / My Project",
  272. "path": "myproject",
  273. "path_with_namespace": "mygroup/my-pro.ject",
  274. "web_url": "https://gitlab.com/gitlab/mygroup/my-pro.ject"
  275. }
  276. JSON;
  277. $this->remoteFilesystem
  278. ->getContents('mycompany.com/gitlab', $apiUrl, false)
  279. ->willReturn($projectData)
  280. ->shouldBeCalledTimes(1)
  281. ;
  282. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  283. $driver->initialize();
  284. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  285. }
  286. public function testGitlabSubGroup()
  287. {
  288. $url = 'https://gitlab.com/mygroup/mysubgroup/myproject';
  289. $apiUrl = 'https://gitlab.com/api/v3/projects/mygroup%2Fmysubgroup%2Fmyproject';
  290. $projectData = <<<JSON
  291. {
  292. "id": 17,
  293. "default_branch": "mymaster",
  294. "public": false,
  295. "http_url_to_repo": "https://gitlab.com/mygroup/mysubgroup/my-pro.ject",
  296. "ssh_url_to_repo": "git@gitlab.com:mygroup/mysubgroup/my-pro.ject.git",
  297. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  298. "name": "My Project",
  299. "name_with_namespace": "My Group / My Project",
  300. "path": "myproject",
  301. "path_with_namespace": "mygroup/mysubgroup/my-pro.ject",
  302. "web_url": "https://gitlab.com/mygroup/mysubgroup/my-pro.ject"
  303. }
  304. JSON;
  305. $this->remoteFilesystem
  306. ->getContents('gitlab.com', $apiUrl, false)
  307. ->willReturn($projectData)
  308. ->shouldBeCalledTimes(1)
  309. ;
  310. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  311. $driver->initialize();
  312. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  313. }
  314. public function testGitlabSubDirectorySubGroup()
  315. {
  316. $url = 'https://mycompany.com/gitlab/mygroup/mysubgroup/myproject';
  317. $apiUrl = 'https://mycompany.com/gitlab/api/v3/projects/mygroup%2Fmysubgroup%2Fmyproject';
  318. $projectData = <<<JSON
  319. {
  320. "id": 17,
  321. "default_branch": "mymaster",
  322. "public": false,
  323. "http_url_to_repo": "https://mycompany.com/gitlab/mygroup/mysubgroup/my-pro.ject",
  324. "ssh_url_to_repo": "git@mycompany.com:mygroup/mysubgroup/my-pro.ject.git",
  325. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  326. "name": "My Project",
  327. "name_with_namespace": "My Group / My Project",
  328. "path": "myproject",
  329. "path_with_namespace": "mygroup/mysubgroup/my-pro.ject",
  330. "web_url": "https://mycompany.com/gitlab/mygroup/mysubgroup/my-pro.ject"
  331. }
  332. JSON;
  333. $this->remoteFilesystem
  334. ->getContents('mycompany.com/gitlab', $apiUrl, false, array())
  335. ->willReturn($projectData)
  336. ->shouldBeCalledTimes(1)
  337. ;
  338. $driver = new GitLabDriver(array('url' => $url), $this->io->reveal(), $this->config, $this->process->reveal(), $this->remoteFilesystem->reveal());
  339. $driver->initialize();
  340. $this->assertEquals($apiUrl, $driver->getApiUrl(), 'API URL is derived from the repository URL');
  341. }
  342. public function testForwardsOptions()
  343. {
  344. $options = array(
  345. 'ssl' => array(
  346. 'verify_peer' => false,
  347. )
  348. );
  349. $projectData = <<<JSON
  350. {
  351. "id": 17,
  352. "default_branch": "mymaster",
  353. "public": false,
  354. "http_url_to_repo": "https://gitlab.mycompany.local/mygroup/myproject",
  355. "ssh_url_to_repo": "git@gitlab.mycompany.local:mygroup/myproject.git",
  356. "last_activity_at": "2014-12-01T09:17:51.000+01:00",
  357. "name": "My Project",
  358. "name_with_namespace": "My Group / My Project",
  359. "path": "myproject",
  360. "path_with_namespace": "mygroup/myproject",
  361. "web_url": "https://gitlab.mycompany.local/mygroup/myproject"
  362. }
  363. JSON;
  364. $this->remoteFilesystem
  365. ->getContents(Argument::cetera(), $options)
  366. ->willReturn($projectData)
  367. ->shouldBeCalled();
  368. $driver = new GitLabDriver(
  369. array('url' => 'https://gitlab.mycompany.local/mygroup/myproject', 'options' => $options),
  370. $this->io->reveal(),
  371. $this->config,
  372. $this->process->reveal(),
  373. $this->remoteFilesystem->reveal()
  374. );
  375. $driver->initialize();
  376. }
  377. }