ApiRequestServiceTest.php 5.7 KB

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