lundi 22 juillet 2019

Controlling dependencies in Symfony tests

I have a number of edge to edge tests for my Symfony 4.2 application. By this I mean tests that use the test client to make web requests and then do assertions on the result. Example:

public function testPageNotFound() {
    $client = $this->createClient();
    $client->request('GET', 'does-not-exist');
    $this->assertSame(404, $client->getResponse()->getStatusCode());
}

Is there a way to change specific services in the service container in such tests?

Example issue: I have services that makes web requests via an abstraction called FileFetcher. For my tests I want a NullFileFetcher to be used so no real web requests are made. How do I tell Symfony to use this test double?

Surprisingly there is no info on how to do this basic testing task in the main testing documentation https://symfony.com/doc/current/testing.html.

One approach I tried is configuration in config/packages/test/services.yaml. This did not work and as far as I can tell is caused by Symfony not allowing overriding of services.yaml, since it loads the main config/services.yaml last: https://symfony.com/doc/current/configuration.html#configuration-environments

In the end all I want to do is follow some very basic testing best practices:

  1. Initialize the whole environment at the start of each test method execution
  2. Possibly change specific services and configuration away from the default
  3. Execute code under test and make assertions on the result

Any examples of code doing that using Symfony would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire