ApiRequestServiceTest.php 6.5 KB

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