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

tomcodes's avatar

How to keep session between 2 HTTP tests?

I am working on tests on a new user to take an appointment with somebody.

I must be missing something critical because I can't keep the session between 2 assertions.

What am I doing wrong there?


    /** @test */
    public function we_should_be_able_to_take_an_appointment()
    {
        $user = User::find(1);
        $this->followingRedirects()
            ->post('/new-appointment', [
                '_token' => csrf_token(),
                'user_id' => $user->id,
                'start_at' => Carbon::now()->startOfWeek()->addDays(7)->setTime(10, 0, 0)->format('Y-m-d H:i:s'),
            ])
            ->assertSee('10:00');

        $this->get('/connection')
            ->assertSee('10:10');
}

The 2nd assertion does not work because I get redirected to /.

0 likes
2 replies
kingmaker_bgp's avatar
Level 8

I think you are missing being Logged In between 2 requests.

$user = User::find(1);
$this->actingAs($user);

$this->followingRedirects()
            ->post('/new-appointment', [
                '_token' => csrf_token(),
                'user_id' => $user->id,
                'start_at' => Carbon::now()->startOfWeek()->addDays(7)->setTime(10, 0, 0)->format('Y-m-d H:i:s'),
            ])
            ->assertSee('10:00');

        $this->get('/connection')
            ->assertSee('10:10');
tomcodes's avatar

Oh yes, good catch, thank you very much!

Please or to participate in this conversation.