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

Alphy Gacheru's avatar

How to update Laravel session Id in testing

How can I make the get request load with its session id as what is initially created with $sessionId = session()->getId();

public function test_chatbot_messages_page_has_non_empty_chat(): void
  {
    $sessionId = session()->getId();

    ChatbotMessage::create([
      'session_id' => $sessionId,
      'content' => 'Hello',
      'role' => 'You'
    ]);

    ChatbotMessage::create([
      'session_id' => $sessionId,
      'content' => 'Hi there',
      'role' => 'Bot'
    ]);

    $this->get(route('chatbot-messages.index'));
  }
0 likes
6 replies
tisuchi's avatar

@alphy gacheru Can you try this?

/** @test */
public function chatbot_messages_page_has_non_empty_chat()
{
    // Start the session and get the session ID.
    session()->start();
    $sessionId = session()->getId();

    // Create chatbot messages.
    ChatbotMessage::create([
        'session_id' => $sessionId,
        'content' => 'Hello',
        'role' => 'You'
    ]);

    ChatbotMessage::create([
        'session_id' => $sessionId,
        'content' => 'Hi there',
        'role' => 'Bot'
    ]);

    // Make a GET request to the chatbot-messages.index route with the session ID.
    $response = $this->withSession(['_token' => csrf_token()])
                     ->get(route('chatbot-messages.index'));

    // Add your assertions here.
}
Alphy Gacheru's avatar

@tisuchi Does not work.

I have the following in my controller which works. But when I die and dump the $response in the test in this case. It doesn't have the messages. Seems like the session id does not match.

    public function index()
  {
    $conversationHistory = ChatbotMessage::select(['content', 'role'])
      ->where('session_id', session()
        ->getId())
      ->get();

    return view('chatbot-messages.index', compact('conversationHistory'));
  }
tisuchi's avatar

@Alphy Gacheru If you dd session()->getId(); in the test, do you get anything?

Note: Make sure you start the session first.

Alphy Gacheru's avatar

@tisuchi Yes, even when I dd(ChatbotMessage::all()) before making the get request in the test I get the objects with the session_id created.

I've also tried dd($sessionId, session()->getId()); after the get request. The ids are different.

tisuchi's avatar

@Alphy Gacheru Can you try this?

/** @test */
public function chatbot_messages_page_has_non_empty_chat()
{
    // Start the session.
    $this->startSession();

    // Get the session ID.
    $sessionId = session()->getId();

    // Ensure that the session ID is not empty.
    $this->assertNotEmpty($sessionId);

    // Create chatbot messages.
    ChatbotMessage::create([
        'session_id' => $sessionId,
        'content' => 'Hello',
        'role' => 'You'
    ]);

    ChatbotMessage::create([
        'session_id' => $sessionId,
        'content' => 'Hi there',
        'role' => 'Bot'
    ]);

    $response = $this->get(route('chatbot-messages.index'));

    // You can add assertions here to ensure that the response contains the expected data.
    $response->assertSee('Hello');
    $response->assertSee('Hi there');
}

Please or to participate in this conversation.