loginUser 在功能测试中因子域失败

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

我的堆栈:Symfony 7 / php 8.3 / phpunit 9.6

我有几个带有子域的防火墙。我尝试测试一下这个:

        front:
        host: ^(app\.mydomain\.localhost)|(app\.mydomain)$
        pattern: ^/front
        provider: app_user_provider
        json_login:
            login_path: app_login
            check_path: app_login
        logout:
            path: app_user_logout

当我在 phpunit 测试中调用

$this->client->loginUser($testUser);
时,它不起作用,并且我对记录路由的下一个请求发送“访问此资源需要完全身份验证”错误

在framework.yaml文件中,我有这个配置:

framework:
    secret: '%env(APP_SECRET)%'
    #csrf_protection: true
    trusted_proxies: '127.0.0.1,REMOTE_ADDR'
    trusted_headers: [ 'x-forwarded-for', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-port' ]


    # Note that the session will be started ONLY if you read or write from it.
    session:
        cookie_domain: .mydomain.localhost
        cookie_lifetime: 604800

    #esi: true
    #fragments: true

when@test:
    framework:
        test: true
        trusted_hosts: ['app.mydomain.localhost']
        session:
            storage_factory_id: session.storage.factory.mock_file
            cookie_domain: .mydomain.localhost

当我查看这一行的 loginUser 函数时:

$domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $this->getCookieJar()->all())) ?: [''];

$domains 仍为空

我尝试在 phpunit.xml 配置中添加这一行,但它没有改变任何东西:

<ini name="session.cookie_domain" value=".mydomain.localhost"/>

知道我遗漏或误解了什么吗?

symfony phpunit session-cookies
1个回答
0
投票

问题解决了!

loginUser 方法的第二个参数很有用...

public function loginUser(object $user, string $firewallContext = 'main' ...

我只需设置我想要的防火墙

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