ApiRequestServiceTest.php 6.0 KB

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