ApiRequestServiceTest.php 6.0 KB

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