diff --git a/src/RedisAccessToken.php b/src/RedisAccessToken.php index 0828e0f..14cd6aa 100644 --- a/src/RedisAccessToken.php +++ b/src/RedisAccessToken.php @@ -12,7 +12,7 @@ class RedisAccessToken extends RedisAdapter implements AccessTokenInterface { /** * Get access token from Redis storage. - * + * * @param string $token * @return \League\OAuth2\Server\Entity\AccessTokenEntity|null */ @@ -22,44 +22,45 @@ public function get($token) return null; } - return (new AccessTokenEntity($this->getServer())) - ->setToken($access['id']) + return (new AccessTokenEntity($this->server)) + ->setId($access['id']) ->setExpireTime($access['expire_time']); } /** * Get access token from Redis storage by an associated refresh token. - * + * * @param \League\OAuth2\Server\Entity\RefreshTokenEntity $refreshToken * @return \League\OAuth2\Server\Entity\AccessTokenEntity|null */ public function getByRefreshToken(RefreshTokenEntity $refreshToken) { - if (! $refresh = $this->getValue($refreshToken->getToken(), 'oauth_refresh_tokens')) { + if (! $refresh = $this->getValue($refreshToken->getId(), 'oauth_refresh_tokens')) { return null; } - return $this->get($refresh['access_token']); + return $this->get($refresh['access_token_id']); } /** * Get associated access token scopes from Redis storage. - * - * @param \League\OAuth2\Server\Entity\AbstractTokenEntity $token + * + * @param \League\OAuth2\Server\Entity\AccessTokenEntity $token * @return array */ - public function getScopes(AbstractTokenEntity $token) + public function getScopes(AccessTokenEntity $token) { $scopes = []; - foreach ($this->getSet($token->getToken(), 'oauth_access_token_scopes') as $scope) { + foreach ($this->getSet($token->getId(), 'oauth_access_token_scopes') as $scope) { if (! $scope = $this->getValue($scope['id'], 'oauth_scopes')) { continue; } - $scopes[] = (new ScopeEntity($this->getServer())) - ->setId($scope['id']) - ->setDescription($scope['description']); + $scopes[] = (new ScopeEntity($this->server))->hydrate([ + 'id' => $scope['id'], + 'description' => $scope['description'] + ]); } return $scopes; @@ -67,11 +68,10 @@ public function getScopes(AbstractTokenEntity $token) /** * Creates a new access token in Redis storage. - * + * * @param string $token * @param int $expireTime * @param string|int $sessionId - * @return \League\OAuth2\Server\Entity\AccessTokenEntity */ public function create($token, $expireTime, $sessionId) { @@ -80,42 +80,38 @@ public function create($token, $expireTime, $sessionId) 'expire_time' => $expireTime, 'session_id' => $sessionId ]; - + $this->setValue($token, 'oauth_access_tokens', $payload); $this->pushSet(null, 'oauth_access_tokens', $token); - - return (new AccessTokenEntity($this->getServer())) - ->setToken($token) - ->setExpireTime($expireTime); } /** * Associate a scope with an access token in Redis storage. - * + * * @param \League\OAuth2\Server\Entity\AbstractTokenEntity $token * @param \League\OAuth2\Server\Entity\ScopeEntity $scope * @return void */ - public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope) + public function associateScope(AccessTokenEntity $token, ScopeEntity $scope) { - $this->pushSet($token->getToken(), 'oauth_access_token_scopes', ['id' => $scope->getId()]); + $this->pushSet($token->getId(), 'oauth_access_token_scopes', ['id' => $scope->getId()]); } /** * Delete an access token from Redis storage. - * + * * @param \League\OAuth2\Server\Entity\AbstractTokenEntity $token * @return void */ - public function delete(AbstractTokenEntity $token) + public function delete(AccessTokenEntity $token) { // Deletes the access token entry. - $this->deleteKey($token->getToken(), 'oauth_access_tokens'); + $this->deleteKey($token->getId(), 'oauth_access_tokens'); // Deletes the access token entry from the access tokens set. - $this->deleteSet(null, 'oauth_access_tokens', $token->getToken()); + $this->deleteSet(null, 'oauth_access_tokens', $token->getId()); // Deletes the access tokens associated scopes. - $this->deleteKey($token->getToken(), 'oauth_access_token_scopes'); + $this->deleteKey($token->getId(), 'oauth_access_token_scopes'); } } diff --git a/src/RedisAdapter.php b/src/RedisAdapter.php index 5cf5dc0..c8543e8 100644 --- a/src/RedisAdapter.php +++ b/src/RedisAdapter.php @@ -4,13 +4,13 @@ use Closure; use Predis\Client; -use League\OAuth2\Server\Storage\Adapter; +use League\OAuth2\Server\Storage\AbstractStorage; -class RedisAdapter extends Adapter +class RedisAdapter extends AbstractStorage { /** * Create a new redis adpater instance. - * + * * @param \Predis\Client $redis * @return void */ @@ -21,7 +21,7 @@ public function __construct(Client $redis) /** * Get a value from the Redis store. - * + * * @param string $key * @param string $table * @return mixed @@ -43,7 +43,7 @@ public function getValue($key, $table) /** * Set a value in the Redis store. - * + * * @param string $key * @param string $table * @param mixed $value @@ -60,7 +60,7 @@ public function setValue($key, $table, $value) /** * Push a value onto a set. - * + * * @param string $key * @param string $table * @param mixed $value @@ -81,7 +81,7 @@ public function pushSet($key, $table, $value) /** * Get a set from the Redis store. - * + * * @param string $key * @param string $table * @return array @@ -109,7 +109,7 @@ public function getSet($key, $table) /** * Delete a value from a set. - * + * * @param string $key * @param string $table * @param string $value @@ -128,7 +128,7 @@ public function deleteSet($key, $table, $value) /** * Delete a key from the Redis store. - * + * * @param string $key * @param string $table * @return int @@ -148,7 +148,7 @@ public function deleteKey($key, $table) * Get a matching set member by using a callback to run the * comparison. If the callback returns a non-null response * then that response is assumed to be a match. - * + * * @param string $key * @param string $table * @param \Closure $callback @@ -165,7 +165,7 @@ public function getMatchingMember($key, $table, Closure $callback) /** * Increment the value of a key by one. - * + * * @param string $table * @return int */ @@ -178,7 +178,7 @@ public function increment($table) /** * Prepare a value for storage in Redis. - * + * * @param mixed $value * @return string */ @@ -193,7 +193,7 @@ protected function prepareValue($value) /** * Prefix a key with its table. - * + * * @param string $key * @param string $table * @return string diff --git a/src/RedisAuthCode.php b/src/RedisAuthCode.php index 8adf7bf..6ece558 100644 --- a/src/RedisAuthCode.php +++ b/src/RedisAuthCode.php @@ -10,7 +10,7 @@ class RedisAuthCode extends RedisAdapter implements AuthCodeInterface { /** * Get an authorization code from Redis storage. - * + * * @param string $code * @return \League\OAuth2\Server\Entity\AuthCodeEntity|null */ @@ -20,14 +20,20 @@ public function get($code) return null; } - return (new AuthCodeEntity($this->getServer())) - ->setToken($code['id']) + if ($code['expire_time'] >= time()) { + return null; + } + + return (new AuthCodeEntity($this->server)) + ->setId($code['id']) + ->setRedirectUri($code['client_redirect_uri']) ->setExpireTime($code['expire_time']); + } /** * Get associated authorization code scopes from Redis storage. - * + * * @param \League\OAuth2\Server\Entity\AuthCodeEntity $code * @return array */ @@ -35,14 +41,15 @@ public function getScopes(AuthCodeEntity $code) { $scopes = []; - foreach ($this->getSet($code->getToken(), 'oauth_auth_code_scopes') as $scope) { + foreach ($this->getSet($code->getId(), 'oauth_auth_code_scopes') as $scope) { if (! $scope = $this->getValue($scope['id'], 'oauth_scopes')) { continue; } - $scopes[] = (new ScopeEntity($this->getServer())) - ->setId($scope['id']) - ->setDescription($scope['description']); + $scopes[] = (new ScopeEntity($this->server))->hydrate([ + 'id' => $scope['id'], + 'description' => $scope['description'] + ]); } return $scopes; @@ -50,55 +57,51 @@ public function getScopes(AuthCodeEntity $code) /** * Creates a new authorization code in Redis storage. - * + * * @param string $code * @param int $expireTime * @param string|int $sessionId - * @return \League\OAuth2\Server\Entity\AuthCodeEntity */ - public function create($code, $expireTime, $sessionId) + public function create($code, $expireTime, $sessionId, $redirectUri) { $payload = [ - 'id' => $code, - 'expire_time' => $expireTime, - 'session_id' => $sessionId + 'id' => $code, + 'expire_time' => $expireTime, + 'session_id' => $sessionId, + 'client_redirect_uri' => $redirectUri ]; - + $this->setValue($code, 'oauth_auth_codes', $payload); $this->pushSet(null, 'oauth_auth_codes', $code); - - return (new AuthCodeEntity($this->getServer())) - ->setToken($code) - ->setExpireTime($expireTime); } /** * Associate a scope with an authorization code in Redis storage. - * + * * @param \League\OAuth2\Server\Entity\AuthCodeEntity $code * @param \League\OAuth2\Server\Entity\ScopeEntity $scope * @return void */ public function associateScope(AuthCodeEntity $code, ScopeEntity $scope) { - $this->pushSet($code->getToken(), 'oauth_auth_code_scopes', ['id' => $scope->getId()]); + $this->pushSet($code->getId(), 'oauth_auth_code_scopes', ['id' => $scope->getId()]); } /** * Delete an authorization code from Redis storage. - * + * * @param \League\OAuth2\Server\Entity\AuthCodeEntity $code * @return void */ public function delete(AuthCodeEntity $code) { // Deletes the authorization code entry. - $this->deleteKey($code->getToken(), 'oauth_auth_codes'); + $this->deleteKey($code->getId(), 'oauth_auth_codes'); // Deletes the authorization code entry from the authorization codes set. - $this->deleteSet(null, 'oauth_auth_codes', $code->getToken()); + $this->deleteSet(null, 'oauth_auth_codes', $code->getId()); // Deletes the authorization codes associated scopes. - $this->deleteKey($code->getToken(), 'oauth_auth_code_scopes'); + $this->deleteKey($code->getId(), 'oauth_auth_code_scopes'); } } diff --git a/src/RedisClient.php b/src/RedisClient.php index 843212a..d6fdfac 100644 --- a/src/RedisClient.php +++ b/src/RedisClient.php @@ -10,14 +10,14 @@ class RedisClient extends RedisAdapter implements ClientInterface { /** * Indicates if clients are limited to specific grants. - * + * * @var bool */ protected $limitClientsToGrants = false; /** * Limits clients to specific grants. - * + * * @return \Lewis\OAuth2\Server\Storage\RedisClient */ public function limitClientsToGrants() @@ -29,7 +29,7 @@ public function limitClientsToGrants() /** * Get client from Redis storage. - * + * * @param string $clientId * @param string $clientSecret * @param string $redirectUri @@ -42,11 +42,6 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant return null; } - // Attempt to grab a redirection URI from the storage that matches the - // supplied redirection URI. If we can't find a match then we'll set - // this it as "null". - $client['redirect_uri'] = $this->getMatchingRedirectUri($clientId, $redirectUri); - // If a secret and redirection URI were given then we must correctly // validate the client by comparing its ID, secret, and that // the supplied redirection URI was registered. @@ -74,30 +69,17 @@ public function get($clientId, $clientSecret = null, $redirectUri = null, $grant return null; } - return (new ClientEntity($this->getServer())) - ->setId($client['id']) - ->setSecret($client['secret']) - ->setName($client['name']) - ->setRedirectUri($client['redirect_uri']); - } - - /** - * Get a matching redirect URI associated with a client. - * - * @param string $clientId - * @param string $redirectUri - * @return string|null - */ - protected function getMatchingRedirectUri($clientId, $redirectUri) - { - return $this->getMatchingMember($clientId, 'oauth_client_endpoints', function ($value) use ($redirectUri) { - return $value['redirect_uri'] == $redirectUri ? $value['redirect_uri'] : null; - }); + return (new ClientEntity($this->server))->hydrate([ + 'id' => $client['id'], + 'secret' => $client['secret'], + 'name' => $client['name'], + 'redirect_uri' => $client['redirect_uri'] + ]); } /** * Get client from Redis storage by an associated session. - * + * * @param \League\OAuth2\Server\Entity\SessionEntity $session * @return \League\OAuth2\Server\Entity\ClientEntity|null */ @@ -112,7 +94,7 @@ public function getBySession(SessionEntity $session) /** * Determines if the client is able to use the grant type specified. - * + * * @param string $clientId * @param string $grantType * @return bool diff --git a/src/RedisRefreshToken.php b/src/RedisRefreshToken.php index edc224c..7dd6cfb 100644 --- a/src/RedisRefreshToken.php +++ b/src/RedisRefreshToken.php @@ -9,7 +9,7 @@ class RedisRefreshToken extends RedisAdapter implements RefreshTokenInterface { /** * Get refresh token from Redis storage. - * + * * @param string $token * @return \League\OAuth2\Server\Entity\RefreshTokenEntity|null */ @@ -19,18 +19,18 @@ public function get($token) return null; } - return (new RefreshTokenEntity($this->getServer())) - ->setToken($refresh['id']) - ->setExpireTime($refresh['expire_time']); + return (new RefreshTokenEntity($this->server)) + ->setId($refresh['id']) + ->setExpireTime($refresh['expire_time']) + ->setAccessTokenId($refresh['access_token_id']); } /** * Creates a new refresh token in Redis storage. - * + * * @param string $token * @param int $expireTime * @param string $accessToken - * @return \League\OAuth2\Server\Entity\RefreshTokenEntity */ public function create($token, $expireTime, $accessToken) { @@ -39,27 +39,23 @@ public function create($token, $expireTime, $accessToken) 'expire_time' => $expireTime, 'access_token_id' => $accessToken ]; - + $this->setValue($token, 'oauth_refresh_tokens', $payload); $this->pushSet(null, 'oauth_refresh_tokens', $token); - - return (new RefreshTokenEntity($this->getServer())) - ->setToken($token) - ->setExpireTime($expireTime); } /** * Delete a refresh token from Redis storage. - * + * * @param \League\OAuth2\Server\Entity\RefreshTokenEntity $token * @return void */ public function delete(RefreshTokenEntity $token) { // Deletes the access token entry. - $this->deleteKey($token->getToken(), 'oauth_refresh_tokens'); + $this->deleteKey($token->getId(), 'oauth_refresh_tokens'); // Deletes the access token entry from the access tokens set. - $this->deleteSet(null, 'oauth_refresh_tokens', $token->getToken()); + $this->deleteSet(null, 'oauth_refresh_tokens', $token->getId()); } } diff --git a/src/RedisScope.php b/src/RedisScope.php index a9fa146..afd5ee3 100644 --- a/src/RedisScope.php +++ b/src/RedisScope.php @@ -14,14 +14,15 @@ class RedisScope extends RedisAdapter implements ScopeInterface * @param string $grantType * @return \League\OAuth2\Server\Entity\ScopeEntity|null */ - public function get($scope, $grantType = null) + public function get($scope, $grantType = null, $clientId = null) { if (! $scope = $this->getValue($scope, 'oauth_scopes')) { return null; } - return (new ScopeEntity($this->getServer())) - ->setId($scope['id']) - ->setDescription($scope['description']); + return (new ScopeEntity($this->server))->hydrate([ + 'id' => $scope['id'], + 'description' => $scope['description'] + ]); } } diff --git a/src/RedisSession.php b/src/RedisSession.php index f72f5ea..2fa9a13 100644 --- a/src/RedisSession.php +++ b/src/RedisSession.php @@ -12,7 +12,7 @@ class RedisSession extends RedisAdapter implements SessionInterface { /** * Get a session from Redis storage. - * + * * @param string $sessionId * @return \League\OAuth2\Server\Entity\SessionEntity|null */ @@ -22,20 +22,20 @@ public function get($sessionId) return null; } - return (new SessionEntity($this->getServer())) + return (new SessionEntity($this->server)) ->setId($session['id']) ->setOwner($session['owner_type'], $session['owner_id']); } /** * Get a session from Redis storage by an associated access token. - * + * * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken * @return \League\OAuth2\Server\Entity\SessionEntity|null */ public function getByAccessToken(AccessTokenEntity $accessToken) { - if (! $token = $this->getValue($accessToken->getToken(), 'oauth_access_tokens')) { + if (! $token = $this->getValue($accessToken->getId(), 'oauth_access_tokens')) { return null; } @@ -44,13 +44,13 @@ public function getByAccessToken(AccessTokenEntity $accessToken) /** * Get a session from Redis storage by an associated authorization code. - * + * * @param \League\OAuth2\Server\Entity\AuthCodeEntity $authCode * @return \League\OAuth2\Server\Entity\SessionEntity|null */ public function getByAuthCode(AuthCodeEntity $authCode) { - if (! $code = $this->getValue($authCode->getToken(), 'oauth_auth_codes')) { + if (! $code = $this->getValue($authCode->getId(), 'oauth_auth_codes')) { return null; } @@ -59,7 +59,7 @@ public function getByAuthCode(AuthCodeEntity $authCode) /** * Get associated session scopes from Redis storage. - * + * * @param \League\OAuth2\Server\Entity\SessionEntity $session * @return array */ @@ -72,9 +72,10 @@ public function getScopes(SessionEntity $session) continue; } - $scopes[] = (new ScopeEntity($this->getServer())) - ->setId($scope['id']) - ->setDescription($scope['description']); + $scopes[] = (new ScopeEntity($this->server))->hydrate([ + 'id' => $scope['id'], + 'description' => $scope['description'] + ]); } return $scopes; @@ -82,7 +83,7 @@ public function getScopes(SessionEntity $session) /** * Create a new session in Redis storage. - * + * * @param string $ownerType * @param string $ownerId * @param string $clientId @@ -97,8 +98,7 @@ public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = nul 'id' => $sessionId, 'client_id' => $clientId, 'owner_type' => $ownerType, - 'owner_id' => $ownerId, - 'redirect_uri' => $clientRedirectUri + 'owner_id' => $ownerId ]); return $sessionId; @@ -106,7 +106,7 @@ public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = nul /** * Associate a scope with a session in Redis storage. - * + * * @param \League\OAuth2\Server\Entity\SessionEntity $session * @param \League\OAuth2\Server\Entity\ScopeEntity $scope * @return void diff --git a/tests/RedisAccessTokenTest.php b/tests/RedisAccessTokenTest.php index c6aa8b5..97390f2 100644 --- a/tests/RedisAccessTokenTest.php +++ b/tests/RedisAccessTokenTest.php @@ -9,118 +9,118 @@ class RedisAccessTokenTest extends PHPUnit_Framework_TestCase { - public function tearDown() - { - m::close(); - } + public function tearDown() + { + m::close(); + } - public function setUp() - { - $this->redis = m::mock('Predis\Client'); - $this->server = m::mock('League\OAuth2\Server\AbstractServer'); - $this->storage = new RedisAccessToken($this->redis); - $this->storage->setServer($this->server); - } + public function setUp() + { + $this->redis = m::mock('Predis\Client'); + $this->server = m::mock('League\OAuth2\Server\AbstractServer'); + $this->storage = new RedisAccessToken($this->redis); + $this->storage->setServer($this->server); + } - public function testGetAccessTokenReturnsNullForInvalidAccessToken() - { - $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn(null); + public function testGetAccessTokenReturnsNullForInvalidAccessToken() + { + $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn(null); - $this->assertNull($this->storage->get('foo')); - } + $this->assertNull($this->storage->get('foo')); + } - public function testGetAccessTokenReturnsAccessTokenEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn('{"id":"foo","expire_time":1}'); + public function testGetAccessTokenReturnsAccessTokenEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn('{"id":"foo","expire_time":1}'); - $token = $this->storage->get('foo'); + $token = $this->storage->get('foo'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\AccessTokenEntity', $token); - $this->assertEquals('foo', $token->getToken()); - $this->assertEquals(1, $token->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\AccessTokenEntity', $token); + $this->assertEquals('foo', $token->getId()); + $this->assertEquals(1, $token->getExpireTime()); + } - public function testGetAccessTokenByRefreshTokenReturnsNullForInvalidRefreshToken() - { - $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn(null); + public function testGetAccessTokenByRefreshTokenReturnsNullForInvalidRefreshToken() + { + $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn(null); - $refresh = (new RefreshTokenEntity($this->server))->setToken('foo'); + $refresh = (new RefreshTokenEntity($this->server))->setId('foo'); - $this->assertNull($this->storage->getByRefreshToken($refresh)); - } + $this->assertNull($this->storage->getByRefreshToken($refresh)); + } - public function testGetAccessTokenByRefreshTokenReturnsAccessTokenEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn('{"access_token":"bar"}'); - $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:bar')->andReturn('{"id":"bar","expire_time":1}'); + public function testGetAccessTokenByRefreshTokenReturnsAccessTokenEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn('{"access_token_id":"bar"}'); + $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:bar')->andReturn('{"id":"bar","expire_time":1}'); - $refresh = (new RefreshTokenEntity($this->server))->setToken('foo'); - $access = $this->storage->getByRefreshToken($refresh); + $refresh = (new RefreshTokenEntity($this->server))->setId('foo'); + $access = $this->storage->getByRefreshToken($refresh); - $this->assertInstanceOf('League\OAuth2\Server\Entity\AccessTokenEntity', $access); - $this->assertEquals('bar', $access->getToken()); - $this->assertEquals(1, $access->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\AccessTokenEntity', $access); + $this->assertEquals('bar', $access->getId()); + $this->assertEquals(1, $access->getExpireTime()); + } - public function testGetAccessTokenScopes() - { - $this->redis->shouldReceive('smembers')->once()->with('oauth:access:token:scopes:foo')->andReturn([ - ['id' => 'foo'], - ['id' => 'bar'], - ['id' => 'baz'] - ]); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(['id' => 'foo', 'description' => 'foo']); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:bar')->andReturn(null); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:baz')->andReturn(['id' => 'baz', 'description' => 'baz']); + public function testGetAccessTokenScopes() + { + $this->redis->shouldReceive('smembers')->once()->with('oauth:access:token:scopes:foo')->andReturn([ + ['id' => 'foo'], + ['id' => 'bar'], + ['id' => 'baz'] + ]); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(['id' => 'foo', 'description' => 'foo']); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:bar')->andReturn(null); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:baz')->andReturn(['id' => 'baz', 'description' => 'baz']); - $scopes = $this->storage->getScopes((new AccessTokenEntity($this->server))->setToken('foo')); + $scopes = $this->storage->getScopes((new AccessTokenEntity($this->server))->setId('foo')); - $this->assertCount(2, $scopes); - $this->assertEquals('foo', $scopes[0]->getId()); - $this->assertEquals('baz', $scopes[1]->getId()); - } + $this->assertCount(2, $scopes); + $this->assertEquals('foo', $scopes[0]->getId()); + $this->assertEquals('baz', $scopes[1]->getId()); + } - public function testCreateNewAccessTokenEntity() - { - $this->redis->shouldReceive('set')->once()->with('oauth:access:tokens:foo', '{"id":"foo","expire_time":1,"session_id":1}'); - $this->redis->shouldReceive('sadd')->once()->with('oauth:access:tokens', 'foo'); + public function testCreateNewAccessTokenEntity() + { + $this->redis->shouldReceive('set')->once()->with('oauth:access:tokens:foo', '{"id":"foo","expire_time":1,"session_id":1}'); + $this->redis->shouldReceive('sadd')->once()->with('oauth:access:tokens', 'foo'); - $token = $this->storage->create('foo', 1, 1); + $token = $this->storage->create('foo', 1, 1); - $this->assertInstanceOf('League\OAuth2\Server\Entity\AccessTokenEntity', $token); - $this->assertEquals('foo', $token->getToken()); - $this->assertEquals(1, $token->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\AccessTokenEntity', $token); + $this->assertEquals('foo', $token->getId()); + $this->assertEquals(1, $token->getExpireTime()); + } - public function testAssociatingScopeWithAccessToken() - { - $token = (new AccessTokenEntity($this->server))->setToken('foo'); - $scope = (new ScopeEntity($this->server))->setId('bar'); + public function testAssociatingScopeWithAccessToken() + { + $token = (new AccessTokenEntity($this->server))->setId('foo'); + $scope = (new ScopeEntity($this->server))->hydrate(['id' => 'bar']); - $this->redis->shouldReceive('sadd')->once()->with('oauth:access:token:scopes:foo', '{"id":"bar"}'); + $this->redis->shouldReceive('sadd')->once()->with('oauth:access:token:scopes:foo', '{"id":"bar"}'); - $this->storage->associateScope($token, $scope); - } + $this->storage->associateScope($token, $scope); + } - public function testDeleteAccessTokenEntity() - { - $this->redis->shouldReceive('del')->once()->with('oauth:access:tokens:foo'); - $this->redis->shouldReceive('del')->once()->with('oauth:access:token:scopes:foo'); - $this->redis->shouldReceive('srem')->once()->with('oauth:access:tokens', 'foo'); + public function testDeleteAccessTokenEntity() + { + $this->redis->shouldReceive('del')->once()->with('oauth:access:tokens:foo'); + $this->redis->shouldReceive('del')->once()->with('oauth:access:token:scopes:foo'); + $this->redis->shouldReceive('srem')->once()->with('oauth:access:tokens', 'foo'); - $token = (new AccessTokenEntity($this->server))->setToken('foo'); + $token = (new AccessTokenEntity($this->server))->setId('foo'); - $this->storage->delete($token); - } + $this->storage->delete($token); + } } diff --git a/tests/RedisAuthCodeTest.php b/tests/RedisAuthCodeTest.php index 641f143..5328a61 100644 --- a/tests/RedisAuthCodeTest.php +++ b/tests/RedisAuthCodeTest.php @@ -8,94 +8,94 @@ class RedisAuthCodeTest extends PHPUnit_Framework_TestCase { - public function tearDown() - { - m::close(); - } + public function tearDown() + { + m::close(); + } - public function setUp() - { - $this->redis = m::mock('Predis\Client'); - $this->server = m::mock('League\OAuth2\Server\AbstractServer'); - $this->storage = new RedisAuthCode($this->redis); - $this->storage->setServer($this->server); - } + public function setUp() + { + $this->redis = m::mock('Predis\Client'); + $this->server = m::mock('League\OAuth2\Server\AbstractServer'); + $this->storage = new RedisAuthCode($this->redis); + $this->storage->setServer($this->server); + } - public function testGetAuthCodeReturnsNullForInvalidAuthCode() - { - $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn(null); + public function testGetAuthCodeReturnsNullForInvalidAuthCode() + { + $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn(null); - $this->assertNull($this->storage->get('foo')); - } + $this->assertNull($this->storage->get('foo')); + } - public function testGetAuthCodeReturnsAuthCodeEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn('{"id":"foo","expire_time":1}'); + public function testGetAuthCodeReturnsAuthCodeEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn('{"id":"foo","client_redirect_uri":"bar"}'); - $code = $this->storage->get('foo'); + $code = $this->storage->get('foo'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\AuthCodeEntity', $code); - $this->assertEquals('foo', $code->getToken()); - $this->assertEquals(1, $code->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\AuthCodeEntity', $code); + $this->assertEquals('foo', $code->getId()); + $this->assertEquals('bar', $code->getRedirectUri()); + } - public function testGetAuthCodeScopes() - { - $this->redis->shouldReceive('smembers')->once()->with('oauth:auth:code:scopes:foo')->andReturn([ - ['id' => 'foo'], - ['id' => 'bar'], - ['id' => 'baz'] - ]); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(['id' => 'foo', 'description' => 'foo']); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:bar')->andReturn(null); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:baz')->andReturn(['id' => 'baz', 'description' => 'baz']); + public function testGetAuthCodeScopes() + { + $this->redis->shouldReceive('smembers')->once()->with('oauth:auth:code:scopes:foo')->andReturn([ + ['id' => 'foo'], + ['id' => 'bar'], + ['id' => 'baz'] + ]); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(['id' => 'foo', 'description' => 'foo']); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:bar')->andReturn(null); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:baz')->andReturn(['id' => 'baz', 'description' => 'baz']); - $scopes = $this->storage->getScopes((new AuthCodeEntity($this->server))->setToken('foo')); + $scopes = $this->storage->getScopes((new AuthCodeEntity($this->server))->setId('foo')); - $this->assertCount(2, $scopes); - $this->assertEquals('foo', $scopes[0]->getId()); - $this->assertEquals('baz', $scopes[1]->getId()); - } + $this->assertCount(2, $scopes); + $this->assertEquals('foo', $scopes[0]->getId()); + $this->assertEquals('baz', $scopes[1]->getId()); + } - public function testCreateNewAuthCodeEntity() - { - $this->redis->shouldReceive('set')->once()->with('oauth:auth:codes:foo', '{"id":"foo","expire_time":1,"session_id":1}'); - $this->redis->shouldReceive('sadd')->once()->with('oauth:auth:codes', 'foo'); + public function testCreateNewAuthCodeEntity() + { + $this->redis->shouldReceive('set')->once()->with('oauth:auth:codes:foo', '{"id":"foo","client_redirect_uri":"bar","session_id":1}'); + $this->redis->shouldReceive('sadd')->once()->with('oauth:auth:codes', 'foo'); - $code = $this->storage->create('foo', 1, 1); + $code = $this->storage->create('foo', 1, 1, 'bar'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\AuthCodeEntity', $code); - $this->assertEquals('foo', $code->getToken()); - $this->assertEquals(1, $code->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\AuthCodeEntity', $code); + $this->assertEquals('foo', $code->getId()); + $this->assertEquals('bar', $code->getRedirectUri()); + } - public function testAssociatingScopeWithAuthCode() - { - $code = (new AuthCodeEntity($this->server))->setToken('foo'); - $scope = (new ScopeEntity($this->server))->setId('bar'); + public function testAssociatingScopeWithAuthCode() + { + $code = (new AuthCodeEntity($this->server))->setId('foo'); + $scope = (new ScopeEntity($this->server))->hydrate(['id' => 'bar']); - $this->redis->shouldReceive('sadd')->once()->with('oauth:auth:code:scopes:foo', '{"id":"bar"}'); + $this->redis->shouldReceive('sadd')->once()->with('oauth:auth:code:scopes:foo', '{"id":"bar"}'); - $this->storage->associateScope($code, $scope); - } + $this->storage->associateScope($code, $scope); + } - public function testDeleteAuthCodeEntity() - { - $this->redis->shouldReceive('del')->once()->with('oauth:auth:codes:foo'); - $this->redis->shouldReceive('del')->once()->with('oauth:auth:code:scopes:foo'); - $this->redis->shouldReceive('srem')->once()->with('oauth:auth:codes', 'foo'); + public function testDeleteAuthCodeEntity() + { + $this->redis->shouldReceive('del')->once()->with('oauth:auth:codes:foo'); + $this->redis->shouldReceive('del')->once()->with('oauth:auth:code:scopes:foo'); + $this->redis->shouldReceive('srem')->once()->with('oauth:auth:codes', 'foo'); - $code = (new AuthCodeEntity($this->server))->setToken('foo'); + $code = (new AuthCodeEntity($this->server))->setId('foo'); - $this->storage->delete($code); - } + $this->storage->delete($code); + } } diff --git a/tests/RedisClientTest.php b/tests/RedisClientTest.php index f2ca4db..b6f27a6 100644 --- a/tests/RedisClientTest.php +++ b/tests/RedisClientTest.php @@ -8,109 +8,109 @@ class RedisClientTest extends PHPUnit_Framework_TestCase { - public function tearDown() - { - m::close(); - } + public function tearDown() + { + m::close(); + } - public function setUp() - { - $this->redis = m::mock('Predis\Client'); - $this->server = m::mock('League\OAuth2\Server\AbstractServer'); - $this->storage = new RedisClient($this->redis); - $this->storage->setServer($this->server); - } + public function setUp() + { + $this->redis = m::mock('Predis\Client'); + $this->server = m::mock('League\OAuth2\Server\AbstractServer'); + $this->storage = new RedisClient($this->redis); + $this->storage->setServer($this->server); + } - public function testGetClientReturnsNullForInvalidClient() - { - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn(null); + public function testGetClientReturnsNullForInvalidClient() + { + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn(null); - $this->assertNull($this->storage->get('foo')); - } + $this->assertNull($this->storage->get('foo')); + } - public function testGetClientBySecretAndRedirectUriReturnsNullWhenEitherIsInvalid() - { - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([ - '{"redirect_uri":"baz"}', - '{"redirect_uri":"zap"}' - ]); - $this->assertNull($this->storage->get('foo', 'bar', 'foo')); - $this->assertNull($this->storage->get('foo', 'foo', 'baz')); - } + public function testGetClientBySecretAndRedirectUriReturnsNullWhenEitherIsInvalid() + { + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([ + '{"redirect_uri":"baz"}', + '{"redirect_uri":"zap"}' + ]); + $this->assertNull($this->storage->get('foo', 'bar', 'foo')); + $this->assertNull($this->storage->get('foo', 'foo', 'baz')); + } - public function testGetClientBySecretReturnsNullWhenSecretIsInvalid() - { - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([]); - $this->assertNull($this->storage->get('foo', 'baz', null)); - } + public function testGetClientBySecretReturnsNullWhenSecretIsInvalid() + { + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([]); + $this->assertNull($this->storage->get('foo', 'baz', null)); + } - public function testGetClientByRedirectionUriReturnsNullWhenRedirectionUriIsInvalid() - { - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([['redirect_uri' => 'foo']]); - $this->assertNull($this->storage->get('foo', null, 'bar')); - } + public function testGetClientByRedirectionUriReturnsNullWhenRedirectionUriIsInvalid() + { + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([['redirect_uri' => 'foo']]); + $this->assertNull($this->storage->get('foo', null, 'bar')); + } - public function testGetClientByIdReturnsNullIfClientIsNotAbleToUseGrant() - { - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([]); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:grants:foo')->andReturn([ - ['id' => 'foo'], - ['id' => 'bar'] - ]); + public function testGetClientByIdReturnsNullIfClientIsNotAbleToUseGrant() + { + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar"}'); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([]); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:grants:foo')->andReturn([ + ['id' => 'foo'], + ['id' => 'bar'] + ]); - $this->storage->limitClientsToGrants(); - $this->assertNull($this->storage->get('foo', null, null, 'zap')); - } + $this->storage->limitClientsToGrants(); + $this->assertNull($this->storage->get('foo', null, null, 'zap')); + } - public function testGetClientByIdReturnsClientEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar","name":"Foo"}'); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([['redirect_uri' => 'foo']]); + public function testGetClientByIdReturnsClientEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar","name":"Foo"}'); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([['redirect_uri' => 'foo']]); - $client = $this->storage->get('foo', null, 'foo'); + $client = $this->storage->get('foo', null, 'foo'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\ClientEntity', $client); - $this->assertEquals('foo', $client->getId()); - $this->assertEquals('Foo', $client->getName()); - $this->assertEquals('bar', $client->getSecret()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\ClientEntity', $client); + $this->assertEquals('foo', $client->getId()); + $this->assertEquals('Foo', $client->getName()); + $this->assertEquals('bar', $client->getSecret()); + } - public function testGetClientBySessionReturnsNullWhenSessionIsInvalid() - { - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); + public function testGetClientBySessionReturnsNullWhenSessionIsInvalid() + { + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); - $session = (new SessionEntity($this->server))->setId(1); + $session = (new SessionEntity($this->server))->setId(1); - $this->assertNull($this->storage->getBySession($session)); - } + $this->assertNull($this->storage->getBySession($session)); + } - public function testGetClientBySessionReturnsClientEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"client_id":"foo"}'); - $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar","name":"Foo"}'); - $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([]); + public function testGetClientBySessionReturnsClientEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"client_id":"foo"}'); + $this->redis->shouldReceive('get')->once()->with('oauth:clients:foo')->andReturn('{"id":"foo","secret":"bar","name":"Foo"}'); + $this->redis->shouldReceive('smembers')->once()->with('oauth:client:endpoints:foo')->andReturn([]); - $session = (new SessionEntity($this->server))->setId(1); - $client = $this->storage->getBySession($session); + $session = (new SessionEntity($this->server))->setId(1); + $client = $this->storage->getBySession($session); - $this->assertInstanceOf('League\OAuth2\Server\Entity\ClientEntity', $client); - $this->assertEquals('foo', $client->getId()); - $this->assertEquals('Foo', $client->getName()); - $this->assertEquals('bar', $client->getSecret()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\ClientEntity', $client); + $this->assertEquals('foo', $client->getId()); + $this->assertEquals('Foo', $client->getName()); + $this->assertEquals('bar', $client->getSecret()); + } } diff --git a/tests/RedisRefreshTokenTest.php b/tests/RedisRefreshTokenTest.php index ac0053f..c141d73 100644 --- a/tests/RedisRefreshTokenTest.php +++ b/tests/RedisRefreshTokenTest.php @@ -7,63 +7,63 @@ class RedisRefreshTokenTest extends PHPUnit_Framework_TestCase { - public function tearDown() - { - m::close(); - } + public function tearDown() + { + m::close(); + } - public function setUp() - { - $this->redis = m::mock('Predis\Client'); - $this->server = m::mock('League\OAuth2\Server\AbstractServer'); - $this->storage = new RedisRefreshToken($this->redis); - $this->storage->setServer($this->server); - } + public function setUp() + { + $this->redis = m::mock('Predis\Client'); + $this->server = m::mock('League\OAuth2\Server\AbstractServer'); + $this->storage = new RedisRefreshToken($this->redis); + $this->storage->setServer($this->server); + } - public function testGetRefreshTokenReturnsNullForInvalidRefreshToken() - { - $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn(null); + public function testGetRefreshTokenReturnsNullForInvalidRefreshToken() + { + $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn(null); - $this->assertNull($this->storage->get('foo')); - } + $this->assertNull($this->storage->get('foo')); + } - public function testGetRefreshTokenReturnsRefreshTokenEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn('{"id":"foo","expire_time":1}'); + public function testGetRefreshTokenReturnsRefreshTokenEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:refresh:tokens:foo')->andReturn('{"id":"foo","expire_time":1,"access_token_id":"bar"}'); - $token = $this->storage->get('foo'); + $token = $this->storage->get('foo'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\RefreshTokenEntity', $token); - $this->assertEquals('foo', $token->getToken()); - $this->assertEquals(1, $token->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\RefreshTokenEntity', $token); + $this->assertEquals('foo', $token->getId()); + $this->assertEquals(1, $token->getExpireTime()); + } - public function testCreateNewRefreshTokenEntity() - { - $this->redis->shouldReceive('set')->once()->with('oauth:refresh:tokens:foo', '{"id":"foo","expire_time":1,"access_token_id":"bar"}'); - $this->redis->shouldReceive('sadd')->once()->with('oauth:refresh:tokens', 'foo'); + public function testCreateNewRefreshTokenEntity() + { + $this->redis->shouldReceive('set')->once()->with('oauth:refresh:tokens:foo', '{"id":"foo","expire_time":1,"access_token_id":"bar"}'); + $this->redis->shouldReceive('sadd')->once()->with('oauth:refresh:tokens', 'foo'); - $token = $this->storage->create('foo', 1, 'bar'); + $token = $this->storage->create('foo', 1, 'bar'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\RefreshTokenEntity', $token); - $this->assertEquals('foo', $token->getToken()); - $this->assertEquals(1, $token->getExpireTime()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\RefreshTokenEntity', $token); + $this->assertEquals('foo', $token->getId()); + $this->assertEquals(1, $token->getExpireTime()); + } - public function testDeleteRefreshTokenEntity() - { - $this->redis->shouldReceive('del')->once()->with('oauth:refresh:tokens:foo'); - $this->redis->shouldReceive('srem')->once()->with('oauth:refresh:tokens', 'foo'); + public function testDeleteRefreshTokenEntity() + { + $this->redis->shouldReceive('del')->once()->with('oauth:refresh:tokens:foo'); + $this->redis->shouldReceive('srem')->once()->with('oauth:refresh:tokens', 'foo'); - $token = (new RefreshTokenEntity($this->server))->setToken('foo'); + $token = (new RefreshTokenEntity($this->server))->setId('foo'); - $this->storage->delete($token); - } + $this->storage->delete($token); + } } diff --git a/tests/RedisScopeTest.php b/tests/RedisScopeTest.php index 2d86863..7b43835 100644 --- a/tests/RedisScopeTest.php +++ b/tests/RedisScopeTest.php @@ -6,39 +6,39 @@ class RedisScopeTest extends PHPUnit_Framework_TestCase { - public function tearDown() - { - m::close(); - } + public function tearDown() + { + m::close(); + } - public function setUp() - { - $this->redis = m::mock('Predis\Client'); - $this->server = m::mock('League\OAuth2\Server\AbstractServer'); - $this->storage = new RedisScope($this->redis); - $this->storage->setServer($this->server); - } + public function setUp() + { + $this->redis = m::mock('Predis\Client'); + $this->server = m::mock('League\OAuth2\Server\AbstractServer'); + $this->storage = new RedisScope($this->redis); + $this->storage->setServer($this->server); + } - public function testGetScopeReturnsNullForInvalidScope() - { - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(null); + public function testGetScopeReturnsNullForInvalidScope() + { + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(null); - $this->assertNull($this->storage->get('foo')); - } + $this->assertNull($this->storage->get('foo')); + } - public function testGetScopeReturnsScopeEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn('{"id":"foo","description":"foo"}'); + public function testGetScopeReturnsScopeEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn('{"id":"foo","description":"foo"}'); - $scope = $this->storage->get('foo'); + $scope = $this->storage->get('foo'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\ScopeEntity', $scope); - $this->assertEquals('foo', $scope->getId()); - $this->assertEquals('foo', $scope->getDescription()); - } + $this->assertInstanceOf('League\OAuth2\Server\Entity\ScopeEntity', $scope); + $this->assertEquals('foo', $scope->getId()); + $this->assertEquals('foo', $scope->getDescription()); + } } diff --git a/tests/RedisSessionTest.php b/tests/RedisSessionTest.php index abcb0a0..1963db3 100644 --- a/tests/RedisSessionTest.php +++ b/tests/RedisSessionTest.php @@ -10,145 +10,145 @@ class RedisSessionTest extends PHPUnit_Framework_TestCase { - public function tearDown() - { - m::close(); - } + public function tearDown() + { + m::close(); + } - public function setUp() - { - $this->redis = m::mock('Predis\Client'); - $this->server = m::mock('League\OAuth2\Server\AbstractServer'); - $this->storage = new RedisSession($this->redis); - $this->storage->setServer($this->server); - } + public function setUp() + { + $this->redis = m::mock('Predis\Client'); + $this->server = m::mock('League\OAuth2\Server\AbstractServer'); + $this->storage = new RedisSession($this->redis); + $this->storage->setServer($this->server); + } - public function testGetSessionByIdReturnsNullForInvalidSession() - { - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); + public function testGetSessionByIdReturnsNullForInvalidSession() + { + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); - $this->assertNull($this->storage->get(1)); - } + $this->assertNull($this->storage->get(1)); + } - public function testGetSessionByIdReturnsSessionEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"id":1,"owner_type":"user","owner_id":1}'); - - $session = $this->storage->get(1); + public function testGetSessionByIdReturnsSessionEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"id":1,"owner_type":"user","owner_id":1}'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\SessionEntity', $session); - $this->assertEquals(1, $session->getId()); - $this->assertEquals(1, $session->getOwnerId()); - $this->assertEquals('user', $session->getOwnerType()); - } + $session = $this->storage->get(1); + $this->assertInstanceOf('League\OAuth2\Server\Entity\SessionEntity', $session); + $this->assertEquals(1, $session->getId()); + $this->assertEquals(1, $session->getOwnerId()); + $this->assertEquals('user', $session->getOwnerType()); + } - public function testGetSessionByAccessTokenReturnsNullForInvalidAccessToken() - { - $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn(null); - $token = (new AccessTokenEntity($this->server))->setToken('foo'); + public function testGetSessionByAccessTokenReturnsNullForInvalidAccessToken() + { + $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn(null); - $this->assertNull($this->storage->getByAccessToken($token)); - } + $token = (new AccessTokenEntity($this->server))->setId('foo'); + $this->assertNull($this->storage->getByAccessToken($token)); + } - public function testGetSessionByAccessTokenReturnsNullForInvalidAccessTokenSession() - { - $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn('{"id":"foo","session_id":1}'); - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); - $token = (new AccessTokenEntity($this->server))->setToken('foo'); + public function testGetSessionByAccessTokenReturnsNullForInvalidAccessTokenSession() + { + $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn('{"id":"foo","session_id":1}'); + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); - $this->assertNull($this->storage->getByAccessToken($token)); - } + $token = (new AccessTokenEntity($this->server))->setId('foo'); + $this->assertNull($this->storage->getByAccessToken($token)); + } - public function testGetSessionByAccessTokenReturnsSessionEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn('{"id":"foo","session_id":1}'); - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"id":1,"owner_type":"user","owner_id":1}'); - $token = (new AccessTokenEntity($this->server))->setToken('foo'); - $session = $this->storage->getByAccessToken($token); + public function testGetSessionByAccessTokenReturnsSessionEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:access:tokens:foo')->andReturn('{"id":"foo","session_id":1}'); + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"id":1,"owner_type":"user","owner_id":1}'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\SessionEntity', $session); - } + $token = (new AccessTokenEntity($this->server))->setId('foo'); + $session = $this->storage->getByAccessToken($token); + $this->assertInstanceOf('League\OAuth2\Server\Entity\SessionEntity', $session); + } - public function testGetSessionByAuthCodeReturnsNullForInvalidAuthCode() - { - $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn(null); - $code = (new AuthCodeEntity($this->server))->setToken('foo'); + public function testGetSessionByAuthCodeReturnsNullForInvalidAuthCode() + { + $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn(null); - $this->assertNull($this->storage->getByAuthCode($code)); - } + $code = (new AuthCodeEntity($this->server))->setId('foo'); + $this->assertNull($this->storage->getByAuthCode($code)); + } - public function testGetSessionByAuthCodeReturnsNullForInvalidAuthCodeSession() - { - $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn('{"id":"foo","session_id":1}'); - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); - $code = (new AuthCodeEntity($this->server))->setToken('foo'); + public function testGetSessionByAuthCodeReturnsNullForInvalidAuthCodeSession() + { + $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn('{"id":"foo","session_id":1}'); + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn(null); - $this->assertNull($this->storage->getByAuthCode($code)); - } + $code = (new AuthCodeEntity($this->server))->setId('foo'); + $this->assertNull($this->storage->getByAuthCode($code)); + } - public function testGetSessionByAuthCodeReturnsSessionEntity() - { - $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn('{"id":"foo","session_id":1}'); - $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"id":1,"owner_type":"user","owner_id":1}'); - $code = (new AuthCodeEntity($this->server))->setToken('foo'); - $session = $this->storage->getByAuthCode($code); + public function testGetSessionByAuthCodeReturnsSessionEntity() + { + $this->redis->shouldReceive('get')->once()->with('oauth:auth:codes:foo')->andReturn('{"id":"foo","session_id":1}'); + $this->redis->shouldReceive('get')->once()->with('oauth:sessions:1')->andReturn('{"id":1,"owner_type":"user","owner_id":1}'); - $this->assertInstanceOf('League\OAuth2\Server\Entity\SessionEntity', $session); - } + $code = (new AuthCodeEntity($this->server))->setId('foo'); + $session = $this->storage->getByAuthCode($code); + $this->assertInstanceOf('League\OAuth2\Server\Entity\SessionEntity', $session); + } - public function testGetSessionScopes() - { - $this->redis->shouldReceive('smembers')->once()->with('oauth:session:scopes:1')->andReturn([ - ['id' => 'foo'], - ['id' => 'bar'], - ['id' => 'baz'] - ]); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(['id' => 'foo', 'description' => 'foo']); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:bar')->andReturn(null); - $this->redis->shouldReceive('get')->once()->with('oauth:scopes:baz')->andReturn(['id' => 'baz', 'description' => 'baz']); - $scopes = $this->storage->getScopes((new SessionEntity($this->server))->setId(1)); + public function testGetSessionScopes() + { + $this->redis->shouldReceive('smembers')->once()->with('oauth:session:scopes:1')->andReturn([ + ['id' => 'foo'], + ['id' => 'bar'], + ['id' => 'baz'] + ]); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:foo')->andReturn(['id' => 'foo', 'description' => 'foo']); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:bar')->andReturn(null); + $this->redis->shouldReceive('get')->once()->with('oauth:scopes:baz')->andReturn(['id' => 'baz', 'description' => 'baz']); - $this->assertCount(2, $scopes); - $this->assertEquals('foo', $scopes[0]->getId()); - $this->assertEquals('baz', $scopes[1]->getId()); - } + $scopes = $this->storage->getScopes((new SessionEntity($this->server))->setId(1)); + $this->assertCount(2, $scopes); + $this->assertEquals('foo', $scopes[0]->getId()); + $this->assertEquals('baz', $scopes[1]->getId()); + } - public function testCreateNewSessionEntity() - { - $this->redis->shouldReceive('incr')->once()->with('oauth:session:ids')->andReturn(1); - $this->redis->shouldReceive('set')->once()->with('oauth:sessions:1', '{"id":1,"client_id":1,"owner_type":"user","owner_id":1,"redirect_uri":"foo"}'); - $this->assertEquals(1, $this->storage->create('user', 1, 1, 'foo')); - } + public function testCreateNewSessionEntity() + { + $this->redis->shouldReceive('incr')->once()->with('oauth:session:ids')->andReturn(1); + $this->redis->shouldReceive('set')->once()->with('oauth:sessions:1', '{"id":1,"client_id":1,"owner_type":"user","owner_id":1,"redirect_uri":"foo"}'); + $this->assertEquals(1, $this->storage->create(1, 1, 'user', 1)); + } - public function testAssociatingScopeWithSession() - { - $session = (new SessionEntity($this->server))->setId(1); - $scope = (new ScopeEntity($this->server))->setId('foo'); - $this->redis->shouldReceive('sadd')->once()->with('oauth:session:scopes:1', '{"id":"foo"}'); + public function testAssociatingScopeWithSession() + { + $session = (new SessionEntity($this->server))->setId(1); + $scope = (new ScopeEntity($this->server))->hydrate(['id' => 'foo']); - $this->storage->associateScope($session, $scope); - } + $this->redis->shouldReceive('sadd')->once()->with('oauth:session:scopes:1', '{"id":"foo"}'); + + $this->storage->associateScope($session, $scope); + } }