Skip to content

Commit

Permalink
Improve code coverage in provider
Browse files Browse the repository at this point in the history
And refactor to require less overloading.
  • Loading branch information
shadowhand committed Mar 8, 2016
1 parent 401c923 commit 53affda
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
26 changes: 9 additions & 17 deletions src/Untappd.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ public function getBaseAuthorizationUrl()

public function getBaseAccessTokenUrl(array $params)
{
return 'https://untappd.com/oauth/authorize';
// Untappd requires inclusion of additional params for verification
return 'https://untappd.com/oauth/authorize?' .
http_build_query(array_replace($params, [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_url' => $this->redirectUri,
]));
}

public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return $this->appendQuery(
'https://api.untappd.com/v4/user/info',
return 'https://api.untappd.com/v4/user/info?' .
http_build_query([
'access_token' => (string) $token,
])
);
]);
}

protected function getDefaultScopes()
Expand Down Expand Up @@ -59,18 +63,6 @@ protected function getAccessTokenMethod()
return self::METHOD_GET;
}

protected function getAccessTokenUrl(array $params)
{
// Untappd requires inclusion of additional params for verification
$params = array_replace($params, [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_url' => $this->redirectUri,
]);

return parent::getAccessTokenUrl($params);
}

protected function prepareAccessTokenResponse(array $result)
{
// Untappd wraps the response to include metadata
Expand Down
55 changes: 54 additions & 1 deletion test/src/UntappdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ public function testAuthorizationUrl()
$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_url', $query);
$this->assertArrayHasKey('response_type', $query);

$this->assertSame('mock_client_id', $query['client_id']);
$this->assertSame('none', $query['redirect_url']);
}

public function testBaseAccessTokenUrl()
{
$url = $this->provider->getBaseAccessTokenUrl([]);
$uri = parse_url($url);
parse_str($uri['query'], $query);

$this->assertEquals('/oauth/authorize', $uri['path']);

$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('client_secret', $query);
$this->assertArrayHasKey('redirect_url', $query);

$this->assertSame('mock_client_id', $query['client_id']);
$this->assertSame('mock_secret', $query['client_secret']);
$this->assertSame('none', $query['redirect_url']);
}

public function testResourceOwnerDetailsUrl()
Expand All @@ -58,6 +70,47 @@ public function testResourceOwnerDetailsUrl()

}

public function testAccessToken()
{
$response = m::mock('GuzzleHttp\Psr7\Response');

$response->shouldReceive('getHeader')
->with('content-type')
->andReturn(['application/json']);

$json = <<<EOJ
{
"meta": {
"http_code": 200
},
"response": {
"access_token": "untappd_token"
}
}
EOJ;

$response->shouldReceive('getBody')
->andReturn($json);

$provider = m::mock('Shadowhand\OAuth2\Client\Provider\Untappd[sendRequest]')
->shouldAllowMockingProtectedMethods();

$provider->shouldReceive('sendRequest')
->with(m::on(function ($request) {
$this->assertSame('GET', $request->getMethod());
return true;
}))
->times(1)
->andReturn($response);

$token = $provider->getAccessToken('authorization_code', [
'code' => 'mock-code',
]);

$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $token);
$this->assertSame('untappd_token', $token->getToken());
}

public function testUserData()
{
$json = <<<EOJ
Expand Down Expand Up @@ -154,7 +207,7 @@ public function testErrorResponse()
$json = <<<EOJ
{
"meta": {
"code": 500,
"http_code": 500,
"error_detail": "The user has not authorized this application or the token is invalid.",
"error_type": "invalid_auth",
"developer_friendly": "The user has not authorized this application or the token is invalid.",
Expand Down

0 comments on commit 53affda

Please sign in to comment.