3

In a Laravel feature test, I'm trying to store a variable in a session so that I can use it in the rest of my tests, like so:

public function testLogin()
{
    $response = $this->json('POST', '/login', $myCredentials);

    $response
        ->assertStatus(200)
        ->assertJson([
            'token' => true
        ]);

    session(['token' => $response['token']]);
}

When I run "phpunit" in the command line, I get this error:

PHP Fatal error: Uncaught ReflectionException: Class session does not exist in /vendor/laravel/framework/src/Illuminate/Container/Container.php:752

Apparently the "session()" global helper doesn't work in test classes. I also tried to work with the class directly by using "Illuminate\Session" or just "\Session", but both returned in "not found" errors. How can I store and retrieve session variables within test classes?

2 Answers 2

7

In tests it's a bit different.

https://laravel.com/docs/5.2/testing#sessions-and-authentication

Here is an example:

public function testApplication()
{
    $this->withSession(['foo' => 'bar'])
         ->visit('/');
}
Sign up to request clarification or add additional context in comments.

2 Comments

What I'm trying to do is get a token variable from my first request, then store it and use it across multiple methods in multiple classes. I don't know how I could accomplish this with the withSession() method, because what I need would present a recursive problem: "$this->withSession(['token' => session('token')]". Or do I just need to retrieve the token on every class? I'm trying to avoid unnecessary HTTP requests.
I guess I could get it one time in a master test class, then extend this to the other classes. That way I don't need to rely on sessions.
0

There a way to do that you want. The unic problem it's that doesn't work with session.

When you start the test, you must generate the function "master" that will call the rest of functions.

    /**
         * Try to login the api client (if you have another middleware use it)
         * @group groupTests
         * @test
         */
        public function masterFunction() {
            //create the body data to try generate the oauth token
            $body = [
                'client_id' => $this->client_id_test,
                'client_secret' => $this->secret,
                'grant_type' => 'client_credentials',
                'scope' => ''
            ];
            //get the response with the data
            $response = $this->json('POST','/oauth/token',$body,['Accept' => 'application/json']);
            //check that return a valid token
            $response->assertStatus(200)->assertJsonStructure(['token_type','expires_in','access_token']);
            //get token data in var
            $token = $response->json("token_type")." ".$response->json("access_token");
            //send string token to the next function
            $this->childrenFunction($token);
        }

When you construct "children functions" must make them like this:

/**
     * This function get the token as param
     * @param String $token The token that we want
     * @group groupTests
     */
    private function childrenFunction($token){
        //here can call to $token as a var
        dd($token);
    }

It's important that "children functions" doesn't have * @test at the header description.

Comments

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.