oauth2-server/tests/unit/Entity/SessionEntityTest.php

155 lines
6.1 KiB
PHP
Raw Normal View History

2014-01-16 22:21:06 +05:30
<?php
2014-05-02 19:42:15 +05:30
namespace LeagueTests\Entity;
2014-01-16 22:21:06 +05:30
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\AuthorizationServer;
2014-05-02 19:42:15 +05:30
use League\OAuth2\Server\Entity\AccessTokenEntity;
use League\OAuth2\Server\Entity\ClientEntity;
use League\OAuth2\Server\Entity\RefreshTokenEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Entity\SessionEntity;
use Mockery as M;
2014-01-16 22:21:06 +05:30
2014-11-08 23:56:12 +05:30
class SessionEntityTest extends \PHPUnit_Framework_TestCase
2014-01-16 22:21:06 +05:30
{
public function testSetGet()
{
2014-07-11 22:49:10 +05:30
$emitter = M::mock('League\Event\Emitter');
$emitter->shouldReceive('emit');
2014-01-16 22:21:06 +05:30
$server = M::mock('League\OAuth2\Server\AbstractServer');
2014-07-11 22:49:10 +05:30
$server->shouldReceive('setEventEmitter');
$server->shouldReceive('getEventEmitter')->andReturn($emitter);
$server->setEventEmitter($emitter);
2014-05-02 21:51:53 +05:30
$entity = new SessionEntity($server);
2014-01-16 22:21:06 +05:30
$entity->setId('foobar');
$entity->setOwner('user', 123);
2014-05-02 21:51:53 +05:30
$entity->associateAccessToken((new AccessTokenEntity($server)));
$entity->associateRefreshToken((new RefreshTokenEntity($server)));
$entity->associateClient((new ClientEntity($server)));
2014-07-11 22:49:10 +05:30
$entity->associateScope(
(new ScopeEntity($server))->hydrate(['id' => 'foo'])
);
2014-01-16 22:21:06 +05:30
// $entity->associateAuthCode((new AuthCode($server)));
$this->assertEquals('foobar', $entity->getId());
$this->assertEquals('user', $entity->getOwnerType());
$this->assertEquals(123, $entity->getOwnerId());
2014-05-02 21:51:53 +05:30
$this->assertTrue($entity->getClient() instanceof ClientEntity);
2014-01-16 22:21:06 +05:30
$this->assertTrue($entity->hasScope('foo'));
2014-01-16 22:57:05 +05:30
$reflector = new \ReflectionClass($entity);
$accessTokenProperty = $reflector->getProperty('accessToken');
$accessTokenProperty->setAccessible(true);
$refreshTokenProperty = $reflector->getProperty('refreshToken');
$refreshTokenProperty->setAccessible(true);
2014-05-02 21:51:53 +05:30
$this->assertTrue($accessTokenProperty->getValue($entity) instanceof AccessTokenEntity);
$this->assertTrue($refreshTokenProperty->getValue($entity) instanceof RefreshTokenEntity);
2014-01-16 22:21:06 +05:30
// $this->assertTrue($reader($entity, 'authCode') instanceof AuthCode);
}
public function testFormatScopes()
{
$server = M::mock('League\OAuth2\Server\AbstractServer');
2014-05-02 21:51:53 +05:30
$entity = new SessionEntity($server);
$reflectedEntity = new \ReflectionClass('League\OAuth2\Server\Entity\SessionEntity');
2014-01-16 22:21:06 +05:30
$method = $reflectedEntity->getMethod('formatScopes');
$method->setAccessible(true);
$scopes = [
2014-07-11 22:49:10 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'scope1']),
2014-11-08 23:56:12 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'scope2']),
2014-01-16 22:21:06 +05:30
];
$result = $method->invokeArgs($entity, [$scopes]);
$this->assertTrue(isset($result['scope1']));
$this->assertTrue(isset($result['scope2']));
2014-05-02 21:51:53 +05:30
$this->assertTrue($result['scope1'] instanceof ScopeEntity);
$this->assertTrue($result['scope2'] instanceof ScopeEntity);
2014-01-16 22:21:06 +05:30
}
public function testGetScopes()
{
2014-05-02 21:51:53 +05:30
$server = M::mock('League\OAuth2\Server\AuthorizationServer');
$server->shouldReceive('setAccessTokenStorage');
$server->shouldReceive('setSessionStorage');
2014-01-16 22:21:06 +05:30
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
$accessTokenStorage->shouldReceive('setServer');
$server->setAccessTokenStorage($accessTokenStorage);
$server->shouldReceive('getAccessTokenStorage')->andReturn($accessTokenStorage);
2014-05-02 21:51:53 +05:30
2014-01-16 22:21:06 +05:30
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
$sessionStorage->shouldReceive('getScopes')->andReturn(
[]
);
$sessionStorage->shouldReceive('setServer');
$server->setSessionStorage($sessionStorage);
$server->shouldReceive('getSessionStorage')->andReturn($sessionStorage);
2014-05-02 21:51:53 +05:30
$entity = new SessionEntity($server);
2014-01-16 22:21:06 +05:30
$this->assertEquals($entity->getScopes(), []);
}
public function testHasScopes()
{
2014-05-02 21:51:53 +05:30
$server = M::mock('League\OAuth2\Server\AuthorizationServer');
$server->shouldReceive('setAccessTokenStorage');
$server->shouldReceive('setSessionStorage');
2014-01-16 22:21:06 +05:30
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
$accessTokenStorage->shouldReceive('setServer');
$server->setAccessTokenStorage($accessTokenStorage);
$server->shouldReceive('getAccessTokenStorage')->andReturn($accessTokenStorage);
2014-05-02 21:51:53 +05:30
2014-01-16 22:21:06 +05:30
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
$sessionStorage->shouldReceive('getScopes')->andReturn(
[]
);
$sessionStorage->shouldReceive('setServer');
$server->setSessionStorage($sessionStorage);
$server->shouldReceive('getSessionStorage')->andReturn($sessionStorage);
2014-05-02 21:51:53 +05:30
$entity = new SessionEntity($server);
2014-01-16 22:21:06 +05:30
$this->assertFalse($entity->hasScope('foo'));
}
2014-05-03 15:25:25 +05:30
public function testSave()
2014-01-16 22:21:06 +05:30
{
2014-05-02 21:51:53 +05:30
$server = M::mock('League\OAuth2\Server\AuthorizationServer');
$server->shouldReceive('setSessionStorage');
$server->shouldReceive('setClientStorage');
2014-01-16 22:21:06 +05:30
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
$sessionStorage->shouldReceive('create');
$sessionStorage->shouldReceive('associateScope');
$sessionStorage->shouldReceive('setServer');
$sessionStorage->shouldReceive('getScopes')->andReturn([
2014-11-08 23:56:12 +05:30
(new ScopeEntity($server))->hydrate(['id' => 'foo']),
2014-01-16 22:21:06 +05:30
]);
$server->shouldReceive('getSessionStorage')->andReturn($sessionStorage);
2014-05-02 21:51:53 +05:30
2014-01-16 22:21:06 +05:30
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
$clientStorage->shouldReceive('getBySession')->andReturn(
2014-07-11 22:49:10 +05:30
(new ClientEntity($server))->hydrate(['id' => 'foo'])
2014-01-16 22:21:06 +05:30
);
$clientStorage->shouldReceive('setServer');
$server->shouldReceive('getClientStorage')->andReturn($clientStorage);
2014-05-02 21:51:53 +05:30
2014-01-16 22:21:06 +05:30
$server->setSessionStorage($sessionStorage);
$server->setClientStorage($clientStorage);
2014-05-02 21:51:53 +05:30
$entity = new SessionEntity($server);
2014-01-16 22:21:06 +05:30
$this->assertEquals(null, $entity->save());
}
2014-05-03 15:25:25 +05:30
}