phpsymfonyphpunitsession-cookies

loginUser fails with subdomain in functional tests


My stack : Symfony 7 / php 8.3 / phpunit 9.6

I have several firewalls with subdomains. i try to test this one :

        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

When I call $this->client->loginUser($testUser); in my phpunit test, it doesn't works and my next request to a logged route send an "Full authentication is required to access this resource" error

in the framework.yaml file, i have this config :

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

When i look in the loginUser function at this line :

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

$domains remains empty

i tried to add this line in the phpunit.xml config but it doesn't change anything :

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

Any idea what I am missing or misunderstanding ?


Solution

  • Problem solved !

    the loginUser method second argument is useful ...

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

    I just had to set the firewall i wanted