How to mock the Symfony HttpClient as a dependecy? How to mock the Symfony HttpClient as a dependecy? symfony symfony

How to mock the Symfony HttpClient as a dependecy?


You shouldn't depend on Symfony\Component\HttpClient, but on Symfony\Contracts\HttpClient\HttpClientInterface.

MockHttpClient implements that interface, so it's a valid substitution for injection in that case.

If you inject HttpClient, because you want to use the factory to create arbitrary clients at runtime, mocking is going to be harder. But it's unlikely that's what you really want.

The correct way to integrate the Http Client is by type-hinting for the interface, and letting the framework instantiate the appropriate client depending on your configuration.

That is cleaner (you are depending on an abstraction and not on a concretion), and so ii is much easier to write tests for.