ApiRequestServiceTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /**
  89. * @see TestableApiRequestService::post()
  90. */
  91. public function testPost(): void
  92. {
  93. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  94. ->setConstructorArgs([$this->client])
  95. ->setMethodsExcept(['post'])
  96. ->getMock();
  97. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  98. $apiRequestService
  99. ->expects(self::once())
  100. ->method('request')
  101. ->with('POST', 'path/to/data', [], ['option' => 2, 'json' => ['foo' => 1]])
  102. ->willReturn($response);
  103. $apiRequestService
  104. ->expects(self::once())
  105. ->method('addBodyOption')
  106. ->with(['option' => 2, 'json' => 3], ['foo' => 1])
  107. ->willReturn(['option' => 2, 'json' => ['foo' => 1]]);
  108. $actualResponse = $apiRequestService->post('path/to/data', ['foo' => 1], [], ['option' => 2, 'json' => 3]);
  109. $this->assertEquals($response, $actualResponse);
  110. }
  111. /**
  112. * @see TestableApiRequestService::put()
  113. */
  114. public function testPut(): void
  115. {
  116. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  117. ->setConstructorArgs([$this->client])
  118. ->setMethodsExcept(['put'])
  119. ->getMock();
  120. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  121. $apiRequestService->expects(self::once())
  122. ->method('request')
  123. ->with('PUT', 'path/to/data', [], ['option' => 2, 'body' => 'foo'])
  124. ->willReturn($response);
  125. $apiRequestService
  126. ->expects(self::once())
  127. ->method('addBodyOption')
  128. ->with([], 'foo')
  129. ->willReturn(['option' => 2, 'body' => 'foo']);
  130. $actualResponse = $apiRequestService->put('path/to/data', 'foo');
  131. $this->assertEquals($response, $actualResponse);
  132. }
  133. /**
  134. * @see TestableApiRequestService::delete()
  135. */
  136. public function testDelete(): void
  137. {
  138. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  139. ->setConstructorArgs([$this->client])
  140. ->setMethodsExcept(['delete'])
  141. ->getMock();
  142. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  143. $apiRequestService->expects(self::once())
  144. ->method('request')
  145. ->with('DELETE', 'path/to/data', [], [])
  146. ->willReturn($response);
  147. $actualResponse = $apiRequestService->delete('path/to/data');
  148. $this->assertEquals($response, $actualResponse);
  149. }
  150. /**
  151. * @see TestableApiRequestService::request()
  152. */
  153. public function testRequest(): void
  154. {
  155. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  156. ->setConstructorArgs([$this->client])
  157. ->setMethodsExcept(['request'])
  158. ->getMock();
  159. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  160. $this->client->expects(self::once())->method('request')->with('GET', 'path/to/data?param=1', [])->willReturn($response);
  161. $actualResponse = $apiRequestService->request('GET', 'path/to/data', ['param' => 1]);
  162. $this->assertEquals($response, $actualResponse);
  163. }
  164. /**
  165. * @see TestableApiRequestService::request()
  166. */
  167. public function testRequestWithError(): void
  168. {
  169. $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
  170. ->setConstructorArgs([$this->client])
  171. ->setMethodsExcept(['request'])
  172. ->getMock();
  173. $this->client->expects(self::once())->method('request')->willThrowException(new TransportException('error', 500));
  174. $this->expectException(HttpException::class);
  175. $apiRequestService->request('GET', 'path/to/data');
  176. }
  177. }