GitLabDriverTest.php 7.9 KB

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