100% code coverage
parent
1a190e760f
commit
375d01c0b8
|
@ -48,6 +48,6 @@ class Eventbrite extends IdentityProvider
|
||||||
|
|
||||||
public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token)
|
public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token)
|
||||||
{
|
{
|
||||||
return array($response->first_name, $response->last_name);
|
return $response->user->user_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,7 +111,7 @@ abstract class IdentityProvider
|
||||||
}
|
}
|
||||||
$grant = new $grant;
|
$grant = new $grant;
|
||||||
} elseif (! $grant instanceof GrantInterface) {
|
} elseif (! $grant instanceof GrantInterface) {
|
||||||
throw new \InvalidArgumentException($grant.' is not an instance of League\OAuth2\Client\Grant\GrantInterface');
|
throw new \InvalidArgumentException(get_class($grant) . ' is not an instance of League\OAuth2\Client\Grant\GrantInterface');
|
||||||
}
|
}
|
||||||
|
|
||||||
$defaultParams = array(
|
$defaultParams = array(
|
||||||
|
@ -126,21 +126,30 @@ abstract class IdentityProvider
|
||||||
try {
|
try {
|
||||||
switch (strtoupper($this->method)) {
|
switch (strtoupper($this->method)) {
|
||||||
case 'GET':
|
case 'GET':
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
// No providers included with this library use get but 3rd parties may
|
||||||
$client = $this->getHttpClient();
|
$client = $this->getHttpClient();
|
||||||
$client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&', PHP_QUERY_RFC1738));
|
$client->setBaseUrl($this->urlAccessToken() . '?' . $this->httpBuildQuery($requestParams, '', '&', PHP_QUERY_RFC1738));
|
||||||
$request = $client->send();
|
$request = $client->send();
|
||||||
$response = $request->getBody();
|
$response = $request->getBody();
|
||||||
break;
|
break;
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
case 'POST':
|
case 'POST':
|
||||||
$client = $this->getHttpClient();
|
$client = $this->getHttpClient();
|
||||||
$client->setBaseUrl($this->urlAccessToken());
|
$client->setBaseUrl($this->urlAccessToken());
|
||||||
$request = $client->post(null, null, $requestParams)->send();
|
$request = $client->post(null, null, $requestParams)->send();
|
||||||
$response = $request->getBody();
|
$response = $request->getBody();
|
||||||
break;
|
break;
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
default:
|
||||||
|
throw new \InvalidArgumentException('Neither GET nor POST is specified for request');
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
} catch (BadResponseException $e) {
|
} catch (BadResponseException $e) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
$raw_response = explode("\n", $e->getResponse());
|
$raw_response = explode("\n", $e->getResponse());
|
||||||
$response = end($raw_response);
|
$response = end($raw_response);
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->responseType) {
|
switch ($this->responseType) {
|
||||||
|
@ -153,36 +162,38 @@ abstract class IdentityProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($result['error']) && ! empty($result['error'])) {
|
if (isset($result['error']) && ! empty($result['error'])) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
throw new IDPException($result);
|
throw new IDPException($result);
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
return $grant->handleResponse($result);
|
return $grant->handleResponse($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUserDetails(AccessToken $token, $force = false)
|
public function getUserDetails(AccessToken $token)
|
||||||
{
|
{
|
||||||
$response = $this->fetchUserDetails($token);
|
$response = $this->fetchUserDetails($token);
|
||||||
|
|
||||||
return $this->userDetails(json_decode($response), $token);
|
return $this->userDetails(json_decode($response), $token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUserUid(AccessToken $token, $force = false)
|
public function getUserUid(AccessToken $token)
|
||||||
{
|
{
|
||||||
$response = $this->fetchUserDetails($token, $force);
|
$response = $this->fetchUserDetails($token, true);
|
||||||
|
|
||||||
return $this->userUid(json_decode($response), $token);
|
return $this->userUid(json_decode($response), $token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUserEmail(AccessToken $token, $force = false)
|
public function getUserEmail(AccessToken $token)
|
||||||
{
|
{
|
||||||
$response = $this->fetchUserDetails($token, $force);
|
$response = $this->fetchUserDetails($token, true);
|
||||||
|
|
||||||
return $this->userEmail(json_decode($response), $token);
|
return $this->userEmail(json_decode($response), $token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUserScreenName(AccessToken $token, $force = false)
|
public function getUserScreenName(AccessToken $token)
|
||||||
{
|
{
|
||||||
$response = $this->fetchUserDetails($token, $force);
|
$response = $this->fetchUserDetails($token, true);
|
||||||
|
|
||||||
return $this->userScreenName(json_decode($response), $token);
|
return $this->userScreenName(json_decode($response), $token);
|
||||||
}
|
}
|
||||||
|
@ -195,6 +206,7 @@ abstract class IdentityProvider
|
||||||
* @param string $arg_separator
|
* @param string $arg_separator
|
||||||
* @param null|integer $enc_type
|
* @param null|integer $enc_type
|
||||||
* @return string
|
* @return string
|
||||||
|
* @codeCoverageIgnoreStart
|
||||||
*/
|
*/
|
||||||
protected function httpBuildQuery($params, $numeric_prefix = 0, $arg_separator = '&', $enc_type = null)
|
protected function httpBuildQuery($params, $numeric_prefix = 0, $arg_separator = '&', $enc_type = null)
|
||||||
{
|
{
|
||||||
|
@ -210,7 +222,7 @@ abstract class IdentityProvider
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fetchUserDetails(AccessToken $token, $force = false)
|
protected function fetchUserDetails(AccessToken $token)
|
||||||
{
|
{
|
||||||
$url = $this->urlUserDetails($token);
|
$url = $this->urlUserDetails($token);
|
||||||
|
|
||||||
|
|
|
@ -15,13 +15,15 @@ class User
|
||||||
protected $imageUrl;
|
protected $imageUrl;
|
||||||
protected $urls;
|
protected $urls;
|
||||||
|
|
||||||
public function __get($property) {
|
public function __get($property)
|
||||||
|
{
|
||||||
if (property_exists($this, $property)) {
|
if (property_exists($this, $property)) {
|
||||||
return $this->$property;
|
return $this->$property;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __set($property, $value) {
|
public function __set($property, $value)
|
||||||
|
{
|
||||||
if (property_exists($this, $property)) {
|
if (property_exists($this, $property)) {
|
||||||
$this->$property = $value;
|
$this->$property = $value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,15 +69,4 @@ class AccessToken
|
||||||
{
|
{
|
||||||
return (string) $this->accessToken;
|
return (string) $this->accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a boolean if the property is set
|
|
||||||
*
|
|
||||||
* @param string variable name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function __isset($key)
|
|
||||||
{
|
|
||||||
return isset($this->$key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace LeagueTest\OAuth2\Client\Grant;
|
||||||
|
|
||||||
|
use \Mockery as m;
|
||||||
|
|
||||||
|
class AuthorizationCodeTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $provider;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->provider = new \League\OAuth2\Client\Provider\Google(array(
|
||||||
|
'clientId' => 'mock_client_id',
|
||||||
|
'clientSecret' => 'mock_secret',
|
||||||
|
'redirectUri' => 'none',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
# m::close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetAccessToken()
|
||||||
|
{
|
||||||
|
$grant = new \League\OAuth2\Client\Grant\AuthorizationCode();
|
||||||
|
$this->assertEquals('authorization_code', (string) $grant);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException BadMethodCallException
|
||||||
|
*/
|
||||||
|
public function testInvalidRefreshToken()
|
||||||
|
{
|
||||||
|
$response = m::mock('Guzzle\Http\Message\Response');
|
||||||
|
$response->shouldReceive('getBody')->times(2)->andReturn('{"access_token": "mock_access_token", "expires": 3600, "refresh_token": "mock_refresh_token", "uid": 1}');
|
||||||
|
|
||||||
|
$client = m::mock('Guzzle\Service\Client');
|
||||||
|
$client->shouldReceive('setBaseUrl')->times(1);
|
||||||
|
$client->shouldReceive('post->send')->times(1)->andReturn($response);
|
||||||
|
$this->provider->setHttpClient($client);
|
||||||
|
|
||||||
|
$token = $this->provider->getAccessToken('authorization_code', array('invalid_code' => 'mock_authorization_code'));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace LeagueTest\OAuth2\Client\Provider;
|
namespace LeagueTest\OAuth2\Client\Grant;
|
||||||
|
|
||||||
use \Mockery as m;
|
use \Mockery as m;
|
||||||
|
|
||||||
|
|
|
@ -90,6 +90,7 @@ class EventbriteTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
$this->assertEquals(12345, $this->provider->getUserUid($token));
|
$this->assertEquals(12345, $this->provider->getUserUid($token));
|
||||||
$this->assertEquals('mock_email', $this->provider->getUserEmail($token));
|
$this->assertEquals('mock_email', $this->provider->getUserEmail($token));
|
||||||
|
$this->assertEquals(12345, $this->provider->getUserScreenName($token));
|
||||||
$this->assertEquals('mock_email', $user->email);
|
$this->assertEquals('mock_email', $user->email);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace LeagueTest\OAuth2\Client\Provider;
|
||||||
|
|
||||||
|
use \Mockery as m;
|
||||||
|
|
||||||
|
class IdentityProviderTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $provider;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->provider = new \League\OAuth2\Client\Provider\Google(array(
|
||||||
|
'clientId' => 'mock_client_id',
|
||||||
|
'clientSecret' => 'mock_secret',
|
||||||
|
'redirectUri' => 'none',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
# m::close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testInvalidGrantString()
|
||||||
|
{
|
||||||
|
$test = $this->provider->getAccessToken('invalid_grant', array('invalid_parameter' => 'none'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testInvalidGrantObject()
|
||||||
|
{
|
||||||
|
$grant = new \StdClass;
|
||||||
|
$test = $this->provider->getAccessToken($grant, array('invalid_parameter' => 'none'));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace LeagueTest\OAuth2\Client\Token;
|
||||||
|
|
||||||
|
class AccessTokenTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testInvalidRefreshToken()
|
||||||
|
{
|
||||||
|
$test = new \League\OAuth2\Client\Token\AccessToken(array('invalid_access_token' => 'none'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue