Home > other >  Make private and removed service available in tests
Make private and removed service available in tests

Time:01-26

I'm trying to make vatin-bundle compatible with Symfony 6.

But the tests fail

The "validator" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

It seems this is new in Symfony 6

The container in static::getContainer() is actually a special test container. It gives you access to both the public services and the non-removed private services services.

What is the preferred way to make validator available in tests again?

The only way I found is creating my own alias like

services:
    myvalidator:
        alias: validator
        public: true

and use the new alias. Is there a better way?

CodePudding user response:

If the service is removed, then it's no longer accessible no matter what. It's not a matter of visibility, the service is no longer there. So you need to prevent the service from being removed: creating an alias is the best and simplest way to go about it.

This has been confirmed by maintainers here.

You can create the alias only during testing, and still access the original service. (e.g. ->get('validator'). Once the alias is created, the original service is no longer removed.

I don't think this is something really new in Symfony 6, but it's bee a thing since Symfony 4.4. Although it's true that now on Symfony 6, since it's removes previously deprecated behaviour, things could have changed.

  •  Tags:  
  • Related