Symfony News

New in Symfony 4.1: Simpler service testing

Nicolas Grekas

Contributed by
Nicolas Grekas
in #26499.

In Symfony 3.4 we made all services private by default meaning that you cannot longer call $this->get('my_service_id') in your controllers to quickly get some service.

We made this change because using the service container directly is not considered a good practice: it hides the dependencies of your classes, making them coupled to external configuration, thus harder to test and to review.

Whenever we remove a feature like that, we provide an alternative that is considered better and, if possible, as simple to use as the previous one. That's why controllers allow injecting services with type hints in their action methods and their constructors.

The only remaining drawback of "private services by default" is that testing was harder than before. Some developers even defined some config in the test environment to make all services public in tests. In Symfony 4.1, we did the same and now tests allow fetching private services by default.

In practice, tests based on WebTestCase and KernelTestCase now access to a special container via $client->getContainer() or the static::$container property that allows to fetch non-removed private services:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class AddUserCommandTest extends KernelTestCase
{
    private function assertUserCreated()
    {
        self::bootKernel();

        // returns the real and unchanged service container
        $container = self::$kernel->getContainer();

        // returns the special container that allows fetching private services
        $client = static::createClient();
        $container = $client->getContainer();
        // alternative way to get the special container:
        // $container = self::$container;

        $user = self::$container->get('doctrine')->getRepository(User::class)->findOneByEmail('...');
        $this->assertTrue(self::$container->get('security.password_encoder')->isPasswordValid($user, '...');
        // ...
}

Be trained by Symfony experts - 2018-03-21 Clichy - 2018-04-3 Paris - 2018-04-3 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook