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

PaulAdams's avatar

PEST: Error from session() helper when testing with PEST.

Hello SuperDevs, How so I write a test in PEST for a function that uses the session() helper? My test is in the response below. When I run it, I get BindingResolutionException Target class [session] does not exist.

Thank you for reading my request :) I can't find an answer in the docs.

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! In Pest PHP, you can test functions that use the Laravel session() helper by manipulating the session directly in your tests. Pest uses the same underlying framework as PHPUnit, so you can interact with the session in a similar way as you would in a typical Laravel PHPUnit test.

Here's an example of how you might write a test that interacts with the session:

test('a function that uses the session helper', function () {
    // Set a value in the session before calling the function
    session(['key' => 'value']);

    // Call your function that uses the session() helper
    $result = yourFunctionThatUsesSession();

    // Make your assertions
    expect($result)->toBe('expected result');

    // You can also assert if the session has certain data
    expect(session('key'))->toBe('value');

    // If you need to test flash data, you can do the following
    session()->flash('flash_key', 'flash_value');
    expect(session('flash_key'))->toBe('flash_value');
});

// If you need to test with a fresh session, you can use the `withSession` helper
test('a function with a fresh session', function () {
    $this->withSession(['key' => 'value']);

    // Your test code here
});

In the above example, yourFunctionThatUsesSession() represents the function you're testing, which utilizes the session() helper. You can set session data before invoking your function and then assert that the function behaves as expected, possibly by checking the session values after the function is called.

Remember that when testing web routes or controller methods that rely on session data, you might need to make HTTP requests and pass session data along with those requests. Here's an example of how you might do that:

test('a web route that uses the session', function () {
    $response = $this->withSession(['key' => 'value'])->get('/your-route');

    // Assert the response status, content, or session data
    $response->assertStatus(200);
    $response->assertSessionHas('key', 'value');
});

In this example, $this->withSession(['key' => 'value']) sets the session data before making a GET request to /your-route. You can then make assertions about the response and the session data.

Remember to import the necessary classes and functions at the top of your test file if they are not already imported:

use function Pest\Laravel\test;

This should give you a good starting point for writing tests that involve the session in Pest PHP.

PaulAdams's avatar

No luck, I'm afraid. I get BindingResolutionException Target class [session] does not exist.

This is my test:

<?php

use App\Actions\GuestSession;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Session;

beforeEach(function () {
    $this->guestSession = new GuestSession;
});

it('returns existing session id if present', function () {
    $existingSessionId = (string) Str::uuid();
    Session::shouldReceive('get')
        ->once()
        ->with('sessionId')
        ->andReturn($existingSessionId);

    $sessionId = $this->guestSession->get();

    expect($sessionId)->toBe($existingSessionId);
});

And this is the class I am testing:

<?php

namespace App\Actions;

use Illuminate\Support\Str;

final class GuestSession
{
    public function get(){
        $sessionId = session()->get('sessionId');
        
        if (! $sessionId) {
            $sessionId = Str::uuid()->toString();
            session()->put('sessionId', $sessionId);
        }

        return $sessionId;
    }
    
}

When I call the session() helper, I get the error stated above: BindingResolutionException Target class [session] does not exist.

Please or to participate in this conversation.