Unit Test - "Session store not set on request."

Submitted by orlissenberg - 2 years ago

Testing an action with a session.

class YourControllerTest extends TestCase
{
    /**
     * @var \Illuminate\Session\SessionManager
     */
    protected $manager;

    public function setUp()
    {
        parent::setUp();

        // Avoid "Session store not set on request." - Exception!
        Session::setDefaultDriver('array');
        $this->manager = app('session');

        $user = \App\Models\User::where('user_id_ish', 'user-identifier')->first();
        Auth::setUser($user);
    }

    /**
     * Test some controller logic.
     */
    public function testYourActionFunction()
    {
        /** @var YourController $controller */
        $controller = app(YourController::class);
        
        $request = new Request();
        $request->setSession($this->manager->driver());
        $request->session()->set('something', []);

        $result = $controller->yourActionFunction($request);
    }
}

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks