ApiRequestServiceTest.php 9.0 KB

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