ApiRequestServiceTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 ApiRequestService $apiRequestService;
  10. public function setUp(): void {
  11. $client = $this->getMockBuilder(HttpClientInterface::class)
  12. ->disableOriginalConstructor()
  13. ->getMock();
  14. $response = $this->getMockBuilder(ResponseInterface::class)
  15. ->disableOriginalConstructor()
  16. ->getMock();
  17. $response->method('getContent')->willReturn('{"a": 1}');
  18. $client
  19. ->expects($this->once())
  20. ->method('request')
  21. ->with("GET", "my_url.org")
  22. ->willReturn($response);
  23. $this->apiRequestService = new ApiRequestService($client);
  24. }
  25. public function testGetJsonContent() {
  26. $this->assertEquals(
  27. ['a' => 1],
  28. $this->apiRequestService->getJsonContent('my_url.org')
  29. );
  30. }
  31. public function testGetContent() {
  32. $this->assertEquals(
  33. '{"a": 1}',
  34. $this->apiRequestService->getContent('my_url.org')
  35. );
  36. }
  37. }