Skip to content

Mocking HttpClientInterface in a WebtestCase failes #62726

@mmarton

Description

@mmarton

Symfony version(s) affected

7.4

Description

Hi!

after upgrading to 7.4 mocking HttpClient in a WebtestCase no longer works

simple controller

<?php

declare(strict_types=1);

namespace App\Controller\Portal;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsController]
final class TestController
{
    #[Route('/test')]
    public function index(HttpClientInterface $client): Response
    {
        return new Response($client->request('GET', 'https://www.google.com')->getContent());
    }
}

simple test

<?php

declare(strict_types=1);

namespace Functional;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class TestControllerTest extends WebTestCase
{
    public function testResponseMocking(): void
    {
        $client = self::createClient();
        $cannedResponse = 'Request Mocked successfully!';
        self::getContainer()->set(HttpClientInterface::class, new MockHttpClient(new MockResponse($cannedResponse)));

        $client->request('GET', 'http://127.0.0.1/test');
        self::assertSame($cannedResponse, $client->getResponse()->getContent());
    }
}

I can't find anything in the changelog / upgrade file that points me to the right direction to how to handle this

How to reproduce

Try my examples in 7.3 -> works
Try my examples in 7.4 -> failes :/

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions