GitLabDriverTest.php 6.7 KB

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