Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fub's avatar
Level 4

Testing with session data (withSession()) fails

Hi,

I'm trying to write a simple test which should make use of some session data. For a simple access controll, a guest can create an item ("Tournament") and the ID of this item is saved in the session. The user should now be able to export some data for this item. Saving the data

I am using the following code

    public function test_admin_export()
    {
        $this->withoutExceptionHandling();

        $t = factory(Tournament::class)->create();

        $this->withSession(['tournament_admin' => [$t->id]])
            ->get('/')
            ->assertOk();
    }

This will return in the following error:

1) Tests\Feature\TournamentsTest::test_admin_export
ErrorException: Trying to get property 'cookies' of non-object

/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Session/CookieSessionHandler.php:69
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Session/Store.php:97
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Session/Store.php:87
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Session/Store.php:71
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Support/Manager.php:165
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:45
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:28
/Users/robin/development/projects/laravel/turnierbuddy/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php:15
/Users/robin/development/projects/laravel/turnierbuddy/tests/Feature/TournamentsTest.php:145

Line 145 is the line with withSession(...).

Any hint what is wrong here?

0 likes
7 replies
Nakov's avatar

@fub I tried to reproduce this, and I couldn't. The only similar error that I've seen is if you have Laravel Debugbar installed, that's where this error appeared. But do you have :

<server name="APP_ENV" value="testing"/>

In your phpunit.xml file, or in the .env.testing file APP_ENV=testing ?

fub's avatar
Level 4

I dont have the Debugbar installed.

My phpunit.xml does have the same line as you posted (APP_ENV = testing).

fub's avatar
Level 4

I just found a fix for this by adding this to my phpunit.xml:

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

I would have guessed that laravel does this automatically when app_env is set to testing.

Nakov's avatar
Nakov
Best Answer
Level 73

If that's not set Laravel uses the default one which should already be set to file. If you open config/session.php file. You will see.

I have:

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

in the phpunit.xml as it is much faster for testing, and I get a good result.

fub's avatar
Level 4

My config/session.php has this (I did not change it):

'driver' => env('SESSION_DRIVER', 'cookie'),

Thanks for the hint with array, I will use this.

Kayn3n's avatar

I'm having the same problem with the cookie in the tests, my phpunit.xml file already has a <server name="SESSION_DRIVER" value="file"/> tag but I still have the error

Please or to participate in this conversation.