diff --git a/src/Token/AccessToken.php b/src/Token/AccessToken.php index 1a5eb9c2..19052c96 100755 --- a/src/Token/AccessToken.php +++ b/src/Token/AccessToken.php @@ -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']; @@ -65,6 +59,46 @@ 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. * @@ -72,6 +106,6 @@ public function __construct(array $options = null) */ public function __toString() { - return (string) $this->accessToken; + return (string) $this->getToken(); } } diff --git a/test/src/Grant/RefreshTokenTest.php b/test/src/Grant/RefreshTokenTest.php index 7576b1c9..99fd928c 100644 --- a/test/src/Grant/RefreshTokenTest.php +++ b/test/src/Grant/RefreshTokenTest.php @@ -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 @@ -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', @@ -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); } @@ -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()]); } } diff --git a/test/src/Provider/AbstractProviderTest.php b/test/src/Provider/AbstractProviderTest.php index d179fbdd..dd7e4850 100644 --- a/test/src/Provider/AbstractProviderTest.php +++ b/test/src/Provider/AbstractProviderTest.php @@ -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() diff --git a/test/src/Token/AccessTokenTest.php b/test/src/Token/AccessTokenTest.php index 2c4c5d43..8548c64a 100644 --- a/test/src/Token/AccessTokenTest.php +++ b/test/src/Token/AccessTokenTest.php @@ -2,6 +2,8 @@ namespace League\OAuth2\Client\Test\Token; +use League\OAuth2\Client\Token\AccessToken; + class AccessTokenTest extends \PHPUnit_Framework_TestCase { /** @@ -9,13 +11,18 @@ class AccessTokenTest extends \PHPUnit_Framework_TestCase */ 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); } }