1
votes

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.

2

2 Answers

2
votes

The doc needs to be updated: the last file that overrides previous definitions is config/services_test.yaml That should help you solve your use case. See https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/src/Kernel.php#L42

1
votes

You should be able to override the service in the test by swapping it out in the container

$this->container->getDefinition('x.x_service')->setSynthetic(true);
$this->container->set('x.x_service', new NullFileFetcher());