Symfony HttpClient and sequential requests assertions
Symfony/HttpClient is a powerful component to perform HTTP(s) requests. It already embeds a MockHttpClient and MockResponse to ease testing. When a method performs only one request, it’s very convenient to use, but when the class performs many request and you want to make assertions on the requests it becomes a bit more complex.
Let’s consider a class that performs HTTP requests:
class MyClass
{
public function __construct(
private HttpClientInterface $httpClient,
) {
}
public function sendRequests(): void
{
// In real life, these requests would have been created programmatically.
// Otherwise, it's totally useless to test them!
$this->httpClient->request('GET', 'https://httpbin.org/status/200', ['headers' => ['my-header' => 'foo']]);
$this->httpClient->request('PUT', 'https://httpbin.org/status/201', ['headers' => ['my-header' => 'bar']]);
}
}
Now, we want to test all requests.
If you look at the API you can see you can pass different shapes of data.
Here, we are going to pass a Generator of function. These functions are able to perform assertion on the request. Then they will return a Response.
final class MyClassTest extends TestCase
{
public function test()
{
$httpClient = new MockHttpClient((function (): Generator {
$expectedRequests = [
['GET', 'https://httpbin.org/status/200', 'my-header: foo'],
['PUT', 'https://httpbin.org/status/201', 'my-header: bar'],
];
foreach ($expectedRequests as [$expectedMethod, $expectedUrl, $expectedHeader]) {
yield function (string $method, string $url, array $options) use ($expectedMethod, $expectedUrl, $expectedHeader): ResponseInterface {
$this->assertSame($expectedMethod, $method);
$this->assertSame($expectedUrl, $url);
$this->assertSame($expectedHeader, $options['normalized_headers']['my-header'][0]);
return new MockResponse();
};
}
})());
$myClass = new MyClass($httpClient);
// In real life, you usually pass some parameters to construct all requests.
$myClass->sendRequests();
}
}
The first step is to create a MockHttpClient
. We give it a generator (did you notice the callback is executed right away?).
The generator is responsible for generating a callback that must return a response. One callback per request.
The callback allows us to make some assertion on the method
, the url
, and the options
.
The second and last step is to instantiate MyClass
with the mock HTTP client and execute the method we want to test.
That’s all! ✨
We hope you enjoy this post and see you soon for a new copy/paste snippet!
Commentaires et discussions
Nos formations sur ce sujet
Notre expertise est aussi disponible sous forme de formations professionnelles !
Symfony avancée
Découvrez les fonctionnalités et concepts avancés de Symfony
Ces clients ont profité de notre expertise
LOOK Cycle bénéficie désormais d’une nouvelle plateforme eCommerce disponible sur 70 pays et 5 langues. La base technique modulaire de Sylius permet de répondre aux exigences de LOOK Cycle en terme de catalogue, produits, tunnel d’achat, commandes, expéditions, gestion des clients et des expéditions.
Nous avons réalisé la refonte du site de l’agence Beautiful Monday en utilisant nos compétences HTML5/CSS3 côté front-end, et le framework Symfony2 côté back-end. Afin de s’afficher correctement sur n’importe quel appareil, le site est entièrement responsive. La partie intégration a été effectuée avec un grand soin, en respectant parfaitement la maquette…
L’équipe de Finarta a fait appel à JoliCode pour le développement de leur plateforme Web. Basée sur le framework Symfony 2, l’application est un réseau privé de galerie et se veut être une place de communication et de vente d’oeuvres d’art entre ses membres. Pour cela, de nombreuses règles de droits ont été mises en places et une administration poussée…