【问题标题】:Override symfony TokenAuthenticator on phpunit tests在 phpunit 测试中覆盖 symfony TokenAuthenticator
【发布时间】:2020-08-09 08:41:53
【问题描述】:

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

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

我尝试了什么

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

路径: 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】:

    您不需要覆盖 Authenticator。

    您可以找到解决方案here

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 2021-02-08
      • 2018-03-04
      • 2022-01-27
      • 2018-06-16
      • 2015-01-31
      • 2015-04-26
      相关资源
      最近更新 更多