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

dacastro4's avatar

Getting status 419 running a phpunit test

Hey Guys. I have a weird issue that i don’t understand entirely. I’m running a test but a I’m getting a ng

$member = factory(User::class)->create([ 'type' => 'member' ]);
$title = "Random Title for Test.";
$case = factory(DentalCase::class)->raw(['title' => $title]);

$response = $this->actingAs($member)
->post('/cases/create', $case);

$response->assertStatus(302);
$this->assertDatabaseHas('cases', [
'title' => $title,
]);
0 likes
9 replies
ejdelmonico's avatar

Did you login or authorize $member before creating the record? I believe that code means unauthorized because of timeout. Like the session expired or something.

ejdelmonico's avatar

419 is an old authentication error code that phpunit still uses. Try use WithoutMiddleware at the top of the class. It disables middleware for testing. It could also be a token mismatch exception.

dacastro4's avatar

@ejdelmonico Yeah, I know that use WithoutMiddleware solves the problem but for my co-workers who is pulled my branch, everything is working perfectly.

iraklisg's avatar

I am facing a similar problem... My tests run succesfully on my local machine but when I am trying to run my tests via docker (utilizing an app and a db service) I get the following error

Expected status code 200 but received 419.

Adding $this->withoutMiddleware(); in my test function solves the problem, although I cannot figure out why is running succesfully out of docker and fails otherwise :confused:

iraklisg's avatar

I finally figured out what the problem was with Expected status code 200 but received 419 when running tests via docker. Specifically, my test parameters (incl test databases names, etc) existed on a .env.testing file but I was mistekenly using the paameters defined on .env file. By correcting this everything runs smoothly :sunglasses:

thomaswardiii@gmail.com's avatar

The middleware comes with code built-in that detects if it is being used in a test. This check looks for 2 things:

  • Am I being ran via a command line
  • Am I being ran in an environment type of testing

Default, proper setup of the software correctly causes both flags to be true when running PHP unit. However, the most likely culprit is the value in your APP_ENV. Common ways for this to to be incorrect include:

  • Misconfigured phpunit.xml file. It should contain <server name="APP_ENV" value="testing" />
  • A shell session that has an explicit value set in APP_ENV that overrides this value
  • A Docker/docker-compose/kubernetes session that has an explicit value set in APP_ENV. Seeing about getting this value set via the .env and/or phpunit.xml files is perhaps better if possible. Or ensuring the build/test process sets the value.

This one stumped me as well and I was not convinced that I would need the use of WithoutMiddleware since I did not for a different project, but it turned out I had experimented with something on the command line and overrode APP_ENV in bash.

1 like
Yavor2's avatar

I also had a similar problem, on production in a docker container, some tests returned error "Expected response status code [200] but received 419". So, in order to solve this error, it was necessary to clear the config cache before running the tests, as it says in the documentation:

php artisan config:clear
6 likes

Please or to participate in this conversation.