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

Agelios's avatar

Session id changed

Hi. I have trouble with testing my api.

$phone = '6876876876';

        $this->json('post', route('api.client.login'), [
            'phone' => $phone
        ]);
        
        $this->json('post', route('api.client.login.confirm'), [
            'phone' => $phone,
            'code' => '12345678',
        ])->dump();

I need same session for both requests. How i can do it?

0 likes
3 replies
Agelios's avatar

I tried to rewrite call method

public function setUp()
    {
        parent::setUp();

        $this->session_id = session()->getId();
        $this->session_cookie = [session()->getName() => $this->session_id];
    }
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $cookies = array_merge($cookies, $this->session_cookie);
        parent::call($method, $uri, $parameters, $cookies, $files, $server, $content);
    }

but it works only for $this->get() and other like this, not for $this->json()

Agelios's avatar
Agelios
OP
Best Answer
Level 4

I solved the problem. Ty for all... Maybe someone will need it in the future

    protected $session_id;
    protected $session_cookie;

    public function setUp()
    {
        parent::setUp();

        $this->session_id = session()->getId();
        $this->session_cookie = [session()->getName() => encrypt($this->session_id)];
    }
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $cookies = array_merge($cookies, $this->session_cookie);
        return parent::call($method, $uri, $parameters, $cookies, $files, $server, $content);
    }

the problem was in cookie. Laravel need cookie which they can decrypt. And our call method needed to return parent::call()

Please or to participate in this conversation.