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

bmartus's avatar
Level 10

Mock Session ID

I'm trying to mock the session ID so I can run some tests. The application I'm working on allows a guest to create content (keeping track of Session ID) and then if/when they register, the content is claimed and assigned to that user.

My test needs to generate a known session ID, assign it to the content and then assert that the content is assigned to the registered user.

Here is what I have (in a sample test, removing most of the functionality to isolate the issue) .. and below it is the error I'm getting. What do I need to be doing here to set a session ID and then use it in the test?

<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Session;
use Tests\TestCase;

class SessionTest extends TestCase
{
    /** @test */
    public function mocking_session_id()
    {
        $this->withoutExceptionHandling();

        Session::shouldReceive('getId')->andReturn('session_id_1234');

        // use factory to create content

        // register user, log in, etc.
        $response = $this->post('/session-test');

        // eventually assert that content belongs to user
        $response->assertOk();
    }
}
Route::post('/session-test', function () {

    dd(session()->getId());

});
There was 1 error:

1) Tests\Feature\SessionTest::mocking_session_id
Mockery\Exception\BadMethodCallException: Received Mockery_0_Illuminate_Session_SessionManager::getSessionConfig(), but no expectations were specified

Thanks!

0 likes
6 replies
guybrush_threepwood's avatar

I don't know much about testing, so forgive me if this is a stupid suggestion, but can't you use Laravel's withSession() method to set the session ID?

class SessionTest extends TestCase
{
    /** @test */
    public function mocking_session_id()
    {
        $this->withoutExceptionHandling();

        // use factory to create content

        // register user, log in, etc.
        $response = $this->withSession(['id' => 'session_id_1234'])->post('/session-test');

        // eventually assert that content belongs to user
        $response->assertOk();
    }
}

By the way, here are the Laravel Framework session tests, maybe you can pick up something useful: https://github.com/laravel/framework/blob/0b12ef19623c40e22eff91a4b48cb13b3b415b25/tests/Session/SessionStoreTest.php

bmartus's avatar
Level 10

Thanks for the reply. Looks like using withSession() just appears to set an id key/value in the session, but not the id of the session.

I'll give the Session tests a look, may be something in there that could help.

Thanks again..

1 like
dgershman's avatar

I want to do the same thing. Did you ever figure this out?

bmartus's avatar
Level 10

@dgershman nope. I put it on the back burner and never ended up working anything out.

Niush's avatar

@dgershman Don't know if this is helpful, or even enough for your use case. But can you try this:

use Illuminate\Support\Str;

Str::createRandomStringsUsing(fn($length) => str_repeat("x", $length));

// or even: createRandomStringsUsingSequence()

Such that the generated session id will be 40 x's.

clemblanco's avatar

For what it's worth, I was trying to achieve the same thing today and found a way to make it work.

test('something', function () {
    // important to be a string that is 40 characters long
    // and is a combination of letters and numbers
    $sessionId = 'aSm1hnkyHkbSxrPNMUDITgROcX88CoR2WULkgtHS';
    session()->setId($sessionId);
    // your test case will already have a session created at this time
    // and this will create another session
    session()->save();

    // this is where "the magic happens" where we tell Laravel which session ID to use
    $this->withCookie(config('session.cookie'), $sessionId)
        ->get('/your-endpoint');
});

Then in your routes/web.php you can have:

Route::get('/your-endpoint', function () {
    dd(session()->getId());
    // this should output "aSm1hnkyHkbSxrPNMUDITgROcX88CoR2WULkgtHS"
});

I've tried this with both file and array drivers, with cookie encryption turned off.

If you want to be able to do the same with cookie encryption turned on, you might need to encrypt the cookie the same way Laravel would before using withCookie().

Hope it helps ✌️

Please or to participate in this conversation.