GitBitbucketDriverTest.php 8.7 KB

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