首页 > 解决方案 > 在 Symfony 5.3 phpunit 测试期间,“security.user_password_hasher”的定义没有类?

问题描述

这个问题在某种程度上与:如何在 Symfony 5.3 phpunit 测试中访问私有服务?. 我通过以下方式设置环境services_test.yaml

services:
    security.user_password_hasher:
        public: true

当我尝试检索时,出现security.user_password_hasher异常:

$encoded = static::getContainer()->get('security.user_password_hasher')->hashPassword($user, $password);

Symfony\Component\DependencyInjection\Exception\RuntimeException:“security.user_password_hasher”的定义没有类。如果您打算在运行时动态注入此服务,请将其标记为 synthet=true。如果这是一个仅由子定义使用的抽象定义,请添加abstract=true,否则指定一个类以消除此错误。

为什么我得到这个异常?在我检索的同一个测试套件EntityManagerInterface::class中,一切都很好。

当我通过将其注入控制器来访问 env UserPasswordHasherInterface(它是 security.user_password_hasher 的别名)时,不会引发错误。为什么将其设置更改为崩溃测试?就是这个服务比较特殊,在环境中需要特别注意?或者可能是因为env 更改了一些默认设置?devpublic function homepage(UserPasswordHasherInterface $hash)public: truetesttest

我是 Symfony 的新手。我的猜测是容器做了一些隐藏的魔法。有人能透露秘密吗?什么意思:[注意] 在编译容器时,“security.user_password_hasher”服务或别名已被删除或内联?

标签: phpsymfonydependency-injectionsymfony5

解决方案


推荐阅读