在phpunit测试中覆盖symfony TokenAuthenticator

问题描述 投票:0回答:1

我通过扩展AbstractGuardAuthenticator类来创建自定义令牌身份验证类,如here所述。我的要求是接受一个jwt,将其提取并通过对照完全正常的数据库检查其id来识别用户。

现在我需要重写它的基础方法以在php单元测试中使用。简单来说,绕过它并设置一个默认用户。因此,当我执行功能测试时,无需每次都传递用户令牌即可测试API端点。

我尝试了什么

我试图在测试文件夹中定义一个单独的security.yaml,并使用它调用自定义类以覆盖它,但应用仍然调用默认类。

Path: config / packages / test / security.yaml

security:

  firewalls:

    main:
      anonymous: lazy
      logout: ~

      guard:
        authenticators:
          - Otrium\Infrastructure\Security\TestTokenAuthenticator
        entry_point: Otrium\Infrastructure\Security\TestTokenAuthenticator

      stateless: true

到目前为止,我发现的唯一解决方案是保留一个匹配的jwt并将其作为每个测试的标头值传递。

Symfony版本-5 / Php版本-7.4

php symfony security phpunit symfony5
1个回答
0
投票

您无需覆盖身份验证器。

您可以找到解决方案here

我在项目中使用了它,不需要在每个请求中都传递令牌。

© www.soinside.com 2019 - 2024. All rights reserved.