Chron's avatar
Level 6

Is the session not expiring in tests?

I tried having this simple test

 public function test_if_session_has_expired(): void {
        $user = User::factory()->create(['type' => 'admin']);
        $this->actingAs($user);

        $loggedInResponse = $this->get(route('dashboard'));

        $loggedInResponse->assertOk();

		// SESSION_LIFETIME=3
        $this->travel(500)->minutes();

        $reloadResponse = $this->get(route('dashboard'));
        $reloadResponse->assertOk();
    }

It should fail and redirect to login page but it passed.

1 like
2 replies
imrandevbd's avatar

Laravel’s in-memory “array” session driver (the default in tests) doesn’t enforce cookie lifetimes, so time-travel won’t expire it. Switch your TEST SESSION_DRIVER to “file” or “database” (e.g. in phpunit.xml) or manually expire the session cookie to test expiry.

1 like

Please or to participate in this conversation.