phplaravelsessionlaravel-10php-8.1

Laravel session ID changes after every HTTP web request in a feature test


I'm writing a feature test and noticed that the session ID value changes after every request.

For instance, each dumped value will be different:

dump(session()->getId());

$response = $this->get('/?param=foo');

dump(session()->getId());

$response = $this->get('/?param=foo');

dump(session()->getId());

This issue doesn't seem to happen outside of the testing environment. I can reload a page in the browser and the session ID remains consistent.

This is my web middleware group, which is pretty standard out of the box:

\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,

I'm running this Laravel app on Valet on Mac with zero custom PHP configuration changes. Just whatever Homebrew provides with php@8.1.

Here are the session values from php -i:

session.auto_start => Off => Off
session.cache_expire => 180 => 180
session.cache_limiter => nocache => nocache
session.cookie_domain => no value => no value
session.cookie_httponly => no value => no value
session.cookie_lifetime => 0 => 0
session.cookie_path => / => /
session.cookie_samesite => no value => no value
session.cookie_secure => 0 => 0
session.gc_divisor => 1000 => 1000
session.gc_maxlifetime => 1440 => 1440
session.gc_probability => 1 => 1
session.lazy_write => On => On
session.name => PHPSESSID => PHPSESSID
session.referer_check => no value => no value
session.save_handler => files => files
session.save_path => no value => no value
session.serialize_handler => php => php
session.sid_bits_per_character => 5 => 5
session.sid_length => 26 => 26
session.upload_progress.cleanup => On => On
session.upload_progress.enabled => On => On
session.upload_progress.freq => 1% => 1%
session.upload_progress.min_freq => 1 => 1
session.upload_progress.name => PHP_SESSION_UPLOAD_PROGRESS => PHP_SESSION_UPLOAD_PROGRESS
session.upload_progress.prefix => upload_progress_ => upload_progress_
session.use_cookies => 1 => 1
session.use_only_cookies => 1 => 1
session.use_strict_mode => 0 => 0
session.use_trans_sid => 0 => 0
session.trans_sid_hosts => no value => no value
session.trans_sid_tags => a=href,area=href,frame=src,form= => a=href,area=href,frame=src,form=

In my phpunit.xml file:

<server name="SESSION_DRIVER" value="array"/>

What could cause the session to be regenerated every time?


Solution

  • Probably you're looking for this:

    $sessionId = session()->getId()
    $this->withCookies([
            session()->getName() => $sessionId
        ])->get('/?param=foo')
    

    More info about cookies in test environment: https://laravel.com/docs/10.x/http-tests#cookies

    You can also use withSession() to set custom session data (https://laravel.com/docs/10.x/http-tests#session-and-authentication)