6

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;
}
  • 3
    it should be `$this->withSession([ ... ])` according to [the docs](https://laravel.com/docs/5.4/http-tests#session-and-authentication) – apokryfos Aug 22 '18 at 08:20
  • @apokryfos the error that gets returned when using "$this->withSession([...])" **call to undefined function class_im_testing::withSession** – Leevashan Aug 22 '18 at 08:26
  • @Leevashan that method should be in the test case class not in the class you're testing. – apokryfos Aug 22 '18 at 08:28
  • @apokryfos maybe we arent instantiating something that would allow us to use session within our test, any suggestions? – Leevashan Aug 22 '18 at 08:28
  • 2
    Make sure your test case extends the [`TestCase`](https://github.com/laravel/laravel/blob/5.4/tests/TestCase.php) class that comes with laravel and not the default PhpUnit test case class – apokryfos Aug 22 '18 at 08:30
  • @apokryfos our test case class extends **use PHPUnit\Framework\TestCase;** and the "TestCase" abstract class does not include a "withSession" within it. – Leevashan Aug 22 '18 at 08:33
  • @apokryfos seems that the test class is infact importing the phpunit testCase class instead of the laravels testCase, will update now and check, thanks man! – Leevashan Aug 22 '18 at 08:34
  • 2
    Laravel's test case extends [this `TestCase`](https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Testing/TestCase.php) which has a lot of extra methods and also includes many traits. Your `withSession` method is actually from [`InteractsWithSession`](https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Testing/Concerns/InteractsWithSession.php) which is one of the traits included. – apokryfos Aug 22 '18 at 08:35
  • @apokryfos thank you for your guidance, importing the correct TestCase class worked :), we will mark your response as the accepted answer. – Leevashan Aug 22 '18 at 08:42

1 Answers1

7

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.

apokryfos
  • 38,771
  • 9
  • 70
  • 114