Laravel phpunit: RuntimeException: Session store not set on request.

revision f91ab1018de2b6c5ec7dbd2a90b04f916757abd0

raw

README.rst

When using a method that requires a session in a unit test of a laravel based application, I encountered the following crash:

RuntimeException: Session store not set on request.

.../vendor/laravel/framework/src/Illuminate/Http/Request.php:415
.../vendor/kris/laravel-form-builder/src/Kris/LaravelFormBuilder/Fields/FormField.php:531
.../vendor/kris/laravel-form-builder/src/Kris/LaravelFormBuilder/Fields/FormField.php:284
.../vendor/kris/laravel-form-builder/src/Kris/LaravelFormBuilder/Fields/FormField.php:552
.../vendor/kris/laravel-form-builder/src/Kris/LaravelFormBuilder/Fields/FormField.php:121
.../vendor/kris/laravel-form-builder/src/Kris/LaravelFormBuilder/Form.php:181
[...]

I tried to solve it with the code given in https://github.com/laravel/framework/issues/9632

$this->app['request']->setSession($this->app['session']->driver('array'));

but now I got another exception:

TypeError:
 Argument 1 passed to Symfony\Component\HttpFoundation\Request::setSession()
 must be an instance of
  Symfony\Component\HttpFoundation\Session\SessionInterface,
 instance of Illuminate\Session\Store given

I do not know why the Illuminate Session Store does not implement Symfony's SessionInterface in Laravel 5.5. Looks like a bug to me.

My solution was to force the session into the request at the beginning of my test:

$req = $this->app['request'];
$sessionProp = new \ReflectionProperty($req, 'session');
$sessionProp->setAccessible(true);
$sessionProp->setValue($req, $this->app['session']->driver('array'));

History