William Francis Gomes William Francis Gomes - 7 months ago 112
PHP Question

Call to undefined method Session::has() in Laravel 5 + Codeception functional testing

I was running some functional tests in Laravel 5 with Codeception.

Below is a sample test

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class HomePageTestCest
{
use WithoutMiddleware;

public function _before(FunctionalTester $I)
{
//some setup to run
}

public function _after(FunctionalTester $I)
{
}

// tests
public function tryToTest(FunctionalTester $I)
{
$I->wantToTest('If i can go to home page without errors');
$I->amOnPage('/');
$I->canSee('WELCOME TO HOMEPAGE');
}
}


But i am getting below error while running the test:

Fatal error: Call to undefined method Session::has() in /var/www/laravel5/storage/framework/views/e7953b3ce9b90e51d4cfdb279790953bbe25dc9a.php on line 225


And in line 225 i have:

<?php if(Session::has('someKey')): ?>
//some html code
<?php endif; ?>


I think the problem is with session driver. I also tried to implement this but nothing changed.

Hope will get some help. Thanks in advance.

Answer

In LARAVEL 5.2 they also create the session class like helper

where in you can make use of it like this

session()->has('key')

See DOCUMENTATION SESSION

namespace App\Http\Controllers;

// import session class using alias
use Session;

class SampleController exntends Controller {

    public function __construct() {

    }

    public function index() {
        // print session values
        var_dump(Session::all());
    }

}
Comments