ApiRequestServiceTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Tests\Service;
  3. use App\Service\Rest\ApiRequestService;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. use Symfony\Contracts\HttpClient\ResponseInterface;
  7. class ApiRequestServiceTest extends TestCase
  8. {
  9. private HttpClientInterface $client;
  10. private ApiRequestService $apiRequestService;
  11. public function setUp(): void {
  12. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  13. ->disableOriginalConstructor()
  14. ->getMock();
  15. $response = $this->getMockBuilder(ResponseInterface::class)
  16. ->disableOriginalConstructor()
  17. ->getMock();
  18. $response->method('getContent')->willReturn('{"a": 1}');
  19. $this->client
  20. ->expects($this->once())
  21. ->method('request')
  22. ->with("GET", "my_url.org")
  23. ->willReturn($response);
  24. $this->apiRequestService = new ApiRequestService($this->client);
  25. }
  26. public function testGetJsonContent() {
  27. $this->assertEquals(
  28. $this->apiRequestService->getJsonContent('my_url.org'),
  29. ['a' => 1]
  30. );
  31. }
  32. public function testGetContent() {
  33. $this->assertEquals(
  34. $this->apiRequestService->getContent('my_url.org'),
  35. '{"a": 1}'
  36. );
  37. }
  38. }