ApiRequestServiceTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace App\Tests\Unit\Service\Rest;
  3. use App\Service\Rest\ApiRequestService;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Component\HttpClient\Exception\TransportException;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. use Symfony\Contracts\HttpClient\ResponseInterface;
  9. class TestableApiRequestService extends ApiRequestService
  10. {
  11. public function addBodyOption(array $options, array|string $body): array
  12. {
  13. return parent::addBodyOption($options, $body);
  14. }
  15. }
  16. class ApiRequestServiceTest extends TestCase
  17. {
  18. private HttpClientInterface $client;
  19. public function setUp(): void
  20. {
  21. $this->client = $this->getMockBuilder(HttpClientInterface::class)->disableOriginalConstructor()->getMock();
  22. }
  23. /**
  24. * @see TestableApiRequestService::getJsonContent()
  25. */
  26. public function testGetJsonContent(): void
  27. {
  28. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  29. ->setConstructorArgs([$this->client])
  30. ->setMethodsExcept(['getJsonContent'])
  31. ->getMock();
  32. $apiRequestService->expects(self::once())
  33. ->method('getContent')
  34. ->with('path/to/data', [], [])
  35. ->willReturn('{"foo": "bar"}');
  36. $data = $apiRequestService->getJsonContent('path/to/data');
  37. $this->assertEquals(['foo' => 'bar'], $data);
  38. }
  39. /**
  40. * @see TestableApiRequestService::getJsonContent()
  41. */
  42. public function testGetContent(): void
  43. {
  44. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  45. ->setConstructorArgs([$this->client])
  46. ->setMethodsExcept(['getContent'])
  47. ->getMock();
  48. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  49. $response->expects(self::once())->method('getContent')->willReturn('{foo: bar}');
  50. $apiRequestService->expects(self::once())
  51. ->method('get')
  52. ->with('path/to/data', [], [])
  53. ->willReturn($response);
  54. $content = $apiRequestService->getContent('path/to/data');
  55. $this->assertEquals('{foo: bar}', $content);
  56. }
  57. /**
  58. * @see TestableApiRequestService::getContent()
  59. */
  60. public function testGetContentWithError(): void
  61. {
  62. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  63. ->setConstructorArgs([$this->client])
  64. ->setMethodsExcept(['getContent'])
  65. ->getMock();
  66. $apiRequestService->expects(self::once())
  67. ->method('get')
  68. ->willThrowException(new TransportException());
  69. $this->expectException(HttpException::class);
  70. $apiRequestService->getContent('path/to/data');
  71. }
  72. /**
  73. * @see TestableApiRequestService::get()
  74. */
  75. public function testGet(): void
  76. {
  77. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  78. ->setConstructorArgs([$this->client])
  79. ->setMethodsExcept(['get'])
  80. ->getMock();
  81. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  82. $apiRequestService->expects(self::once())
  83. ->method('request')
  84. ->with('GET', 'path/to/data', [], [])
  85. ->willReturn($response);
  86. $actualResponse = $apiRequestService->get('path/to/data');
  87. $this->assertEquals($response, $actualResponse);
  88. }
  89. public function testAddBodyOptionBodyIsString()
  90. {
  91. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  92. ->setConstructorArgs([$this->client])
  93. ->setMethodsExcept(['addBodyOption'])
  94. ->getMock();
  95. $options = ['some_option' => 'some_value'];
  96. $body = 'foo';
  97. $result = $apiRequestService->addBodyOption($options, $body);
  98. $this->assertEquals(
  99. ['some_option' => 'some_value', 'body' => 'foo'],
  100. $result
  101. );
  102. }
  103. public function testAddBodyOptionBodyIsArray()
  104. {
  105. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  106. ->setConstructorArgs([$this->client])
  107. ->setMethodsExcept(['addBodyOption'])
  108. ->getMock();
  109. $options = ['some_option' => 'some_value'];
  110. $body = ['foo' => 'bar'];
  111. $result = $apiRequestService->addBodyOption($options, $body);
  112. $this->assertEquals(
  113. ['some_option' => 'some_value', 'json' => ['foo' => 'bar']],
  114. $result
  115. );
  116. }
  117. public function testAddBodyOptionKeyExists()
  118. {
  119. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  120. ->setConstructorArgs([$this->client])
  121. ->setMethodsExcept(['addBodyOption'])
  122. ->getMock();
  123. $options = ['some_option' => 'some_value', 'body' => 'exists'];
  124. $body = 'foo';
  125. $result = $apiRequestService->addBodyOption($options, $body);
  126. $this->assertEquals(
  127. ['some_option' => 'some_value', 'body' => 'foo'],
  128. $result
  129. );
  130. }
  131. /**
  132. * @see TestableApiRequestService::post()
  133. */
  134. public function testPost(): void
  135. {
  136. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  137. ->setConstructorArgs([$this->client])
  138. ->setMethodsExcept(['post'])
  139. ->getMock();
  140. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  141. $apiRequestService
  142. ->expects(self::once())
  143. ->method('request')
  144. ->with('POST', 'path/to/data', [], ['option' => 2, 'json' => ['foo' => 1]])
  145. ->willReturn($response);
  146. $apiRequestService
  147. ->expects(self::once())
  148. ->method('addBodyOption')
  149. ->with(['option' => 2, 'json' => 3], ['foo' => 1])
  150. ->willReturn(['option' => 2, 'json' => ['foo' => 1]]);
  151. $actualResponse = $apiRequestService->post('path/to/data', ['foo' => 1], [], ['option' => 2, 'json' => 3]);
  152. $this->assertEquals($response, $actualResponse);
  153. }
  154. /**
  155. * @see TestableApiRequestService::put()
  156. */
  157. public function testPut(): void
  158. {
  159. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  160. ->setConstructorArgs([$this->client])
  161. ->setMethodsExcept(['put'])
  162. ->getMock();
  163. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  164. $apiRequestService->expects(self::once())
  165. ->method('request')
  166. ->with('PUT', 'path/to/data', [], ['option' => 2, 'body' => 'foo'])
  167. ->willReturn($response);
  168. $apiRequestService
  169. ->expects(self::once())
  170. ->method('addBodyOption')
  171. ->with([], 'foo')
  172. ->willReturn(['option' => 2, 'body' => 'foo']);
  173. $actualResponse = $apiRequestService->put('path/to/data', 'foo');
  174. $this->assertEquals($response, $actualResponse);
  175. }
  176. /**
  177. * @see TestableApiRequestService::delete()
  178. */
  179. public function testDelete(): void
  180. {
  181. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  182. ->setConstructorArgs([$this->client])
  183. ->setMethodsExcept(['delete'])
  184. ->getMock();
  185. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  186. $apiRequestService->expects(self::once())
  187. ->method('request')
  188. ->with('DELETE', 'path/to/data', [], [])
  189. ->willReturn($response);
  190. $actualResponse = $apiRequestService->delete('path/to/data');
  191. $this->assertEquals($response, $actualResponse);
  192. }
  193. /**
  194. * @see TestableApiRequestService::request()
  195. */
  196. public function testRequest(): void
  197. {
  198. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  199. ->setConstructorArgs([$this->client])
  200. ->setMethodsExcept(['request'])
  201. ->getMock();
  202. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  203. $this->client->expects(self::once())->method('request')->with('GET', 'path/to/data?param=1', [])->willReturn($response);
  204. $actualResponse = $apiRequestService->request('GET', 'path/to/data', ['param' => 1]);
  205. $this->assertEquals($response, $actualResponse);
  206. }
  207. /**
  208. * @see TestableApiRequestService::request()
  209. */
  210. public function testRequestWithError(): void
  211. {
  212. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  213. ->setConstructorArgs([$this->client])
  214. ->setMethodsExcept(['request'])
  215. ->getMock();
  216. $this->client->expects(self::once())->method('request')->willThrowException(new TransportException('error', 500));
  217. $this->expectException(HttpException::class);
  218. $apiRequestService->request('GET', 'path/to/data');
  219. }
  220. }