Guzzle Test mock oauth acess token

Viewed 122

In my tests, I "mock" my OAuth process as follows:

$oauthMiddleware = new OAuth2Middleware(new NullGrantType);
$oauthMiddleware->setAccessToken([
    "access_token" => "290473f650...",
    "expires_in" => 3600,
    "token_type" => "Bearer",
    "scope" => "*"
]);

$handlerStack = HandlerStack::create();
$handlerStack->push($oauthMiddleware);

$client = new GuzzleHttpClient([
    'handler' => $handlerStack,
    RequestOptions::AUTH => 'oauth',
]);

I follow the instructions given in the repository:

use kamermans\OAuth2\GrantType\NullGrantType;

$oauth = new OAuth2Middleware(new NullGrantType);
$oauth->setAccessToken([
    // Your access token goes here
    'access_token' => 'abcdefghijklmnop',
    // You can specify 'expires_in` as well, but it doesn't make much sense in this scenario
    // You can also specify 'scope' => 'list of scopes'
]);

But instead of using the manually set access token, it throws:

No access token is present, and there is no way to obtain one with NullGrantType.

I traced the exception back to kamermans\OAuth2\GrantType\NullGrantType::getRawData(). In the documentation, it says, for the exception not to be thrown, setAccessToken() must be called. But as you can see in my code, I do exactly that (correct me if I'm wrong). Does anyone see what I'm doing wrong?

0 Answers
Related