GitBitbucketDriverTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\Repository\Vcs;
  12. use Composer\Config;
  13. use Composer\TestCase;
  14. use Composer\Util\Filesystem;
  15. /**
  16. * @group bitbucket
  17. */
  18. class GitBitbucketDriverTest extends TestCase
  19. {
  20. /** @type \Composer\IO\IOInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. private $io;
  22. /** @type \Composer\Config */
  23. private $config;
  24. /** @type \Composer\Util\RemoteFilesystem|\PHPUnit_Framework_MockObject_MockObject */
  25. private $rfs;
  26. /** @type string */
  27. private $home;
  28. /** @type string */
  29. private $originUrl = 'bitbucket.org';
  30. protected function setUp()
  31. {
  32. $this->io = $this->getMock('Composer\IO\IOInterface');
  33. $this->home = $this->getUniqueTmpDirectory();
  34. $this->config = new Config();
  35. $this->config->merge(array(
  36. 'config' => array(
  37. 'home' => $this->home,
  38. ),
  39. ));
  40. $this->rfs = $this->getMockBuilder('Composer\Util\RemoteFilesystem')
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. }
  44. public function tearDown()
  45. {
  46. $fs = new Filesystem;
  47. $fs->removeDirectory($this->home);
  48. }
  49. /**
  50. * @param array $repoConfig
  51. * @return GitBitbucketDriver
  52. */
  53. private function getDriver(array $repoConfig)
  54. {
  55. $driver = new GitBitbucketDriver(
  56. $repoConfig,
  57. $this->io,
  58. $this->config,
  59. null,
  60. $this->rfs
  61. );
  62. $driver->initialize();
  63. return $driver;
  64. }
  65. public function testGetRootIdentifier()
  66. {
  67. $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
  68. $this->rfs->expects($this->any())
  69. ->method('getContents')
  70. ->with(
  71. $this->originUrl,
  72. 'https://api.bitbucket.org/1.0/repositories/user/repo',
  73. false
  74. )
  75. ->willReturn(
  76. '{"scm": "git", "has_wiki": false, "last_updated": "2016-05-17T13:20:21.993", "no_forks": true, "forks_count": 0, "created_on": "2015-02-18T16:22:24.688", "owner": "user", "logo": "https://bitbucket.org/user/repo/avatar/32/?ts=1463484021", "email_mailinglist": "", "is_mq": false, "size": 9975494, "read_only": false, "fork_of": null, "mq_of": null, "followers_count": 0, "state": "available", "utc_created_on": "2015-02-18 15:22:24+00:00", "website": "", "description": "", "has_issues": false, "is_fork": false, "slug": "repo", "is_private": true, "name": "repo", "language": "php", "utc_last_updated": "2016-05-17 11:20:21+00:00", "no_public_forks": true, "creator": null, "resource_uri": "/1.0/repositories/user/repo"}'
  77. );
  78. $this->assertEquals(
  79. 'master',
  80. $driver->getRootIdentifier()
  81. );
  82. }
  83. public function testGetParams()
  84. {
  85. $url = 'https://bitbucket.org/user/repo.git';
  86. $driver = $this->getDriver(array('url' => $url));
  87. $this->assertEquals($url, $driver->getUrl());
  88. $this->assertEquals(
  89. array(
  90. 'type' => 'zip',
  91. 'url' => 'https://bitbucket.org/user/repo/get/reference.zip',
  92. 'reference' => 'reference',
  93. 'shasum' => ''
  94. ),
  95. $driver->getDist('reference')
  96. );
  97. $this->assertEquals(
  98. array('type' => 'git', 'url' => $url, 'reference' => 'reference'),
  99. $driver->getSource('reference')
  100. );
  101. }
  102. public function testGetComposerInformation()
  103. {
  104. $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
  105. $this->rfs->expects($this->any())
  106. ->method('getContents')
  107. ->withConsecutive(
  108. array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/src/master/composer.json', false),
  109. array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/changesets/master', false),
  110. array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/tags', false),
  111. array('bitbucket.org', 'https://api.bitbucket.org/1.0/repositories/user/repo/branches', false)
  112. )
  113. ->willReturnOnConsecutiveCalls(
  114. '{"node": "937992d19d72", "path": "composer.json", "data": "{\n \"name\": \"user/repo\",\n \"description\": \"test repo\",\n \"license\": \"GPL\",\n \"authors\": [\n {\n \"name\": \"Name\",\n \"email\": \"local@domain.tld\"\n }\n ],\n \"require\": {\n \"creator/package\": \"^1.0\"\n },\n \"require-dev\": {\n \"phpunit/phpunit\": \"~4.8\"\n }\n}\n", "size": 269}',
  115. '{"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}',
  116. '{}',
  117. '{"master": {"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}}'
  118. );
  119. $this->assertEquals(
  120. array(
  121. 'name' => 'user/repo',
  122. 'description' => 'test repo',
  123. 'license' => 'GPL',
  124. 'authors' => array(
  125. array(
  126. 'name' => 'Name',
  127. 'email' => 'local@domain.tld'
  128. )
  129. ),
  130. 'require' => array(
  131. 'creator/package' => '^1.0'
  132. ),
  133. 'require-dev' => array(
  134. 'phpunit/phpunit' => '~4.8'
  135. ),
  136. 'time' => '2016-05-17 13:19:52',
  137. 'support' => array(
  138. 'source' => 'https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=master'
  139. )
  140. ),
  141. $driver->getComposerInformation('master')
  142. );
  143. }
  144. public function testGetTags()
  145. {
  146. $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
  147. $this->rfs->expects($this->once())
  148. ->method('getContents')
  149. ->with(
  150. 'bitbucket.org',
  151. 'https://api.bitbucket.org/1.0/repositories/user/repo/tags',
  152. false
  153. )
  154. ->willReturn(
  155. '{"1.0.1": {"node": "9b78a3932143", "files": [{"type": "modified", "file": "path/to/file"}], "branches": [], "raw_author": "User <local@domain.tld>", "utctimestamp": "2015-04-16 14:50:40+00:00", "author": "user", "timestamp": "2015-04-16 16:50:40", "raw_node": "9b78a3932143497c519e49b8241083838c8ff8a1", "parents": ["84531c04dbfc", "50c2a4635ad0"], "branch": null, "message": "Commit message\n", "revision": null, "size": -1}, "1.0.0": {"node": "d3393d514318", "files": [{"type": "modified", "file": "path/to/file2"}], "branches": [], "raw_author": "User <local@domain.tld>", "utctimestamp": "2015-04-16 09:31:45+00:00", "author": "user", "timestamp": "2015-04-16 11:31:45", "raw_node": "d3393d514318a9267d2f8ebbf463a9aaa389f8eb", "parents": ["5a29a73cd1a0"], "branch": null, "message": "Commit message\n", "revision": null, "size": -1}}'
  156. );
  157. $this->assertEquals(
  158. array(
  159. '1.0.1' => '9b78a3932143497c519e49b8241083838c8ff8a1',
  160. '1.0.0' => 'd3393d514318a9267d2f8ebbf463a9aaa389f8eb'
  161. ),
  162. $driver->getTags()
  163. );
  164. }
  165. public function testGetBranches()
  166. {
  167. $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
  168. $this->rfs->expects($this->once())
  169. ->method('getContents')
  170. ->with(
  171. 'bitbucket.org',
  172. 'https://api.bitbucket.org/1.0/repositories/user/repo/branches',
  173. false
  174. )
  175. ->willReturn(
  176. '{"master": {"node": "937992d19d72", "files": [{"type": "modified", "file": "path/to/file"}], "raw_author": "User <local@domain.tld>", "utctimestamp": "2016-05-17 11:19:52+00:00", "author": "user", "timestamp": "2016-05-17 13:19:52", "raw_node": "937992d19d72b5116c3e8c4a04f960e5fa270b22", "parents": ["71e195a33361"], "branch": "master", "message": "Commit message\n", "revision": null, "size": -1}}'
  177. );
  178. $this->assertEquals(
  179. array(
  180. 'master' => '937992d19d72b5116c3e8c4a04f960e5fa270b22'
  181. ),
  182. $driver->getBranches()
  183. );
  184. }
  185. public function testSupports()
  186. {
  187. $this->assertTrue(
  188. GitBitbucketDriver::supports($this->io, $this->config, 'https://bitbucket.org/user/repo.git')
  189. );
  190. $this->assertFalse(
  191. GitBitbucketDriver::supports($this->io, $this->config, 'git@bitbucket.org:user/repo.git')
  192. );
  193. $this->assertFalse(
  194. GitBitbucketDriver::supports($this->io, $this->config, 'https://github.com/user/repo.git')
  195. );
  196. }
  197. }