I have a controller end-point that does an external API request under hood which I can't really make each time I run tests.
I'm using HttpClientInterface to make the request and now my idea is to replace it with MockHttpClient. So here is what I have so far:
class MyControllerTest extends WebTestCase
{
public function testSomething(): void
{
$client = static::createClient();
$response = new MockResponse(null, ['http_code' => 200]);
$client->getContainer()->set(HttpClientInterface::class, new MockHttpClient($response));
$client->request('GET', '/my-end-point');
self::assertResponseIsSuccessful();
}
}
But it gives me the following error:
The "Symfony\Contracts\HttpClient\HttpClientInterface" service is private, you cannot replace it
which is kinda makes sense. Is there a better solution or how to overcome the problem?