Skip to content

Commit

Permalink
Make all properties of AccessToken protected
Browse files Browse the repository at this point in the history
- add getter methods
- update tests

Refs #294
  • Loading branch information
shadowhand committed May 12, 2015
1 parent 9ba7b83 commit e345d2c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
70 changes: 52 additions & 18 deletions src/Token/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,32 @@
class AccessToken
{
/**
* @var string accessToken
* @var string
*/
public $accessToken;
protected $accessToken;

/**
* @var int expires
* @var int
*/
public $expires;
protected $expires;

/**
* @var string refreshToken
* @var string
*/
public $refreshToken;
protected $refreshToken;

/**
* @var string uid
* @var string
*/
public $uid;
protected $uid;

/**
* Sets the token, expiry, etc values.
*
* @param array $options token options
* @return void
* @param array $options
*/
public function __construct(array $options = null)
public function __construct(array $options = [])
{
if (empty($options['access_token'])) {
throw new InvalidArgumentException(sprintf(
'Required option not passed: "%s"',
'access_token'
));
throw new InvalidArgumentException('Required option not passed: "access_token"');
}

$this->accessToken = $options['access_token'];
Expand All @@ -65,13 +59,53 @@ public function __construct(array $options = null)
}
}

/**
* Get the access token.
*
* @return string
*/
public function getToken()
{
return $this->accessToken;
}

/**
* Get the refresh token, if defined.
*
* @return string|null
*/
public function getRefreshToken()
{
return $this->refreshToken;
}

/**
* Get the expiration timestamp, if defined.
*
* @return integer|null
*/
public function getExpires()
{
return $this->expires;
}

/**
* Get the user identifier, if defined.
*
* @return string|null
*/
public function getUid()
{
return $this->uid;
}

/**
* Returns the token key.
*
* @return string
*/
public function __toString()
{
return (string) $this->accessToken;
return (string) $this->getToken();
}
}
12 changes: 7 additions & 5 deletions test/src/Grant/RefreshTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace League\OAuth2\Client\Test\Grant;

use League\OAuth2\Client\Grant\RefreshToken;
use League\OAuth2\Client\Test\Provider\Fake as MockProvider;
use Mockery as m;

class RefreshTokenTest extends \PHPUnit_Framework_TestCase
Expand All @@ -11,7 +13,7 @@ class RefreshTokenTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->provider = new \League\OAuth2\Client\Test\Provider\Fake([
$this->provider = new MockProvider([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
Expand All @@ -37,10 +39,10 @@ public function testGetAccessToken()
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $token);

$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$grant = new RefreshToken();
$this->assertEquals('refresh_token', (string) $grant);

$newToken = $this->provider->getAccessToken($grant, ['refresh_token' => $token->refreshToken]);
$newToken = $this->provider->getAccessToken($grant, ['refresh_token' => $token->getRefreshToken()]);
$this->assertInstanceOf('League\OAuth2\Client\Token\AccessToken', $newToken);
}

Expand All @@ -59,7 +61,7 @@ public function testInvalidRefreshToken()

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

$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$refreshToken = $this->provider->getAccessToken($grant, ['invalid_refresh_token' => $token->refreshToken]);
$grant = new RefreshToken();
$refreshToken = $this->provider->getAccessToken($grant, ['invalid_refresh_token' => $token->getRefreshToken()]);
}
}
6 changes: 3 additions & 3 deletions test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ public function testGetAccessToken()
$result = $provider->getAccessToken($grant, ['code' => 'mock_authorization_code']);

$this->assertSame($result, $token);
$this->assertSame($raw_response['uid'], $token->uid);
$this->assertSame($raw_response['access_token'], $token->accessToken);
$this->assertSame($raw_response['expires'], $token->expires);
$this->assertSame($raw_response['uid'], $token->getUid());
$this->assertSame($raw_response['access_token'], $token->getToken());
$this->assertSame($raw_response['expires'], $token->getExpires());
}

public function testErrorResponsesCanBeCustomizedAtTheProvider()
Expand Down
15 changes: 11 additions & 4 deletions test/src/Token/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

namespace League\OAuth2\Client\Test\Token;

use League\OAuth2\Client\Token\AccessToken;

class AccessTokenTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidRefreshToken()
{
new \League\OAuth2\Client\Token\AccessToken(['invalid_access_token' => 'none']);
$token = new AccessToken(['invalid_access_token' => 'none']);
}

public function testExpiresInCorrection()
{
$options = array('access_token' => 'access_token', 'expires_in' => 100);
$token = new \League\OAuth2\Client\Token\AccessToken($options);
$this->assertNotNull($token->expires);
$options = ['access_token' => 'access_token', 'expires_in' => 100];
$token = new AccessToken($options);

$expires = $token->getExpires();

$this->assertNotNull($expires);
$this->assertGreaterThan(time(), $expires);
$this->assertLessThan(time() + 200, $expires);
}
}

0 comments on commit e345d2c

Please sign in to comment.