PSR fixes

This commit is contained in:
Alex Bilbie 2012-12-23 21:36:30 +00:00
parent 0f30b2a803
commit df85c98e53

View File

@ -1,398 +1,401 @@
<?php <?php
class Authentication_Server_test extends PHPUnit_Framework_TestCase { class Authentication_Server_test extends PHPUnit_Framework_TestCase
{
public function setUp()
{
require 'src/OAuth2/Authentication/Server.php';
require 'src/OAuth2/Authentication/Database.php';
function setUp() $this->oauth = new Oauth2\Authentication\Server();
{
$this->oauth = new Oauth2\Authentication\Server();
require_once('database_mock.php'); require_once 'database_mock.php';
$this->oauthdb = new OAuthdb(); $this->oauthdb = new OAuthdb();
$this->assertInstanceOf('Oauth2\Authentication\Database', $this->oauthdb); $this->assertInstanceOf('Oauth2\Authentication\Database', $this->oauthdb);
$this->oauth->registerDbAbstractor($this->oauthdb); $this->oauth->registerDbAbstractor($this->oauthdb);
} }
function test_generateCode() public function test_generateCode()
{ {
$reflector = new ReflectionClass($this->oauth); $reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('generateCode'); $method = $reflector->getMethod('generateCode');
$method->setAccessible(true); $method->setAccessible(true);
$result = $method->invoke($this->oauth); $result = $method->invoke($this->oauth);
$result2 = $method->invoke($this->oauth); $result2 = $method->invoke($this->oauth);
$this->assertEquals(40, strlen($result)); $this->assertEquals(40, strlen($result));
$this->assertNotEquals($result, $result2); $this->assertNotEquals($result, $result2);
} }
function test_redirectUri() public function test_redirectUri()
{ {
$result1 = $this->oauth->redirectUri('http://example.com/foo'); $result1 = $this->oauth->redirectUri('http://example.com/foo');
$result2 = $this->oauth->redirectUri('http://example.com/foo', array('foo' => 'bar')); $result2 = $this->oauth->redirectUri('http://example.com/foo', array('foo' => 'bar'));
$result3 = $this->oauth->redirectUri('http://example.com/foo', array('foo' => 'bar'), '#'); $result3 = $this->oauth->redirectUri('http://example.com/foo', array('foo' => 'bar'), '#');
$this->assertEquals('http://example.com/foo?', $result1); $this->assertEquals('http://example.com/foo?', $result1);
$this->assertEquals('http://example.com/foo?foo=bar', $result2); $this->assertEquals('http://example.com/foo?foo=bar', $result2);
$this->assertEquals('http://example.com/foo#foo=bar', $result3); $this->assertEquals('http://example.com/foo#foo=bar', $result3);
} }
function test_checkClientAuthoriseParams_GET() public function test_checkClientAuthoriseParams_GET()
{ {
$_GET['client_id'] = 'test'; $_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test'; $_GET['redirect_uri'] = 'http://example.com/test';
$_GET['response_type'] = 'code'; $_GET['response_type'] = 'code';
$_GET['scope'] = 'test'; $_GET['scope'] = 'test';
$expect = array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'response_type' => 'code',
'scopes' => array(
0 => array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
)
)
);
$result = $this->oauth->checkClientAuthoriseParams(); $expect = array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'response_type' => 'code',
'scopes' => array(
0 => array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
)
)
);
$this->assertEquals($expect, $result); $result = $this->oauth->checkClientAuthoriseParams();
}
function test_checkClientAuthoriseParams_PassedParams() $this->assertEquals($expect, $result);
{ }
unset($_GET['client_id']);
unset($_GET['redirect_uri']);
unset($_GET['response_type']);
unset($_GET['scope']);
$params = array( public function test_checkClientAuthoriseParams_PassedParams()
'client_id' => 'test', {
'redirect_uri' => 'http://example.com/test', unset($_GET['client_id']);
'response_type' => 'code', unset($_GET['redirect_uri']);
'scope' => 'test' unset($_GET['response_type']);
); unset($_GET['scope']);
$this->assertEquals(array( $params = array(
'client_id' => 'test', 'client_id' => 'test',
'redirect_uri' => 'http://example.com/test', 'redirect_uri' => 'http://example.com/test',
'response_type' => 'code', 'response_type' => 'code',
'scopes' => array(0 => array( 'scope' => 'test'
'id' => 1, );
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
), $this->oauth->checkClientAuthoriseParams($params));
}
/** $this->assertEquals(array(
* @expectedException Oauth2\Authentication\ClientException 'client_id' => 'test',
* @expectedExceptionCode 0 'redirect_uri' => 'http://example.com/test',
*/ 'response_type' => 'code',
function test_checkClientAuthoriseParams_missingClientId() 'scopes' => array(0 => array(
{ 'id' => 1,
$this->oauth->checkClientAuthoriseParams(); 'scope' => 'test',
} 'name' => 'test',
'description' => 'test'
))
), $this->oauth->checkClientAuthoriseParams($params));
}
/** /**
* @expectedException Oauth2\Authentication\ClientException * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0 * @expectedExceptionCode 0
*/ */
function test_checkClientAuthoriseParams_missingRedirectUri() public function test_checkClientAuthoriseParams_missingClientId()
{ {
$_GET['client_id'] = 'test'; $this->oauth->checkClientAuthoriseParams();
}
$this->oauth->checkClientAuthoriseParams(); /**
} * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0
*/
public function test_checkClientAuthoriseParams_missingRedirectUri()
{
$_GET['client_id'] = 'test';
/** $this->oauth->checkClientAuthoriseParams();
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 0
*/
function test_checkClientAuthoriseParams_missingResponseType()
{
$_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test';
$this->oauth->checkClientAuthoriseParams(); /**
} * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0
*/
public function test_checkClientAuthoriseParams_missingResponseType()
{
$_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test';
/** $this->oauth->checkClientAuthoriseParams();
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 0
*/
function test_checkClientAuthoriseParams_missingScopes()
{
$_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test';
$_GET['response_type'] = 'code';
$_GET['scope'] = ' ';
$this->oauth->checkClientAuthoriseParams(); /**
} * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0
*/
public function test_checkClientAuthoriseParams_missingScopes()
{
$_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test';
$_GET['response_type'] = 'code';
$_GET['scope'] = ' ';
/** $this->oauth->checkClientAuthoriseParams();
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 4
*/
function test_checkClientAuthoriseParams_invalidScopes()
{
$_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test';
$_GET['response_type'] = 'code';
$_GET['scope'] = 'blah';
$this->oauth->checkClientAuthoriseParams(); /**
} * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 4
*/
public function test_checkClientAuthoriseParams_invalidScopes()
{
$_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test';
$_GET['response_type'] = 'code';
$_GET['scope'] = 'blah';
function test_newAuthoriseRequest() $this->oauth->checkClientAuthoriseParams();
{ }
$result = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$this->assertEquals(40, strlen($result)); public function test_newAuthoriseRequest()
} {
$result = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
function test_newAuthoriseRequest_isUnique() $this->assertEquals(40, strlen($result));
{ }
$result1 = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$result2 = $this->oauth->newAuthoriseRequest('user', '123', array( public function test_newAuthoriseRequest_isUnique()
'client_id' => 'test', {
'redirect_uri' => 'http://example.com/test', $result1 = $this->oauth->newAuthoriseRequest('user', '123', array(
'scopes' => array(array( 'client_id' => 'test',
'id' => 1, 'redirect_uri' => 'http://example.com/test',
'scope' => 'test', 'scopes' => array(array(
'name' => 'test', 'id' => 1,
'description' => 'test' 'scope' => 'test',
)) 'name' => 'test',
)); 'description' => 'test'
))
));
$this->assertNotEquals($result1, $result2); $result2 = $this->oauth->newAuthoriseRequest('user', '123', array(
} 'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
function test_issueAccessToken_POST() $this->assertNotEquals($result1, $result2);
{ }
$auth_code = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$_POST['client_id'] = 'test'; public function test_issueAccessToken_POST()
$_POST['client_secret'] = 'test'; {
$_POST['redirect_uri'] = 'http://example.com/test'; $auth_code = $this->oauth->newAuthoriseRequest('user', '123', array(
$_POST['grant_type'] = 'authorization_code'; 'client_id' => 'test',
$_POST['code'] = $auth_code; 'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$result = $this->oauth->issueAccessToken(); $_POST['client_id'] = 'test';
$_POST['client_secret'] = 'test';
$_POST['redirect_uri'] = 'http://example.com/test';
$_POST['grant_type'] = 'authorization_code';
$_POST['code'] = $auth_code;
$this->assertCount(3, $result); $result = $this->oauth->issueAccessToken();
$this->assertArrayHasKey('access_token', $result);
$this->assertArrayHasKey('token_type', $result);
$this->assertArrayHasKey('expires_in', $result);
}
function test_issueAccessToken_PassedParams() $this->assertCount(3, $result);
{ $this->assertArrayHasKey('access_token', $result);
$auth_code = $this->oauth->newAuthoriseRequest('user', '123', array( $this->assertArrayHasKey('token_type', $result);
'client_id' => 'test', $this->assertArrayHasKey('expires_in', $result);
'redirect_uri' => 'http://example.com/test', }
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$params['client_id'] = 'test'; public function test_issueAccessToken_PassedParams()
$params['client_secret'] = 'test'; {
$params['redirect_uri'] = 'http://example.com/test'; $auth_code = $this->oauth->newAuthoriseRequest('user', '123', array(
$params['grant_type'] = 'authorization_code'; 'client_id' => 'test',
$params['code'] = $auth_code; 'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$result = $this->oauth->issueAccessToken($params); $params['client_id'] = 'test';
$params['client_secret'] = 'test';
$params['redirect_uri'] = 'http://example.com/test';
$params['grant_type'] = 'authorization_code';
$params['code'] = $auth_code;
$this->assertCount(3, $result); $result = $this->oauth->issueAccessToken($params);
$this->assertArrayHasKey('access_token', $result);
$this->assertArrayHasKey('token_type', $result);
$this->assertArrayHasKey('expires_in', $result);
}
/** $this->assertCount(3, $result);
* @expectedException Oauth2\Authentication\ClientException $this->assertArrayHasKey('access_token', $result);
* @expectedExceptionCode 0 $this->assertArrayHasKey('token_type', $result);
*/ $this->assertArrayHasKey('expires_in', $result);
function test_issueAccessToken_missingGrantType() }
{
$this->oauth->issueAccessToken();
}
/** /**
* @expectedException Oauth2\Authentication\ClientException * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 7 * @expectedExceptionCode 0
*/ */
function test_issueAccessToken_unsupportedGrantType() public function test_issueAccessToken_missingGrantType()
{ {
$params['grant_type'] = 'blah'; $this->oauth->issueAccessToken();
}
$this->oauth->issueAccessToken($params); /**
} * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 7
*/
public function test_issueAccessToken_unsupportedGrantType()
{
$params['grant_type'] = 'blah';
/** $this->oauth->issueAccessToken($params);
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 0
*/
function test_completeAuthCodeGrant_missingClientId()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$method->invoke($this->oauth); /**
} * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0
*/
public function test_completeAuthCodeGrant_missingClientId()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
/** $method->invoke($this->oauth);
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 0
*/
function test_completeAuthCodeGrant_missingClientSecret()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$authParams['client_id'] = 'test'; /**
* @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0
*/
public function test_completeAuthCodeGrant_missingClientSecret()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$method->invoke($this->oauth, $authParams); $authParams['client_id'] = 'test';
}
/** $method->invoke($this->oauth, $authParams);
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 0
*/
function test_completeAuthCodeGrant_missingRedirectUri()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$authParams['client_id'] = 'test'; /**
$authParams['client_secret'] = 'test'; * @expectedException Oauth2\Authentication\ClientException
* @expectedExceptionCode 0
*/
public function test_completeAuthCodeGrant_missingRedirectUri()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$method->invoke($this->oauth, $authParams); $authParams['client_id'] = 'test';
} $authParams['client_secret'] = 'test';
/** $method->invoke($this->oauth, $authParams);
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 8
*/
function test_completeAuthCodeGrant_invalidClient()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$authParams['client_id'] = 'test'; /**
$authParams['client_secret'] = 'test123'; * @expectedException Oauth2\Authentication\ClientException
$authParams['redirect_uri'] = 'http://example.com/test'; * @expectedExceptionCode 8
*/
public function test_completeAuthCodeGrant_invalidClient()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$method->invoke($this->oauth, $authParams); $authParams['client_id'] = 'test';
} $authParams['client_secret'] = 'test123';
$authParams['redirect_uri'] = 'http://example.com/test';
/** $method->invoke($this->oauth, $authParams);
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 0
*/
function test_completeAuthCodeGrant_missingCode()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$authParams['client_id'] = 'test'; /**
$authParams['client_secret'] = 'test'; * @expectedException Oauth2\Authentication\ClientException
$authParams['redirect_uri'] = 'http://example.com/test'; * @expectedExceptionCode 0
*/
public function test_completeAuthCodeGrant_missingCode()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$method->invoke($this->oauth, $authParams); $authParams['client_id'] = 'test';
} $authParams['client_secret'] = 'test';
$authParams['redirect_uri'] = 'http://example.com/test';
/** $method->invoke($this->oauth, $authParams);
* @expectedException Oauth2\Authentication\ClientException }
* @expectedExceptionCode 9
*/
function test_completeAuthCodeGrant_invalidCode()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$authParams['client_id'] = 'test'; /**
$authParams['client_secret'] = 'test'; * @expectedException Oauth2\Authentication\ClientException
$authParams['redirect_uri'] = 'http://example.com/test'; * @expectedExceptionCode 9
$authParams['code'] = 'blah'; */
public function test_completeAuthCodeGrant_invalidCode()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('completeAuthCodeGrant');
$method->setAccessible(true);
$method->invoke($this->oauth, $authParams); $authParams['client_id'] = 'test';
} $authParams['client_secret'] = 'test';
$authParams['redirect_uri'] = 'http://example.com/test';
$authParams['code'] = 'blah';
/** $method->invoke($this->oauth, $authParams);
* @expectedException Oauth2\Authentication\ServerException }
* @expectedExceptionMessage No registered database abstractor
*/
function test_noRegisteredDatabaseAbstractor()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('_dbCall');
$method->setAccessible(true);
$dbAbstractor = $reflector->getProperty('_db'); /**
$dbAbstractor->setAccessible(true); * @expectedException Oauth2\Authentication\ServerException
$dbAbstractor->setValue($this->oauth, null); * @expectedExceptionMessage No registered database abstractor
*/
public function test_noRegisteredDatabaseAbstractor()
{
$reflector = new ReflectionClass($this->oauth);
$method = $reflector->getMethod('_dbCall');
$method->setAccessible(true);
$result = $method->invoke($this->oauth); $dbAbstractor = $reflector->getProperty('_db');
} $dbAbstractor->setAccessible(true);
$dbAbstractor->setValue($this->oauth, null);
/** $result = $method->invoke($this->oauth);
* @expectedException Oauth2\Authentication\ServerException }
* @expectedExceptionMessage Registered database abstractor is not an instance of Oauth2\Authentication\Database
*/
function test_invalidRegisteredDatabaseAbstractor()
{
$fake = new stdClass;
$this->oauth->registerDbAbstractor($fake);
$reflector = new ReflectionClass($this->oauth); /**
$method = $reflector->getMethod('_dbCall'); * @expectedException Oauth2\Authentication\ServerException
$method->setAccessible(true); * @expectedExceptionMessage Registered database abstractor is not an instance of Oauth2\Authentication\Database
*/
public function test_invalidRegisteredDatabaseAbstractor()
{
$fake = new stdClass;
$this->oauth->registerDbAbstractor($fake);
$result = $method->invoke($this->oauth); $reflector = new ReflectionClass($this->oauth);
} $method = $reflector->getMethod('_dbCall');
$method->setAccessible(true);
} $result = $method->invoke($this->oauth);
}
}