GitBitbucketDriverTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\Test\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->getMockBuilder('Composer\IO\IOInterface')->getMock();
  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 testGetRootIdentifierWrongScmType()
  67. {
  68. $this->setExpectedException(
  69. '\RuntimeException',
  70. 'https://bitbucket.org/user/repo.git does not appear to be a git repository, use https://bitbucket.org/user/repo if this is a mercurial bitbucket repository'
  71. );
  72. $this->rfs->expects($this->once())
  73. ->method('getContents')
  74. ->with(
  75. $this->originUrl,
  76. 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner',
  77. false
  78. )
  79. ->willReturn(
  80. '{"scm":"hg","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo","name":"https"},{"href":"ssh:\/\/hg@bitbucket.org\/user\/repo","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}'
  81. );
  82. $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
  83. $driver->getRootIdentifier();
  84. }
  85. public function testDriver()
  86. {
  87. $driver = $this->getDriver(array('url' => 'https://bitbucket.org/user/repo.git'));
  88. $this->rfs->expects($this->any())
  89. ->method('getContents')
  90. ->withConsecutive(
  91. array(
  92. $this->originUrl,
  93. 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=-project%2C-owner',
  94. false,
  95. ),
  96. array(
  97. $this->originUrl,
  98. 'https://api.bitbucket.org/2.0/repositories/user/repo?fields=mainbranch',
  99. false,
  100. ),
  101. array(
  102. $this->originUrl,
  103. 'https://api.bitbucket.org/2.0/repositories/user/repo/refs/tags?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cnext&sort=-target.date',
  104. false,
  105. ),
  106. array(
  107. $this->originUrl,
  108. 'https://api.bitbucket.org/2.0/repositories/user/repo/refs/branches?pagelen=100&fields=values.name%2Cvalues.target.hash%2Cvalues.heads%2Cnext&sort=-target.date',
  109. false,
  110. ),
  111. array(
  112. $this->originUrl,
  113. 'https://api.bitbucket.org/2.0/repositories/user/repo/src/master/composer.json',
  114. false,
  115. ),
  116. array(
  117. $this->originUrl,
  118. 'https://api.bitbucket.org/2.0/repositories/user/repo/commit/master?fields=date',
  119. false,
  120. )
  121. )
  122. ->willReturnOnConsecutiveCalls(
  123. '{"scm":"git","website":"","has_wiki":false,"name":"repo","links":{"branches":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/branches"},"tags":{"href":"https:\/\/api.bitbucket.org\/2.0\/repositories\/user\/repo\/refs\/tags"},"clone":[{"href":"https:\/\/user@bitbucket.org\/user\/repo.git","name":"https"},{"href":"ssh:\/\/git@bitbucket.org\/user\/repo.git","name":"ssh"}],"html":{"href":"https:\/\/bitbucket.org\/user\/repo"}},"language":"php","created_on":"2015-02-18T16:22:24.688+00:00","updated_on":"2016-05-17T13:20:21.993+00:00","is_private":true,"has_issues":false}',
  124. '{"mainbranch": {"name": "master"}}',
  125. '{"values":[{"name":"1.0.1","target":{"hash":"9b78a3932143497c519e49b8241083838c8ff8a1"}},{"name":"1.0.0","target":{"hash":"d3393d514318a9267d2f8ebbf463a9aaa389f8eb"}}]}',
  126. '{"values":[{"name":"master","target":{"hash":"937992d19d72b5116c3e8c4a04f960e5fa270b22"}}]}',
  127. '{"name": "user/repo","description": "test repo","license": "GPL","authors": [{"name": "Name","email": "local@domain.tld"}],"require": {"creator/package": "^1.0"},"require-dev": {"phpunit/phpunit": "~4.8"}}',
  128. '{"date": "2016-05-17T13:19:52+00:00"}'
  129. );
  130. $this->assertEquals(
  131. 'master',
  132. $driver->getRootIdentifier()
  133. );
  134. $this->assertEquals(
  135. array(
  136. '1.0.1' => '9b78a3932143497c519e49b8241083838c8ff8a1',
  137. '1.0.0' => 'd3393d514318a9267d2f8ebbf463a9aaa389f8eb',
  138. ),
  139. $driver->getTags()
  140. );
  141. $this->assertEquals(
  142. array(
  143. 'master' => '937992d19d72b5116c3e8c4a04f960e5fa270b22',
  144. ),
  145. $driver->getBranches()
  146. );
  147. $this->assertEquals(
  148. array(
  149. 'name' => 'user/repo',
  150. 'description' => 'test repo',
  151. 'license' => 'GPL',
  152. 'authors' => array(
  153. array(
  154. 'name' => 'Name',
  155. 'email' => 'local@domain.tld',
  156. ),
  157. ),
  158. 'require' => array(
  159. 'creator/package' => '^1.0',
  160. ),
  161. 'require-dev' => array(
  162. 'phpunit/phpunit' => '~4.8',
  163. ),
  164. 'time' => '2016-05-17T13:19:52+00:00',
  165. 'support' => array(
  166. 'source' => 'https://bitbucket.org/user/repo/src/937992d19d72b5116c3e8c4a04f960e5fa270b22/?at=master',
  167. ),
  168. 'homepage' => 'https://bitbucket.org/user/repo',
  169. ),
  170. $driver->getComposerInformation('master')
  171. );
  172. return $driver;
  173. }
  174. /**
  175. * @depends testDriver
  176. * @param \Composer\Repository\Vcs\VcsDriverInterface $driver
  177. */
  178. public function testGetParams($driver)
  179. {
  180. $url = 'https://bitbucket.org/user/repo.git';
  181. $this->assertEquals($url, $driver->getUrl());
  182. $this->assertEquals(
  183. array(
  184. 'type' => 'zip',
  185. 'url' => 'https://bitbucket.org/user/repo/get/reference.zip',
  186. 'reference' => 'reference',
  187. 'shasum' => '',
  188. ),
  189. $driver->getDist('reference')
  190. );
  191. $this->assertEquals(
  192. array('type' => 'git', 'url' => $url, 'reference' => 'reference'),
  193. $driver->getSource('reference')
  194. );
  195. }
  196. public function testSupports()
  197. {
  198. $this->assertTrue(
  199. GitBitbucketDriver::supports($this->io, $this->config, 'https://bitbucket.org/user/repo.git')
  200. );
  201. $this->assertFalse(
  202. GitBitbucketDriver::supports($this->io, $this->config, 'git@bitbucket.org:user/repo.git')
  203. );
  204. $this->assertFalse(
  205. GitBitbucketDriver::supports($this->io, $this->config, 'https://github.com/user/repo.git')
  206. );
  207. }
  208. }