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

BernardoBF4's avatar

Testing session timeout

I've been trying to test if my user gets logged out after 120 minutes, but I've not been able to do this. Currently, my function that logs in my user is like this:

public function loginUser(LoginUserRequest $request)
  {
    $response = $this->auth_service->login($request->all());
    return $response;
  }

I've tried using Laravel's time travel to check if after two hours the user isn't logged anymore, but this simply doesn't work as I expected. Anyone knows what to do in this case?

public function a_user_is_logged_out_after_two_hours()
    {
        $password = $this->faker->password(6, 12);
        $user = User::factory()->withEncryptedPassword($password)->create();
        $credentials = ['usu_email' => $user->usu_email, 'usu_password' => $password];

        $this->post(route('cms.auth.log_user'), $credentials);

        $this->travel(121)->minutes();
        $this->assertEquals(null, auth()->user());
    }
0 likes
4 replies
Tray2's avatar

Not sure but it seems that you are only fooloing the test and not the framework, I think you need to do it the other way around, and travel back in time for the login and then travel back to present.

I never played around with time travelling, but if I remeber correctly @sinnbeck has.

1 like
BernardoBF4's avatar

@Tray2 So it would be something like this?

/** @test */
    public function a_user_is_logged_out_after_two_hours()
    {
        $password = $this->faker->password(6, 12);
        $user = User::factory()->withEncryptedPassword($password)->create();
        $credentials = ['usu_email' => $user->usu_email, 'usu_password' => $password];

        $this->travel(121)->minutes();
        $this->post(route('cms.auth.log_user'), $credentials);
        $this->travelBack();

        $this->assertEquals(null, auth()->user());
    }

Please or to participate in this conversation.