Mauro Baptista

I'm a software developer! I'm comfortable with PHP and Javascript. Currently developing this blog and a limo reservation app using Laravel and TDD!

Always learning something new!

March 01, 2017 at 12:00 / By Mauro Baptista Laravel PHP

Fixing the TokenMismatchException on Laravel Test

It quite frustrating when you get the Illuminate\Session\TokenMismatchException when running your tests.

I've found a solution that is not beautiful, but it works!

Basically is add the following code (lines 10 to 12) on the class Illuminate\Foundation\EnvironmentDetector:

     * Get the environment argument from the console.
     * @param  array  $args
     * @return string|null
    protected function getEnvironmentArgument(array $args)
        if (isset($args[0]) && Str::endsWith($args[0], 'phpunit')) {
            return 'env=testing';

        return Arr::first($args, function ($value) {
            return Str::startsWith($value, '--env');

I have no idea, but the args that the Laravel returns when I'm using the PHPunit do not contain an --env. Including this if, I can force include this env and return the \App::environment() as testing.

Even though I'm setting < env name="APP_ENV" value="testing"/> on my phpunit.xml, I keep getting the App:environment() which the value that I set on .env file. That is the reason to include this if conditional.

Whoops, looks like something went wrong.

1/1 ErrorException in Filesystem.php line 111: file_put_contents(): Only 0 of 310 bytes written, possibly out of free disk space

  1. in Filesystem.php line 111
  2. at HandleExceptions->handleError('2', 'file_put_contents(): Only 0 of 310 bytes written, possibly out of free disk space', '/opt/bitnami/apps/', '111', array('path' => '/opt/bitnami/apps/', 'contents' => 'a:4:{s:6:"_token";s:40:"zX9JJgPlJtkGCq9BQrz5noS4J2ugcE4x3lwiDw2Y";s:9:"_previous";a:1:{s:3:"url";s:72:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159991;s:1:"c";i:1519159991;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', 'lock' => true))
  3. at file_put_contents('/opt/bitnami/apps/', 'a:4:{s:6:"_token";s:40:"zX9JJgPlJtkGCq9BQrz5noS4J2ugcE4x3lwiDw2Y";s:9:"_previous";a:1:{s:3:"url";s:72:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159991;s:1:"c";i:1519159991;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', '2') in Filesystem.php line 111
  4. at Filesystem->put('/opt/bitnami/apps/', 'a:4:{s:6:"_token";s:40:"zX9JJgPlJtkGCq9BQrz5noS4J2ugcE4x3lwiDw2Y";s:9:"_previous";a:1:{s:3:"url";s:72:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159991;s:1:"c";i:1519159991;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', true) in FileSessionHandler.php line 83
  5. at FileSessionHandler->write('gtlWL4r6BKtOQikc38ioHlCu23IfVj0ykL7DG71M', 'a:4:{s:6:"_token";s:40:"zX9JJgPlJtkGCq9BQrz5noS4J2ugcE4x3lwiDw2Y";s:9:"_previous";a:1:{s:3:"url";s:72:"";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159991;s:1:"c";i:1519159991;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}') in Store.php line 263
  6. at Store->save() in StartSession.php line 88
  7. at StartSession->terminate(object(Request), object(Response)) in Kernel.php line 177
  8. at Kernel->terminate(object(Request), object(Response)) in index.php line 56