2

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?

yivi
  • 42,438
  • 18
  • 116
  • 138
Brucie Alpha
  • 1,135
  • 2
  • 12
  • 31

1 Answers1

2

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.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • Well the tests work for symfony 4 and 5 so there must be a change in symfony 6. While testing you had a special test.conainer that also allowed private services. – Brucie Alpha Jan 25 '22 at 14:04
  • Yes, you can test private services. But services that are not used are removed, so you cannot access them even if private. Creating an alias prevents the compilation step from removing the service from the container. – yivi Jan 25 '22 at 14:06
  • In any case, the easiest and best solution is exactly this: create an alias only on the test container. – yivi Jan 25 '22 at 14:15
  • Thanks for the answer. I have it to work, aliasing the service in the service.yaml file of the test environment. – user1077915 Nov 04 '22 at 17:03