From 1f87c7a7be7f202d4957908cbfd157b63770b04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Sun, 11 Feb 2018 23:14:34 +0100 Subject: [PATCH] Update PHPUnit, run static analysis on tests --- .travis.yml | 2 +- composer.json | 5 ++-- phpstan.neon | 9 +++++++ src/Exception/OAuthServerException.php | 5 +++- src/ResponseTypes/RedirectResponse.php | 4 +++- tests/AuthorizationServerTest.php | 8 +++---- .../ResponseTypes/BearerResponseTypeTest.php | 24 +++++-------------- tests/{ => Utils}/CryptKeyTest.php | 6 ++--- tests/{ => Utils}/CryptTraitTest.php | 0 9 files changed, 33 insertions(+), 30 deletions(-) create mode 100644 phpstan.neon rename tests/{ => Utils}/CryptKeyTest.php (82%) rename tests/{ => Utils}/CryptTraitTest.php (100%) diff --git a/.travis.yml b/.travis.yml index 187d4f6f..21f3e926 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ install: script: - vendor/bin/phpunit - - vendor/bin/phpstan analyse -l 6 src + - vendor/bin/phpstan analyse -l 6 -c phpstan.neon src tests branches: only: diff --git a/composer.json b/composer.json index 86f82c41..814d7054 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,10 @@ "defuse/php-encryption": "^2.1" }, "require-dev": { - "phpunit/phpunit": "^4.8.38 || ^5.7.21", + "phpunit/phpunit": "^6.3 || ^7.0", "zendframework/zend-diactoros": "^1.0", - "phpstan/phpstan": "^0.9.2" + "phpstan/phpstan": "^0.9.2", + "phpstan/phpstan-phpunit": "^0.9.4" }, "repositories": [ { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..a4800dd5 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon + - vendor/phpstan/phpstan-phpunit/rules.neon + - vendor/phpstan/phpstan-phpunit/strictRules.neon +parameters: + ignoreErrors: + - '#Class Zend\\Diactoros\\ServerRequest constructor invoked with \d+ parameters, 0-6 required#' + - '#Parameter \#2 \$key of method Lcobucci\\JWT\\Builder::sign\(\) expects string, Lcobucci\\JWT\\Signer\\Key given#' + reportUnmatchedIgnoredErrors: false diff --git a/src/Exception/OAuthServerException.php b/src/Exception/OAuthServerException.php index 756dfd3b..e50942c0 100644 --- a/src/Exception/OAuthServerException.php +++ b/src/Exception/OAuthServerException.php @@ -236,10 +236,13 @@ class OAuthServerException extends \Exception $this->redirectUri .= (strstr($this->redirectUri, '?') === false) ? '?' : '&'; } - return $response->withStatus(302)->withHeader('Location', $this->redirectUri . http_build_query($payload)); + /** @var ResponseInterface $response */ + $response = $response->withStatus(302)->withHeader('Location', $this->redirectUri . http_build_query($payload)); + return $response; } foreach ($headers as $header => $content) { + /** @var ResponseInterface $response */ $response = $response->withHeader($header, $content); } diff --git a/src/ResponseTypes/RedirectResponse.php b/src/ResponseTypes/RedirectResponse.php index e4639148..f40f087b 100644 --- a/src/ResponseTypes/RedirectResponse.php +++ b/src/ResponseTypes/RedirectResponse.php @@ -35,6 +35,8 @@ class RedirectResponse extends AbstractResponseType */ public function generateHttpResponse(ResponseInterface $response) { - return $response->withStatus(302)->withHeader('Location', $this->redirectUri); + /** @var ResponseInterface $response */ + $response = $response->withStatus(302)->withHeader('Location', $this->redirectUri); + return $response; } } diff --git a/tests/AuthorizationServerTest.php b/tests/AuthorizationServerTest.php index 8b409a00..07783841 100644 --- a/tests/AuthorizationServerTest.php +++ b/tests/AuthorizationServerTest.php @@ -198,16 +198,16 @@ class AuthorizationServerTest extends TestCase $clientRepositoryMock->method('getClientEntity')->willReturn($client); $grant = new AuthCodeGrant( - $this->getMock(AuthCodeRepositoryInterface::class), - $this->getMock(RefreshTokenRepositoryInterface::class), + $this->getMockBuilder(AuthCodeRepositoryInterface::class)->getMock(), + $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(), new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $server = new AuthorizationServer( $clientRepositoryMock, - $this->getMock(AccessTokenRepositoryInterface::class), - $this->getMock(ScopeRepositoryInterface::class), + $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(), + $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(), 'file://' . __DIR__ . '/Stubs/private.key', 'file://' . __DIR__ . '/Stubs/public.key' ); diff --git a/tests/ResponseTypes/BearerResponseTypeTest.php b/tests/ResponseTypes/BearerResponseTypeTest.php index 56ae9e3e..31245b07 100644 --- a/tests/ResponseTypes/BearerResponseTypeTest.php +++ b/tests/ResponseTypes/BearerResponseTypeTest.php @@ -20,9 +20,7 @@ class BearerResponseTypeTest extends TestCase { public function testGenerateHttpResponse() { - $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); - - $responseType = new BearerTokenResponse($accessTokenRepositoryMock); + $responseType = new BearerTokenResponse(); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); @@ -64,9 +62,7 @@ class BearerResponseTypeTest extends TestCase public function testGenerateHttpResponseWithExtraParams() { - $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); - - $responseType = new BearerTokenResponseWithParams($accessTokenRepositoryMock); + $responseType = new BearerTokenResponseWithParams(); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); @@ -111,10 +107,7 @@ class BearerResponseTypeTest extends TestCase public function testDetermineAccessTokenInHeaderValidToken() { - $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); - $accessTokenRepositoryMock->method('isAccessTokenRevoked')->willReturn(false); - - $responseType = new BearerTokenResponse($accessTokenRepositoryMock); + $responseType = new BearerTokenResponse(); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); @@ -158,9 +151,8 @@ class BearerResponseTypeTest extends TestCase public function testDetermineAccessTokenInHeaderInvalidJWT() { $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); - $accessTokenRepositoryMock->method('isAccessTokenRevoked')->willReturn(false); - $responseType = new BearerTokenResponse($accessTokenRepositoryMock); + $responseType = new BearerTokenResponse(); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); @@ -247,9 +239,7 @@ class BearerResponseTypeTest extends TestCase public function testDetermineAccessTokenInHeaderInvalidToken() { - $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); - - $responseType = new BearerTokenResponse($accessTokenRepositoryMock); + $responseType = new BearerTokenResponse(); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); @@ -273,9 +263,7 @@ class BearerResponseTypeTest extends TestCase public function testDetermineMissingBearerInHeader() { - $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); - - $responseType = new BearerTokenResponse($accessTokenRepositoryMock); + $responseType = new BearerTokenResponse(); $responseType->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key')); $responseType->setEncryptionKey(base64_encode(random_bytes(36))); diff --git a/tests/CryptKeyTest.php b/tests/Utils/CryptKeyTest.php similarity index 82% rename from tests/CryptKeyTest.php rename to tests/Utils/CryptKeyTest.php index 70bbc8d7..9f3f337c 100644 --- a/tests/CryptKeyTest.php +++ b/tests/Utils/CryptKeyTest.php @@ -17,7 +17,7 @@ class CryptKeyTest extends TestCase public function testKeyCreation() { - $keyFile = __DIR__ . '/Stubs/public.key'; + $keyFile = __DIR__ . '/../Stubs/public.key'; $key = new CryptKey($keyFile, 'secret'); $this->assertEquals('file://' . $keyFile, $key->getKeyPath()); @@ -26,7 +26,7 @@ class CryptKeyTest extends TestCase public function testKeyFileCreation() { - $keyContent = file_get_contents(__DIR__ . '/Stubs/public.key'); + $keyContent = file_get_contents(__DIR__ . '/../Stubs/public.key'); $key = new CryptKey($keyContent); $this->assertEquals( @@ -34,7 +34,7 @@ class CryptKeyTest extends TestCase $key->getKeyPath() ); - $keyContent = file_get_contents(__DIR__ . '/Stubs/private.key.crlf'); + $keyContent = file_get_contents(__DIR__ . '/../Stubs/private.key.crlf'); $key = new CryptKey($keyContent); $this->assertEquals( diff --git a/tests/CryptTraitTest.php b/tests/Utils/CryptTraitTest.php similarity index 100% rename from tests/CryptTraitTest.php rename to tests/Utils/CryptTraitTest.php