GitBitbucketDriverTest.php 9.0 KB

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