简体   繁体   中英

Storing and retrieving session variables in unit/feature tests in Laravel

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?

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('/');
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM