GitBitbucketDriverTest.php 9.0 KB

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