vendredi 24 mai 2019

Phpunit - The data provider specified for method is invalid

I want to create a unit testing for Authentication with JWT in Laravel.

As PHPUnit shows to use data provider, use an array of arrays to pass data from a function to another. I mock a login request then get the token to use in logout, but the logout test always shows warning invalid data provider. Though I can var_dump token in login function, it seems to disappear in logout one. Below is my code:

public function test_successful_login() {

        $res = $this
        ->withHeaders([
            'accept' => 'application/json',
            'cache-control' => 'no-cache',
        ])
        ->post('/api/v1/auth/register', [
            'email' => $this->user->email,
            'name' => $this->user->name,
            'password' => $this->user->password,
        ])
        ->assertOk();

                // it's ok
                // var_dump(json_decode($res->getContent())->access_token);
                // die;

        return [['token' => json_decode($res->getContent())->access_token]];
    }

/**
 * @dataProvider test_successful_login
 */
public function test_successful_logout($token) {

        // invalid here
    // var_dump(func_get_args());
    // die;

    $this
    ->withHeaders([
        'cache-control' => 'no-cache',
        'accept' => 'application/json',
        'authorization' => 'Bearer '.$token,
    ])
    ->post('/api/v1/auth/logout')
    ->assertOk();;
}

How can I retrieve the token? Any help is appreciated.

I'm using Laravel 5.7 and PHPUnit 8.1



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire