0

I am learning Unit test in Symfony. I have a class that makes a client request

class Client
{
    public function __construct(
        private readonly string $url,
        private readonly HttpClientInterface $httpClient
    ) {
    }

    /**
     * @throws TransportExceptionInterface
     */
    public function makeRequest(string $filename): array
    {
        try {
            $response = $this->httpClient->request(
                Request::METHOD_GET,
                $this->url . "$filename.json"
            );

            return $this->processResponse($response);
        } catch (TransportException $e) {
            throw new TransportException($e->getMessage());
        }
    }

I am trying to do the unit test for makeRequest method. So far I have tried this but I am not going anywhere. First I am not able to pass dependency. Second how to check if url is correct and returning valid output.

class ClientTest extends WebTestCase
{
    private $httpClient = null;

    protected function setUp(): void
    {
        self::bootKernel();
        $container = self::$kernel->getContainer();

        $this->httpClient = $container->get('http_client');
    }
    public function testIfRequestReturnsResult()
    {
        $controller = new Client();

    }
}
user1687891
  • 794
  • 2
  • 14
  • 30

0 Answers0