ApiRequestServiceTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ApiRequestServiceTest extends TestCase
  10. {
  11. private HttpClientInterface $client;
  12. public function setUp(): void
  13. {
  14. $this->client = $this->getMockBuilder(HttpClientInterface::class)->disableOriginalConstructor()->getMock();
  15. }
  16. /**
  17. * @see ApiRequestService::getJsonContent()
  18. */
  19. public function testGetJsonContent(): void
  20. {
  21. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  22. ->setConstructorArgs([$this->client])
  23. ->setMethodsExcept(['getJsonContent'])
  24. ->getMock();
  25. $apiRequestService->expects(self::once())
  26. ->method('getContent')
  27. ->with('path/to/data', [], [])
  28. ->willReturn('{"foo": "bar"}');
  29. $data = $apiRequestService->getJsonContent('path/to/data');
  30. $this->assertEquals(['foo' => 'bar'], $data);
  31. }
  32. /**
  33. * @see ApiRequestService::getJsonContent()
  34. */
  35. public function testGetContent(): void
  36. {
  37. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  38. ->setConstructorArgs([$this->client])
  39. ->setMethodsExcept(['getContent'])
  40. ->getMock();
  41. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  42. $response->expects(self::once())->method('getContent')->willReturn('{foo: bar}');
  43. $apiRequestService->expects(self::once())
  44. ->method('get')
  45. ->with('path/to/data', [], [])
  46. ->willReturn($response);
  47. $content = $apiRequestService->getContent('path/to/data');
  48. $this->assertEquals('{foo: bar}', $content);
  49. }
  50. /**
  51. * @see ApiRequestService::getContent()
  52. */
  53. public function testGetContentWithError(): void
  54. {
  55. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  56. ->setConstructorArgs([$this->client])
  57. ->setMethodsExcept(['getContent'])
  58. ->getMock();
  59. $apiRequestService->expects(self::once())
  60. ->method('get')
  61. ->willThrowException(new TransportException());
  62. $this->expectException(HttpException::class);
  63. $apiRequestService->getContent('path/to/data');
  64. }
  65. /**
  66. * @see ApiRequestService::get()
  67. */
  68. public function testGet(): void
  69. {
  70. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  71. ->setConstructorArgs([$this->client])
  72. ->setMethodsExcept(['get'])
  73. ->getMock();
  74. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  75. $apiRequestService->expects(self::once())
  76. ->method('request')
  77. ->with('GET', 'path/to/data', [], [])
  78. ->willReturn($response);
  79. $actualResponse = $apiRequestService->get('path/to/data');
  80. $this->assertEquals($response, $actualResponse);
  81. }
  82. /**
  83. * @see ApiRequestService::post()
  84. */
  85. public function testPost(): void
  86. {
  87. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  88. ->setConstructorArgs([$this->client])
  89. ->setMethodsExcept(['post'])
  90. ->getMock();
  91. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  92. $apiRequestService->expects(self::once())
  93. ->method('request')
  94. ->with('POST', 'path/to/data', [], [])
  95. ->willReturn($response);
  96. $actualResponse = $apiRequestService->post('path/to/data');
  97. $this->assertEquals($response, $actualResponse);
  98. }
  99. /**
  100. * @see ApiRequestService::put()
  101. */
  102. public function testPut(): void
  103. {
  104. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  105. ->setConstructorArgs([$this->client])
  106. ->setMethodsExcept(['put'])
  107. ->getMock();
  108. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  109. $apiRequestService->expects(self::once())
  110. ->method('request')
  111. ->with('PUT', 'path/to/data', [], [])
  112. ->willReturn($response);
  113. $actualResponse = $apiRequestService->put('path/to/data');
  114. $this->assertEquals($response, $actualResponse);
  115. }
  116. /**
  117. * @see ApiRequestService::delete()
  118. */
  119. public function testDelete(): void
  120. {
  121. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  122. ->setConstructorArgs([$this->client])
  123. ->setMethodsExcept(['delete'])
  124. ->getMock();
  125. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  126. $apiRequestService->expects(self::once())
  127. ->method('request')
  128. ->with('DELETE', 'path/to/data', [], [])
  129. ->willReturn($response);
  130. $actualResponse = $apiRequestService->delete('path/to/data');
  131. $this->assertEquals($response, $actualResponse);
  132. }
  133. /**
  134. * @see ApiRequestService::request()
  135. */
  136. public function testRequest(): void
  137. {
  138. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  139. ->setConstructorArgs([$this->client])
  140. ->setMethodsExcept(['request'])
  141. ->getMock();
  142. $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
  143. $this->client->expects(self::once())->method('request')->with('GET', 'path/to/data?param=1', [])->willReturn($response);
  144. $actualResponse = $apiRequestService->request('GET', 'path/to/data', ['param' => 1]);
  145. $this->assertEquals($response, $actualResponse);
  146. }
  147. /**
  148. * @see ApiRequestService::request()
  149. */
  150. public function testRequestWithError(): void
  151. {
  152. $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
  153. ->setConstructorArgs([$this->client])
  154. ->setMethodsExcept(['request'])
  155. ->getMock();
  156. $this->client->expects(self::once())->method('request')->willThrowException(new TransportException('error', 500));
  157. $this->expectException(HttpException::class);
  158. $apiRequestService->request('GET', 'path/to/data');
  159. }
  160. }