GitBitbucketDriverTest.php 9.6 KB

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