diff --git a/composer.json b/composer.json index 8f7f2dca..2635dcee 100644 --- a/composer.json +++ b/composer.json @@ -6,18 +6,18 @@ "require": { "php": ">=7.1.0", "ext-openssl": "*", - "league/event": "^2.1", - "lcobucci/jwt": "^3.2.2", + "league/event": "^2.2", + "lcobucci/jwt": "^3.3.1", "psr/http-message": "^1.0.1", - "defuse/php-encryption": "^2.1", + "defuse/php-encryption": "^2.2.1", "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^6.3 || ^7.0", - "zendframework/zend-diactoros": "^1.3.2", - "phpstan/phpstan": "^0.9.2", - "phpstan/phpstan-phpunit": "^0.9.4", - "phpstan/phpstan-strict-rules": "^0.9.0", + "phpunit/phpunit": "^7.5.13 || ^8.2.3", + "zendframework/zend-diactoros": "^2.1.2", + "phpstan/phpstan": "^0.11.8", + "phpstan/phpstan-phpunit": "^0.11.2", + "phpstan/phpstan-strict-rules": "^0.11.1", "roave/security-advisories": "dev-master" }, "repositories": [ diff --git a/tests/AuthorizationServerTest.php b/tests/AuthorizationServerTest.php index f88cd733..9ba15749 100644 --- a/tests/AuthorizationServerTest.php +++ b/tests/AuthorizationServerTest.php @@ -31,7 +31,7 @@ class AuthorizationServerTest extends TestCase { const DEFAULT_SCOPE = 'basic'; - public function setUp() + public function setUp(): void { // Make sure the keys have the correct permissions. chmod(__DIR__ . '/Stubs/private.key', 0600); @@ -326,10 +326,6 @@ class AuthorizationServerTest extends TestCase } } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 2 - */ public function testValidateAuthorizationRequestUnregistered() { $server = new AuthorizationServer( @@ -340,19 +336,13 @@ class AuthorizationServerTest extends TestCase 'file://' . __DIR__ . '/Stubs/public.key' ); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(2); $server->validateAuthorizationRequest($request); } diff --git a/tests/AuthorizationValidators/BearerTokenValidatorTest.php b/tests/AuthorizationValidators/BearerTokenValidatorTest.php index 801846cb..25c7b188 100644 --- a/tests/AuthorizationValidators/BearerTokenValidatorTest.php +++ b/tests/AuthorizationValidators/BearerTokenValidatorTest.php @@ -11,10 +11,6 @@ use Zend\Diactoros\ServerRequest; class BearerTokenValidatorTest extends TestCase { - /** - * @expectedException League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 9 - */ public function testThrowExceptionWhenAccessTokenIsNotSigned() { $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); @@ -35,6 +31,9 @@ class BearerTokenValidatorTest extends TestCase $request = new ServerRequest(); $request = $request->withHeader('authorization', sprintf('Bearer %s', $unsignedJwt)); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(9); + $bearerTokenValidator->validateAuthorization($request); } } diff --git a/tests/Grant/AbstractGrantTest.php b/tests/Grant/AbstractGrantTest.php index ec8d3f47..ea0afbca 100644 --- a/tests/Grant/AbstractGrantTest.php +++ b/tests/Grant/AbstractGrantTest.php @@ -148,9 +148,7 @@ class AbstractGrantTest extends TestCase $this->assertEquals($client, $result); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ + public function testValidateClientMissingClientId() { $client = new ClientEntity(); @@ -167,12 +165,11 @@ class AbstractGrantTest extends TestCase $validateClientMethod = $abstractGrantReflection->getMethod('validateClient'); $validateClientMethod->setAccessible(true); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $validateClientMethod->invoke($grantMock, $serverRequest, true, true); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateClientMissingClientSecret() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -192,12 +189,11 @@ class AbstractGrantTest extends TestCase $validateClientMethod = $abstractGrantReflection->getMethod('validateClient'); $validateClientMethod->setAccessible(true); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $validateClientMethod->invoke($grantMock, $serverRequest, true, true); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateClientInvalidClientSecret() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -218,12 +214,11 @@ class AbstractGrantTest extends TestCase $validateClientMethod = $abstractGrantReflection->getMethod('validateClient'); $validateClientMethod->setAccessible(true); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $validateClientMethod->invoke($grantMock, $serverRequest, true, true); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateClientInvalidRedirectUri() { $client = new ClientEntity(); @@ -246,12 +241,11 @@ class AbstractGrantTest extends TestCase $validateClientMethod = $abstractGrantReflection->getMethod('validateClient'); $validateClientMethod->setAccessible(true); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $validateClientMethod->invoke($grantMock, $serverRequest, true, true); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateClientInvalidRedirectUriArray() { $client = new ClientEntity(); @@ -274,12 +268,11 @@ class AbstractGrantTest extends TestCase $validateClientMethod = $abstractGrantReflection->getMethod('validateClient'); $validateClientMethod->setAccessible(true); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $validateClientMethod->invoke($grantMock, $serverRequest, true, true); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateClientBadClient() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -300,6 +293,8 @@ class AbstractGrantTest extends TestCase $validateClientMethod = $abstractGrantReflection->getMethod('validateClient'); $validateClientMethod->setAccessible(true); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $validateClientMethod->invoke($grantMock, $serverRequest, true); } @@ -439,8 +434,7 @@ class AbstractGrantTest extends TestCase $method = $abstractGrantReflection->getMethod('getQueryStringParameter'); $method->setAccessible(true); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withQueryParams([ + $serverRequest = (new ServerRequest())->withQueryParams([ 'foo' => 'bar', ]); @@ -461,9 +455,6 @@ class AbstractGrantTest extends TestCase $this->assertEquals([$scope], $grantMock->validateScopes('basic ')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateScopesBadScope() { $scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); @@ -473,6 +464,8 @@ class AbstractGrantTest extends TestCase $grantMock = $this->getMockForAbstractClass(AbstractGrant::class); $grantMock->setScopeRepository($scopeRepositoryMock); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $grantMock->validateScopes('basic '); } @@ -484,7 +477,7 @@ class AbstractGrantTest extends TestCase $method = $abstractGrantReflection->getMethod('generateUniqueIdentifier'); $method->setAccessible(true); - $this->assertInternalType('string', $method->invoke($grantMock)); + $this->assertIsString($method->invoke($grantMock)); } public function testCanRespondToAuthorizationRequest() @@ -493,21 +486,21 @@ class AbstractGrantTest extends TestCase $this->assertFalse($grantMock->canRespondToAuthorizationRequest(new ServerRequest())); } - /** - * @expectedException \LogicException - */ public function testValidateAuthorizationRequest() { $grantMock = $this->getMockForAbstractClass(AbstractGrant::class); + + $this->expectException(\LogicException::class); + $grantMock->validateAuthorizationRequest(new ServerRequest()); } - /** - * @expectedException \LogicException - */ public function testCompleteAuthorizationRequest() { $grantMock = $this->getMockForAbstractClass(AbstractGrant::class); + + $this->expectException(\LogicException::class); + $grantMock->completeAuthorizationRequest(new AuthorizationRequest()); } } diff --git a/tests/Grant/AuthCodeGrantTest.php b/tests/Grant/AuthCodeGrantTest.php index 07b220c5..2d3098c4 100644 --- a/tests/Grant/AuthCodeGrantTest.php +++ b/tests/Grant/AuthCodeGrantTest.php @@ -26,6 +26,7 @@ use LeagueTests\Stubs\StubResponseType; use LeagueTests\Stubs\UserEntity; use PHPUnit\Framework\TestCase; use Zend\Diactoros\ServerRequest; +use Zend\Diactoros\ServerRequestFactory; class AuthCodeGrantTest extends TestCase { @@ -40,7 +41,7 @@ class AuthCodeGrantTest extends TestCase const CODE_CHALLENGE = 'E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM'; - public function setUp() + public function setUp(): void { $this->cryptStub = new CryptTraitStub(); } @@ -200,9 +201,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(AuthorizationRequest::class, $grant->validateAuthorizationRequest($request)); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateAuthorizationRequestCodeChallengeInvalidLengthTooShort() { $client = new ClientEntity(); @@ -218,28 +216,18 @@ class AuthCodeGrantTest extends TestCase $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - 'code_challenge' => str_repeat('A', 42), - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + 'code_challenge' => str_repeat('A', 42), + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateAuthorizationRequestCodeChallengeInvalidLengthTooLong() { $client = new ClientEntity(); @@ -255,28 +243,18 @@ class AuthCodeGrantTest extends TestCase $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - 'code_challenge' => str_repeat('A', 129), - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + 'code_challenge' => str_repeat('A', 129), + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testValidateAuthorizationRequestCodeChallengeInvalidCharacters() { $client = new ClientEntity(); @@ -292,29 +270,18 @@ class AuthCodeGrantTest extends TestCase $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - 'code_challenge' => str_repeat('A', 42) . '!', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + 'code_challenge' => str_repeat('A', 42) . '!', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testValidateAuthorizationRequestMissingClientId() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -326,26 +293,16 @@ class AuthCodeGrantTest extends TestCase ); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 4 - */ public function testValidateAuthorizationRequestInvalidClientId() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -358,27 +315,17 @@ class AuthCodeGrantTest extends TestCase ); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(4); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 4 - */ public function testValidateAuthorizationRequestBadRedirectUriString() { $client = new ClientEntity(); @@ -393,28 +340,18 @@ class AuthCodeGrantTest extends TestCase ); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://bar', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(4); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 4 - */ public function testValidateAuthorizationRequestBadRedirectUriArray() { $client = new ClientEntity(); @@ -429,28 +366,18 @@ class AuthCodeGrantTest extends TestCase ); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://bar', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(4); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testValidateAuthorizationRequestInvalidCodeChallengeMethod() { $client = new ClientEntity(); @@ -472,22 +399,16 @@ class AuthCodeGrantTest extends TestCase $grant->setScopeRepository($scopeRepositoryMock); $grant->setDefaultScope(self::DEFAULT_SCOPE); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - 'code_challenge' => 'foobar', - 'code_challenge_method' => 'foo', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + 'code_challenge' => 'foobar', + 'code_challenge_method' => 'foo', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); $grant->validateAuthorizationRequest($request); } @@ -513,10 +434,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 9 - */ public function testCompleteAuthorizationRequestDenied() { $authRequest = new AuthorizationRequest(); @@ -535,6 +452,9 @@ class AuthCodeGrantTest extends TestCase ); $grant->setEncryptionKey($this->cryptStub->getKey()); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(9); + $grant->completeAuthorizationRequest($authRequest); } @@ -954,10 +874,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken()); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testRespondToAccessTokenRequestMissingRedirectUri() { $client = new ClientEntity(); @@ -999,13 +915,12 @@ class AuthCodeGrantTest extends TestCase ] ); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); + $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testRespondToAccessTokenRequestRedirectUriMismatch() { $client = new ClientEntity(); @@ -1048,13 +963,12 @@ class AuthCodeGrantTest extends TestCase ] ); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); + $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testRespondToAccessTokenRequestMissingCode() { $client = new ClientEntity(); @@ -1093,6 +1007,9 @@ class AuthCodeGrantTest extends TestCase ] ); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); + /* @var StubResponseType $response */ $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M')); } @@ -1711,10 +1628,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 7 - */ public function testAuthCodeRepositoryFailToPersist() { $authRequest = new AuthorizationRequest(); @@ -1734,13 +1647,12 @@ class AuthCodeGrantTest extends TestCase ); $grant->setEncryptionKey($this->cryptStub->getKey()); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(7); + $this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); } - /** - * @expectedException \League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException - * @expectedExceptionCode 100 - */ public function testAuthCodeRepositoryFailToPersistUniqueNoInfiniteLoop() { $authRequest = new AuthorizationRequest(); @@ -1759,6 +1671,9 @@ class AuthCodeGrantTest extends TestCase new DateInterval('PT10M') ); + $this->expectException(\League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException::class); + $this->expectExceptionCode(100); + $this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); } @@ -1831,10 +1746,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken()); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 7 - */ public function testRefreshTokenRepositoryFailToPersist() { $client = new ClientEntity(); @@ -1896,6 +1807,9 @@ class AuthCodeGrantTest extends TestCase ] ); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(7); + /** @var StubResponseType $response */ $response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M')); @@ -1903,10 +1817,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken()); } - /** - * @expectedException \League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException - * @expectedExceptionCode 100 - */ public function testRefreshTokenRepositoryFailToPersistUniqueNoInfiniteLoop() { $client = new ClientEntity(); @@ -1968,6 +1878,9 @@ class AuthCodeGrantTest extends TestCase ] ); + $this->expectException(\League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException::class); + $this->expectExceptionCode(100); + /** @var StubResponseType $response */ $response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M')); @@ -1975,9 +1888,6 @@ class AuthCodeGrantTest extends TestCase $this->assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken()); } - /** - * @expectedException \LogicException - */ public function testCompleteAuthorizationRequestNoUser() { $grant = new AuthCodeGrant( @@ -1986,6 +1896,8 @@ class AuthCodeGrantTest extends TestCase new DateInterval('PT10M') ); + $this->expectException(\LogicException::class); + $grant->completeAuthorizationRequest(new AuthorizationRequest()); } @@ -2011,20 +1923,11 @@ class AuthCodeGrantTest extends TestCase $grant->setScopeRepository($scopeRepositoryMock); $grant->setDefaultScope(self::DEFAULT_SCOPE); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - [], - [], - [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + ]); $this->expectException(OAuthServerException::class); $this->expectExceptionCode(3); diff --git a/tests/Grant/ImplicitGrantTest.php b/tests/Grant/ImplicitGrantTest.php index b63c3ffa..558923b5 100644 --- a/tests/Grant/ImplicitGrantTest.php +++ b/tests/Grant/ImplicitGrantTest.php @@ -31,7 +31,7 @@ class ImplicitGrantTest extends TestCase */ protected $cryptStub; - public function setUp() + public function setUp(): void { $this->cryptStub = new CryptTraitStub(); } @@ -51,12 +51,12 @@ class ImplicitGrantTest extends TestCase ); } - /** - * @expectedException \LogicException - */ public function testRespondToAccessTokenRequest() { $grant = new ImplicitGrant(new DateInterval('PT10M')); + + $this->expectException(\LogicException::class); + $grant->respondToAccessTokenRequest( new ServerRequest(), new StubResponseType(), @@ -68,19 +68,10 @@ class ImplicitGrantTest extends TestCase { $grant = new ImplicitGrant(new DateInterval('PT10M')); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'token', - 'client_id' => 'foo', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'token', + 'client_id' => 'foo', + ]); $this->assertTrue($grant->canRespondToAuthorizationRequest($request)); } @@ -101,20 +92,11 @@ class ImplicitGrantTest extends TestCase $grant->setScopeRepository($scopeRepositoryMock); $grant->setDefaultScope(self::DEFAULT_SCOPE); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + ]); $this->assertInstanceOf(AuthorizationRequest::class, $grant->validateAuthorizationRequest($request)); } @@ -135,28 +117,15 @@ class ImplicitGrantTest extends TestCase $grant->setScopeRepository($scopeRepositoryMock); $grant->setDefaultScope(self::DEFAULT_SCOPE); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://foo/bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://foo/bar', + ]); $this->assertInstanceOf(AuthorizationRequest::class, $grant->validateAuthorizationRequest($request)); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testValidateAuthorizationRequestMissingClientId() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -164,26 +133,14 @@ class ImplicitGrantTest extends TestCase $grant = new ImplicitGrant(new DateInterval('PT10M')); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - ] - ); + $request = (new ServerRequest())->withQueryParams(['response_type' => 'code']); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 4 - */ public function testValidateAuthorizationRequestInvalidClientId() { $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); @@ -192,27 +149,17 @@ class ImplicitGrantTest extends TestCase $grant = new ImplicitGrant(new DateInterval('PT10M')); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(4); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 4 - */ public function testValidateAuthorizationRequestBadRedirectUriString() { $client = new ClientEntity(); @@ -223,28 +170,18 @@ class ImplicitGrantTest extends TestCase $grant = new ImplicitGrant(new DateInterval('PT10M')); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://bar', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(4); $grant->validateAuthorizationRequest($request); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 4 - */ public function testValidateAuthorizationRequestBadRedirectUriArray() { $client = new ClientEntity(); @@ -255,20 +192,14 @@ class ImplicitGrantTest extends TestCase $grant = new ImplicitGrant(new DateInterval('PT10M')); $grant->setClientRepository($clientRepositoryMock); - $request = new ServerRequest( - [], - [], - null, - null, - 'php://input', - $headers = [], - $cookies = [], - $queryParams = [ - 'response_type' => 'code', - 'client_id' => 'foo', - 'redirect_uri' => 'http://bar', - ] - ); + $request = (new ServerRequest())->withQueryParams([ + 'response_type' => 'code', + 'client_id' => 'foo', + 'redirect_uri' => 'http://bar', + ]); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(4); $grant->validateAuthorizationRequest($request); } @@ -302,10 +233,6 @@ class ImplicitGrantTest extends TestCase $this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 9 - */ public function testCompleteAuthorizationRequestDenied() { $authRequest = new AuthorizationRequest(); @@ -326,6 +253,9 @@ class ImplicitGrantTest extends TestCase $grant->setAccessTokenRepository($accessTokenRepositoryMock); $grant->setScopeRepository($scopeRepositoryMock); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(9); + $grant->completeAuthorizationRequest($authRequest); } @@ -360,10 +290,6 @@ class ImplicitGrantTest extends TestCase $this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest)); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 7 - */ public function testAccessTokenRepositoryFailToPersist() { $authRequest = new AuthorizationRequest(); @@ -385,13 +311,12 @@ class ImplicitGrantTest extends TestCase $grant->setAccessTokenRepository($accessTokenRepositoryMock); $grant->setScopeRepository($scopeRepositoryMock); + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(7); + $grant->completeAuthorizationRequest($authRequest); } - /** - * @expectedException \League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException - * @expectedExceptionCode 100 - */ public function testAccessTokenRepositoryFailToPersistUniqueNoInfiniteLoop() { $authRequest = new AuthorizationRequest(); @@ -413,34 +338,38 @@ class ImplicitGrantTest extends TestCase $grant->setAccessTokenRepository($accessTokenRepositoryMock); $grant->setScopeRepository($scopeRepositoryMock); + $this->expectException(\League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException::class); + $this->expectExceptionCode(100); + $grant->completeAuthorizationRequest($authRequest); } - /** - * @expectedException \LogicException - */ public function testSetRefreshTokenTTL() { $grant = new ImplicitGrant(new DateInterval('PT10M')); + + $this->expectException(\LogicException::class); + $grant->setRefreshTokenTTL(new DateInterval('PT10M')); } - /** - * @expectedException \LogicException - */ public function testSetRefreshTokenRepository() { $grant = new ImplicitGrant(new DateInterval('PT10M')); + $refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); + + $this->expectException(\LogicException::class); + $grant->setRefreshTokenRepository($refreshTokenRepositoryMock); } - /** - * @expectedException \LogicException - */ public function testCompleteAuthorizationRequestNoUser() { $grant = new ImplicitGrant(new DateInterval('PT10M')); + + $this->expectException(\LogicException::class); + $grant->completeAuthorizationRequest(new AuthorizationRequest()); } } diff --git a/tests/Grant/PasswordGrantTest.php b/tests/Grant/PasswordGrantTest.php index 7fc99e83..4b7de98a 100644 --- a/tests/Grant/PasswordGrantTest.php +++ b/tests/Grant/PasswordGrantTest.php @@ -127,9 +127,6 @@ class PasswordGrantTest extends TestCase $this->assertNull($responseType->getRefreshToken()); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testRespondToRequestMissingUsername() { $client = new ClientEntity(); @@ -146,21 +143,18 @@ class PasswordGrantTest extends TestCase $grant->setClientRepository($clientRepositoryMock); $grant->setAccessTokenRepository($accessTokenRepositoryMock); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - ] - ); + $serverRequest = (new ServerRequest())->withQueryParams([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - */ public function testRespondToRequestMissingPassword() { $client = new ClientEntity(); @@ -177,23 +171,19 @@ class PasswordGrantTest extends TestCase $grant->setClientRepository($clientRepositoryMock); $grant->setAccessTokenRepository($accessTokenRepositoryMock); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'username' => 'alex', - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'username' => 'alex', + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 10 - */ public function testRespondToRequestBadCredentials() { $client = new ClientEntity(); @@ -211,17 +201,18 @@ class PasswordGrantTest extends TestCase $grant->setClientRepository($clientRepositoryMock); $grant->setAccessTokenRepository($accessTokenRepositoryMock); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'username' => 'alex', - 'password' => 'whisky', - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'username' => 'alex', + 'password' => 'whisky', + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(10); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } } diff --git a/tests/Grant/RefreshTokenGrantTest.php b/tests/Grant/RefreshTokenGrantTest.php index e895f16b..632cbe80 100644 --- a/tests/Grant/RefreshTokenGrantTest.php +++ b/tests/Grant/RefreshTokenGrantTest.php @@ -27,7 +27,7 @@ class RefreshTokenGrantTest extends TestCase */ protected $cryptStub; - public function setUp() + public function setUp(): void { $this->cryptStub = new CryptTraitStub(); } @@ -209,10 +209,6 @@ class RefreshTokenGrantTest extends TestCase $this->assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken()); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 5 - */ public function testRespondToUnexpectedScope() { $client = new ClientEntity(); @@ -251,24 +247,21 @@ class RefreshTokenGrantTest extends TestCase ) ); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'refresh_token' => $oldRefreshToken, - 'scope' => 'foobar', - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'refresh_token' => $oldRefreshToken, + 'scope' => 'foobar', + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(5); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 3 - */ public function testRespondToRequestMissingOldToken() { $client = new ClientEntity(); @@ -285,22 +278,19 @@ class RefreshTokenGrantTest extends TestCase $grant->setEncryptionKey($this->cryptStub->getKey()); $grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(3); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 8 - */ public function testRespondToRequestInvalidOldToken() { $client = new ClientEntity(); @@ -319,23 +309,20 @@ class RefreshTokenGrantTest extends TestCase $oldRefreshToken = 'foobar'; - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'refresh_token' => $oldRefreshToken, - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'refresh_token' => $oldRefreshToken, + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(8); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 8 - */ public function testRespondToRequestClientMismatch() { $client = new ClientEntity(); @@ -368,23 +355,20 @@ class RefreshTokenGrantTest extends TestCase ) ); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'refresh_token' => $oldRefreshToken, - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'refresh_token' => $oldRefreshToken, + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(8); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 8 - */ public function testRespondToRequestExpiredToken() { $client = new ClientEntity(); @@ -414,23 +398,20 @@ class RefreshTokenGrantTest extends TestCase ) ); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'refresh_token' => $oldRefreshToken, - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'refresh_token' => $oldRefreshToken, + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(8); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } - /** - * @expectedException \League\OAuth2\Server\Exception\OAuthServerException - * @expectedExceptionCode 8 - */ public function testRespondToRequestRevokedToken() { $client = new ClientEntity(); @@ -461,16 +442,17 @@ class RefreshTokenGrantTest extends TestCase ) ); - $serverRequest = new ServerRequest(); - $serverRequest = $serverRequest->withParsedBody( - [ - 'client_id' => 'foo', - 'client_secret' => 'bar', - 'refresh_token' => $oldRefreshToken, - ] - ); + $serverRequest = (new ServerRequest())->withParsedBody([ + 'client_id' => 'foo', + 'client_secret' => 'bar', + 'refresh_token' => $oldRefreshToken, + ]); $responseType = new StubResponseType(); + + $this->expectException(\League\OAuth2\Server\Exception\OAuthServerException::class); + $this->expectExceptionCode(8); + $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M')); } } diff --git a/tests/ResponseTypes/BearerResponseTypeTest.php b/tests/ResponseTypes/BearerResponseTypeTest.php index 5eddfdef..97bc37aa 100644 --- a/tests/ResponseTypes/BearerResponseTypeTest.php +++ b/tests/ResponseTypes/BearerResponseTypeTest.php @@ -57,7 +57,7 @@ class BearerResponseTypeTest extends TestCase $response->getBody()->rewind(); $json = json_decode($response->getBody()->getContents()); - $this->assertAttributeEquals('Bearer', 'token_type', $json); + $this->assertEquals('Bearer', $json->token_type); $this->assertObjectHasAttribute('expires_in', $json); $this->assertObjectHasAttribute('access_token', $json); $this->assertObjectHasAttribute('refresh_token', $json); @@ -100,13 +100,13 @@ class BearerResponseTypeTest extends TestCase $response->getBody()->rewind(); $json = json_decode($response->getBody()->getContents()); - $this->assertAttributeEquals('Bearer', 'token_type', $json); + $this->assertEquals('Bearer', $json->token_type); $this->assertObjectHasAttribute('expires_in', $json); $this->assertObjectHasAttribute('access_token', $json); $this->assertObjectHasAttribute('refresh_token', $json); $this->assertObjectHasAttribute('foo', $json); - $this->assertAttributeEquals('bar', 'foo', $json); + $this->assertEquals('bar', $json->foo); } public function testDetermineAccessTokenInHeaderValidToken() diff --git a/tests/Utils/CryptKeyTest.php b/tests/Utils/CryptKeyTest.php index 9f3f337c..1fe79d20 100644 --- a/tests/Utils/CryptKeyTest.php +++ b/tests/Utils/CryptKeyTest.php @@ -7,11 +7,10 @@ use PHPUnit\Framework\TestCase; class CryptKeyTest extends TestCase { - /** - * @expectedException \LogicException - */ public function testNoFile() { + $this->expectException(\LogicException::class); + new CryptKey('undefined file'); } diff --git a/tests/Utils/CryptTraitTest.php b/tests/Utils/CryptTraitTest.php index c517cec2..06d0b219 100644 --- a/tests/Utils/CryptTraitTest.php +++ b/tests/Utils/CryptTraitTest.php @@ -10,7 +10,7 @@ class CryptTraitTest extends TestCase { protected $cryptStub; - protected function setUp() + protected function setUp(): void { $this->cryptStub = new CryptTraitStub(); }