Skip to content

Commit

Permalink
Merge pull request #163 from bajb/master
Browse files Browse the repository at this point in the history
Improvements to AbstractProvider
  • Loading branch information
ramsey committed Dec 15, 2014
2 parents deb6d7a + bc3845d commit e5efa75
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ abstract class AbstractProvider implements ProviderInterface

public $headers = null;

/**
* @var GuzzleClient
*/
protected $httpClient;

protected $redirectHandler;
Expand Down Expand Up @@ -93,7 +96,7 @@ abstract public function urlAccessToken();
* @param AccessToken $token
* @return string
*/
abstract public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token);
abstract public function urlUserDetails(AccessToken $token);

/**
* Given an object response from the server, process the user details into a format expected by the user
Expand All @@ -103,7 +106,7 @@ abstract public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken
* @param AccessToken $token
* @return mixed
*/
abstract public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token);
abstract public function userDetails($response, AccessToken $token);

public function getScopes()
{
Expand Down Expand Up @@ -131,6 +134,7 @@ public function getAuthorizationUrl($options = [])
return $this->urlAuthorize().'?'.$this->httpBuildQuery($params, '', '&');
}

// @codeCoverageIgnoreStart
public function authorize($options = [])
{
$url = $this->getAuthorizationUrl($options);
Expand Down Expand Up @@ -174,8 +178,8 @@ public function getAccessToken($grant = 'authorization_code', $params = [])
// @codeCoverageIgnoreStart
// No providers included with this library use get but 3rd parties may
$client = $this->getHttpClient();
$client->setBaseUrl($this->urlAccessToken().'?'.$this->httpBuildQuery($requestParams, '', '&'));
$request = $client->send();
$client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&'));
$request = $client->get(null, null, $requestParams)->send();
$response = $request->getBody();
break;
// @codeCoverageIgnoreEnd
Expand Down Expand Up @@ -263,13 +267,29 @@ public function getUserScreenName(AccessToken $token)
return $this->userScreenName(json_decode($response), $token);
}

public function userUid($response, AccessToken $token)
{
return isset($response->id) && $response->id ? $response->id : null;
}

public function userEmail($response, AccessToken $token)
{
return isset($response->email) && $response->email ? $response->email : null;
}

public function userScreenName($response, AccessToken $token)
{
return isset($response->name) && $response->name ? $response->name : null;
}

/**
* Build HTTP the HTTP query, handling PHP version control options
*
* @param array $params
* @param integer $numeric_prefix
* @param string $arg_separator
* @param null|integer $enc_type
*
* @return string
* @codeCoverageIgnoreStart
*/
Expand Down
55 changes: 55 additions & 0 deletions test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace League\OAuth2\Client\Test\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use Mockery as m;

class AbstractProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AbstractProvider
*/
protected $provider;

protected function setUp()
Expand Down Expand Up @@ -87,6 +92,56 @@ public function testSetRedirectHandler()

$this->assertNotFalse($this->testFunction);
}

/**
* @param $response
*
* @dataProvider userPropertyProvider
*/
public function testGetUserProperties($response, $name = null, $email = null, $id = null)
{
$token = new AccessToken(['access_token' => 'abc', 'expires_in' => 3600]);

$provider = $this->getMockForAbstractClass(
'\League\OAuth2\Client\Provider\AbstractProvider',
[
[
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]
]
);

/**
* @var $provider AbstractProvider
*/

$this->assertEquals($name, $provider->userScreenName($response, $token));
$this->assertEquals($email, $provider->userEmail($response, $token));
$this->assertEquals($id, $provider->userUid($response, $token));
}

public function userPropertyProvider()
{
$response = new \stdClass();
$response->id = 1;
$response->email = 'test@example.com';
$response->name = 'test';

$response2 = new \stdClass();
$response2->id = null;
$response2->email = null;
$response2->name = null;

$response3 = new \stdClass();

return [
[$response, 'test', 'test@example.com', 1],
[$response2],
[$response3],
];
}
}

class MockProvider extends \League\OAuth2\Client\Provider\AbstractProvider
Expand Down

0 comments on commit e5efa75

Please sign in to comment.