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:
Solution
Clear the configuration cache:
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: