我正在尝试制作 vatin-bundle与 Symfony 6 兼容。
但是测试失败了
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.
这似乎是 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.
让validator
在测试中再次可用的首选方法是什么?
我发现的唯一方法是创建自己的别名,例如
services:
myvalidator:
alias: validator
public: true
并使用新别名。有更好的办法吗?
最佳答案
如果服务被删除,那么无论如何它都无法再访问。这不是可见性的问题,服务不再存在。因此,您需要防止服务被删除:创建别名是最好也是最简单的方法。
这已被维护者确认here .
您只能在测试期间创建别名,并且仍然可以访问原始服务。 (例如->get('validator')
。创建别名后,原始服务将不再被删除。
我认为这在 Symfony 6 中并不是什么新鲜事,但从 Symfony 4.4 开始就已经存在了。尽管现在在 Symfony 6 上确实如此,因为它删除了以前不推荐使用的行为,所以事情可能会发生变化。
关于php - 使私有(private)和已删除的服务在测试中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70848058/