/test/Bitbucket/Tests/API/Http/ClientTest.php

https://bitbucket.org/gentlero/bitbucket-api · PHP · 236 lines · 180 code · 32 blank · 24 comment · 1 complexity · 31e6760f02b3a4fa9e97fa1de619df42 MD5 · raw file

  1. <?php
  2. namespace Bitbucket\Tests\API\Http;
  3. use Bitbucket\Tests\API as Tests;
  4. use Bitbucket\API\Http\Client;
  5. use Http\Client\Common\HttpMethodsClient;
  6. use Psr\Http\Message\ResponseInterface;
  7. /**
  8. * @author Alexandru G. <alex@gentle.ro>
  9. */
  10. class ClientTest extends Tests\TestCase
  11. {
  12. /** @var Client */
  13. private $client;
  14. public function setUp()
  15. {
  16. $this->client = new Client(array(), $this->getHttpPluginClientBuilder());
  17. }
  18. public function testGetSelfInstance()
  19. {
  20. $this->assertInstanceOf(HttpMethodsClient::class, $this->client->getClient());
  21. }
  22. /**
  23. * @expectedException \InvalidArgumentException
  24. */
  25. public function testSetResponseFormatInvalid()
  26. {
  27. $this->client->setResponseFormat('invalid');
  28. }
  29. public function testResponseFormatSuccess()
  30. {
  31. $this->client->setResponseFormat('xml');
  32. $this->assertEquals('xml', $this->client->getResponseFormat());
  33. }
  34. /**
  35. * @dataProvider invalidApiVersionsProvider
  36. * @expectedException \InvalidArgumentException
  37. * @ticket 57
  38. */
  39. public function testSetApiVersionInvalid($version)
  40. {
  41. $this->client->setApiVersion($version);
  42. }
  43. public function testApiVersionSuccess()
  44. {
  45. $this->client->setApiVersion('2.0');
  46. $this->assertEquals('2.0', $this->client->getApiVersion());
  47. $this->client->setApiVersion('1.0');
  48. $this->assertEquals('1.0', $this->client->getApiVersion());
  49. }
  50. /**
  51. * @dataProvider apiBaseUrlProvider
  52. */
  53. public function testGetApiBaseUrl($apiVersion, $expected)
  54. {
  55. $this->client->setApiVersion($apiVersion);
  56. $this->assertEquals($expected, $this->client->getApiBaseUrl());
  57. }
  58. public function apiBaseUrlProvider()
  59. {
  60. return [
  61. ['1.0', 'https://api.bitbucket.org/1.0'],
  62. ['2.0', 'https://api.bitbucket.org/2.0'],
  63. ];
  64. }
  65. public function testShouldDoGetRequestAndReturnResponseInstance()
  66. {
  67. $endpoint = '/repositories/gentle/eof/issues/3';
  68. $params = ['format' => 'json'];
  69. $headers = ['2' => '4'];
  70. $baseClient = $this->getHttpPluginClientBuilder();
  71. $client = new Client(
  72. [
  73. 'base_url' => 'https://example.com',
  74. 'api_version' => '1.0'
  75. ],
  76. $baseClient
  77. );
  78. $response = $client->get($endpoint, $params, $headers);
  79. $this->assertInstanceOf(ResponseInterface::class, $response);
  80. $this->assertInstanceOf(ResponseInterface::class, $client->getLastResponse());
  81. }
  82. public function testShouldDoPostRequestWithContentAndReturnResponseInstance()
  83. {
  84. $endpoint = '/repositories/gentle/eof/issues/3';
  85. $params = ['1' => '2'];
  86. $headers = ['3' => '4'];
  87. $baseClient = $this->getHttpPluginClientBuilder();
  88. $client = new Client(['user_agent' => 'tests'], $baseClient);
  89. $response = $client->post($endpoint, $params, $headers);
  90. $this->assertInstanceOf(ResponseInterface::class, $response);
  91. $this->assertEquals(http_build_query($params), $client->getLastRequest()->getBody()->getContents());
  92. $this->assertEquals([
  93. 'Content-Type' => ['application/x-www-form-urlencoded'],
  94. 'User-Agent' => ['tests'],
  95. '3' => ['4'],
  96. 'Host' => ['api.bitbucket.org'],
  97. ], $client->getLastRequest()->getHeaders());
  98. }
  99. /**
  100. * @ticket 74
  101. */
  102. public function testShouldDoPostRequestWithJsonContentAndReturnResponseInstance()
  103. {
  104. $endpoint = '/repositories/gentle/eof/pullrequests';
  105. $params = json_encode(['1' => '2', 'name' => 'john']);
  106. $headers = ['Content-Type' => 'application/json'];
  107. $baseClient = $this->getHttpPluginClientBuilder();
  108. $client = new Client(['user_agent' => 'tests'], $baseClient);
  109. $response = $client->post($endpoint, $params, $headers);
  110. $this->assertInstanceOf(ResponseInterface::class, $response);
  111. $this->assertEquals($params, $client->getLastRequest()->getBody()->getContents());
  112. $this->assertEquals([
  113. 'Content-Type' => ['application/json'],
  114. 'User-Agent' => ['tests'],
  115. 'Host' => ['api.bitbucket.org'],
  116. ], $client->getLastRequest()->getHeaders());
  117. }
  118. public function testShouldDoPutRequestAndReturnResponseInstance()
  119. {
  120. $endpoint = '/repositories/gentle/eof/issues/3';
  121. $params = ['1' => '2'];
  122. $headers = ['3' => '4'];
  123. $baseClient = $this->getHttpPluginClientBuilder();
  124. $client = new Client([], $baseClient);
  125. $response = $client->put($endpoint, $params, $headers);
  126. $this->assertInstanceOf(ResponseInterface::class, $response);
  127. }
  128. public function testShouldDoDeleteRequestAndReturnResponseInstance()
  129. {
  130. $endpoint = '/repositories/gentle/eof/issues/3';
  131. $params = ['1' => '2'];
  132. $headers = ['3' => '4'];
  133. $baseClient = $this->getHttpPluginClientBuilder();
  134. $client = new Client([], $baseClient);
  135. $response = $client->delete($endpoint, $params, $headers);
  136. $this->assertInstanceOf(ResponseInterface::class, $response);
  137. }
  138. public function testShouldDoPatchRequestAndReturnResponseInstance()
  139. {
  140. $endpoint = '/repositories/gentle/eof/issues/3';
  141. $params = ['1' => '2'];
  142. $headers = ['3' => '4'];
  143. $baseClient = $this->getHttpPluginClientBuilder();
  144. $client = new Client([], $baseClient);
  145. $response = $client->request($endpoint, $params, 'PATCH', $headers);
  146. $this->assertInstanceOf(ResponseInterface::class, $response);
  147. }
  148. public function testClientIsKeptWhenInvokingChildFactory()
  149. {
  150. $options = [
  151. 'base_url' => 'https://localhost'
  152. ];
  153. $client = new Client($options);
  154. $pullRequest = new \Bitbucket\API\Repositories\PullRequests();
  155. $pullRequest->setClient($client);
  156. $comments = $pullRequest->comments();
  157. $this->assertSame($client, $comments->getClient());
  158. }
  159. /**
  160. * @dataProvider currentApiVersionProvider
  161. */
  162. public function testCurrentApiVersion($currentApiVersion, $apiVersion, $expected)
  163. {
  164. $this->client->setApiVersion($currentApiVersion);
  165. $this->assertSame($expected, $this->client->isApiVersion($apiVersion));
  166. }
  167. public function currentApiVersionProvider()
  168. {
  169. return [
  170. ['1.0', '1.0', true],
  171. ['2.0', '2.0', true],
  172. ['1.0', '2.0', false],
  173. ['2.0', '1', false],
  174. ['2.0', '2', true],
  175. ];
  176. }
  177. /**
  178. * @ticket 64
  179. */
  180. public function testIncludeFormatParamOnlyInV1()
  181. {
  182. $endpoint = sprintf(
  183. '/repositories/gentlero/bitbucket-api/src/%s/%s',
  184. 'develop',
  185. 'lib/Bitbucket/API/Repositories'
  186. );
  187. $params = $headers = [];
  188. $baseClient = $this->getHttpPluginClientBuilder();
  189. $client = new Client(['api_version' => '2.0'], $baseClient);
  190. $client->get($endpoint, $params, $headers);
  191. $req = $client->getLastRequest()->getUri();
  192. $parts = parse_url($req);
  193. if (false === array_key_exists('query', $parts)) {
  194. $parts['query'] = '';
  195. }
  196. $this->assertFalse(strpos($parts['query'], 'format'));
  197. }
  198. public function invalidApiVersionsProvider()
  199. {
  200. return [
  201. ['3.1'], ['1,2'], ['1,0'], ['2.1'], ['4'], [2], ['string'], [2.0]
  202. ];
  203. }
  204. }