7

I'm testing a class in laravel 5.4 with phpunit that uses the session to get data. Retrieving the session data works when accessing it directly when the app is running aka "session('consumer')" returns a value, however when i try to test a function that uses the session, I get the below error:

ReflectionException: Class session does not exist

Can someone please shed some light on how to accomplish setting session data during a test.

I have tried using:

$this->session()->put([$array_value]) but get an error "class session does not exist on class_im_testing"

Thanks in advance for your help!

public function test_get_user()
{
    $mockUser =  [
        'id' => 'cf442564-d076-4198-beb5-edef5aa51a69',
        'name' => 'John Doe',
        'fname' => 'John',
        'lname' => 'Doe',  
    ];

    session->put('consumer', [
        'id' => 'cf442564-d076-4198-beb5-edef5aa51a69',
        'name' => 'John Doe',
        'fname' => 'John',
        'lname' => 'Doe',  
    ]);

    $user = $this->repo->getUser();

    $this->assertEqual($user, $mockUser);
}

public function getUser()
{
   $user = new User(session('consumer'));
   return $user;
}
9
  • 3
    it should be $this->withSession([ ... ]) according to the docs Commented Aug 22, 2018 at 8:20
  • @apokryfos the error that gets returned when using "$this->withSession([...])" call to undefined function class_im_testing::withSession Commented Aug 22, 2018 at 8:26
  • @Leevashan that method should be in the test case class not in the class you're testing. Commented Aug 22, 2018 at 8:28
  • 2
    Make sure your test case extends the TestCase class that comes with laravel and not the default PhpUnit test case class Commented Aug 22, 2018 at 8:30
  • 2
    Laravel's test case extends this TestCase which has a lot of extra methods and also includes many traits. Your withSession method is actually from InteractsWithSession which is one of the traits included. Commented Aug 22, 2018 at 8:35

1 Answer 1

8

Laravel offers a lot of methods to help with testing but in order to make use of them you need to ensure you are using the correct class.

If you're using the default boilerplate you get a TestCase class. This is the class you need to extend in order to get method such as $this->withSession.

This is because the test case extends the Framework TestCase class which in turn includes many traits, among them is the the trait InteractsWithSession which is the trait which actually offers the method withSession.

In addition the Laravel TestCase also extends the PhpUnit test case so you still have access to all PhpUnit assertions.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your guidance, as Leevashan pointed out importing the correct TestCase class solved our issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.