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

AlexG834's avatar

TokenMismatchException for Tests

Hi all. I'm starting a new project and I've just written my first test, but it's failing on CSRF validation. I was under the impression that CSRF validation was disabled for testing? Where does the logic live to disable this?


/** @test */
        public function a_user_can_open_a_bank_account () {


            // Given we have a user
            $this->signIn();

            // And a new bank account
            $newBankAccount = make(BankAccount::class, [
                'user_id' => auth()->id()
            ]);

            // And they submit the form to open a new bank account
            $this->post(auth()->id() . '/banking/account', $newBankAccount->toArray());

            // Then they should have a bank account associated with them
            $this->assertEquals(1, auth()->user()->bankAccounts()->count());
        }
Illuminate\Session\TokenMismatchException

0 likes
8 replies
Cronix's avatar

Or if you'd rather not disable security to test this, add the token to the data being sent in the post

$newBankAccount = make(BankAccount::class, [
    'user_id' => auth()->id()
]);

$newBankAccount = $newBankAccount->toArray();
$newBankAccount['_token'] = csrf_token(); // add token

$this->post(auth()->id() . '/banking/account', $newBankAccount);
2 likes
AlexG834's avatar

Thanks guys. Shouldn't this be automatically disabled in 5.6 though?

Cronix's avatar

Why would it be disabled? It should work the way it normally would in a production scenario, shouldn't it? Or what are you really testing?

AlexG834's avatar

It did, thanks. I was just curious as to why it wasn't being disabled out of the box.

Please or to participate in this conversation.