4 min read

Test 419 Page Expired HTTP Response

Problem

You submit a form in your phpunit tests and expect an HTTP ok (200), redirect (302) or created (201) response but instead receieve a ‘page expired’ (419).

Your test might look something like this:

           $response = $this->post('/contact-us', ['email' => '[email protected]', 'message' => 'Hello world!'])
     ->assertStatus(302);
        

Solution

Clear the configuration cache:

           php artisan config:clear
        

Explanation

PHPUnit will use the cached configuration values instead of the .env.testing. This causes the APP_ENV value to remian unchanged (e.g. local, development, production etc) instead of testing, the VerifyCsrfTokenMiddleware will throw a TokenMismatchException (Status code 419).

It won’t throw this exception when the APP_ENV is set to testing since the handle method of VerifyCsrfTokenMiddleware checks if you are running unit tests with $this->runningUnitTests().

It is recommended not to cache your configuration in your development environment. When you need to cache your configuration in the environment where you are also running unit tests you could clear your cache manually in your TestCase.php:

           public function createApplication()
 {
     ....
     Artisan::call('config:clear')
     ....
 }