From 2e975984efd00246c7ef11a1db029285f7d055d3 Mon Sep 17 00:00:00 2001 From: Andrej Rypo Date: Sat, 7 Nov 2020 09:20:37 +0100 Subject: [PATCH] Improved coverage + dedicated tests for generics + typo fix --- src/Factory/AuthFactory.php | 2 +- tests/FactoryTest.phpt | 14 ++++ tests/GenericHandlerTest.phpt | 94 ++++++++++++++++++++++++ tests/GenericMiddlewareTest.phpt | 118 +++++++++++++++++++++++++++++++ tests/ManipulatorsTest.phpt | 42 ----------- tests/NoLibTest.phpt | 27 +++++++ 6 files changed, 254 insertions(+), 43 deletions(-) create mode 100644 tests/GenericHandlerTest.phpt create mode 100644 tests/GenericMiddlewareTest.phpt delete mode 100644 tests/ManipulatorsTest.phpt create mode 100644 tests/NoLibTest.phpt diff --git a/src/Factory/AuthFactory.php b/src/Factory/AuthFactory.php index 9e28836..7d3b2e4 100644 --- a/src/Factory/AuthFactory.php +++ b/src/Factory/AuthFactory.php @@ -172,7 +172,7 @@ public static function defaultDecoderFactory(string $secret): callable if (!class_exists(JWT::class)) { throw new LogicException( 'Firebase JWT is not installed. ' . - 'Require require firebase/php-jwt package (`composer require firebase/php-jwt:"^5.0"`).' + 'Requires firebase/php-jwt package (`composer require firebase/php-jwt:"^5.0"`).' ); } return fn(): FirebaseJwtDecoder => new FirebaseJwtDecoder($secret); diff --git a/tests/FactoryTest.phpt b/tests/FactoryTest.phpt index bae8c2e..68e0029 100644 --- a/tests/FactoryTest.phpt +++ b/tests/FactoryTest.phpt @@ -10,6 +10,7 @@ use Dakujem\Middleware\Factory\AuthFactory; use Dakujem\Middleware\Factory\AuthWizard; use Dakujem\Middleware\GenericMiddleware; use Dakujem\Middleware\TokenMiddleware; +use LogicException; use Slim\Psr7\Factory\ResponseFactory; use Tester\Assert; use Tester\TestCase; @@ -40,6 +41,19 @@ class _FactoryTest extends TestCase Assert::type(GenericMiddleware::class, AuthWizard::assertTokens($rf)); Assert::type(GenericMiddleware::class, AuthWizard::inspectTokens($rf, fn() => null)); } + + public function testThrows() + { + // no secret key for decoding + Assert::throws(fn() => AuthWizard::factory(null, new ResponseFactory()) + ->decodeTokens(), LogicException::class); + + // no response factory for 401 responses + Assert::throws(fn() => AuthWizard::factory('a secret key what', null) + ->assertTokens(), LogicException::class); + Assert::throws(fn() => AuthWizard::factory('a secret key what', null) + ->inspectTokens(fn() => null), LogicException::class); + } } // run the test diff --git a/tests/GenericHandlerTest.phpt b/tests/GenericHandlerTest.phpt new file mode 100644 index 0000000..05aa4ac --- /dev/null +++ b/tests/GenericHandlerTest.phpt @@ -0,0 +1,94 @@ + + */ +class _GenericHandlerTest extends TestCase +{ + public function testInterface() + { + $check = false; + $testee = new GenericHandler(function ($request) use (&$check) { + $check = true; + + // assert correct arguments + Assert::type(ServerRequestInterface::class, $request); + + return (new ResponseFactory())->createResponse(); + }); + Assert::type(RequestHandlerInterface::class, $testee); + + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee->handle((new RequestFactory())->createRequest('GET', '/')); + Assert::true($check); + } + + public function testHandlerDirectCall() + { + $check = false; + $testee = new GenericHandler(function ($request) use (&$check) { + $check = true; + + // assert correct arguments + Assert::type(ServerRequestInterface::class, $request); + + return (new ResponseFactory())->createResponse(); + }); + + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee((new RequestFactory())->createRequest('GET', '/')); + Assert::true($check); + } + + public function testHandlerWrapper() + { + $check = false; + $testee = TokenManipulators::callableToHandler(function () use (&$check) { + $check = true; + return (new ResponseFactory())->createResponse(); + }); + Assert::type(GenericHandler::class, $testee); + Assert::type(RequestHandlerInterface::class, $testee); + + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee->handle((new RequestFactory())->createRequest('GET', '/')); + Assert::true($check); + } + + /** @noinspection PhpParamsInspection */ + public function testHandlerMustReturnAResponse() + { + $req = (new RequestFactory())->createRequest('GET', '/'); + Assert::throws(fn() => (new GenericHandler(fn() => null))->handle($req), TypeError::class); + Assert::throws(fn() => (new GenericHandler(fn() => 42))->handle($req), TypeError::class); + Assert::throws(fn() => (new GenericHandler(fn() => 'hello world'))->handle($req), TypeError::class); + Assert::throws(fn() => (new GenericHandler(fn() => new AuthWizard()))->handle($req), TypeError::class); + } +} + +// run the test +(new _GenericHandlerTest)->run(); diff --git a/tests/GenericMiddlewareTest.phpt b/tests/GenericMiddlewareTest.phpt new file mode 100644 index 0000000..9051eee --- /dev/null +++ b/tests/GenericMiddlewareTest.phpt @@ -0,0 +1,118 @@ + + */ +class _GenericMiddlewareTest extends TestCase +{ + public function testInterface() + { + $check = false; + $testee = new GenericMiddleware(function ($request, $next) use (&$check) { + $check = true; + + // assert correct arguments + Assert::type(ServerRequestInterface::class, $request); + Assert::type(RequestHandlerInterface::class, $next); + + return $next->handle($request); + }); + Assert::type(MiddlewareInterface::class, $testee); + + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee->process( + (new RequestFactory())->createRequest('GET', '/'), + new GenericHandler(fn() => (new ResponseFactory())->createResponse()) + ); + Assert::true($check); + } + + public function testDirectCall() + { + $check = false; + $testee = new GenericMiddleware(function ($request, $next) use (&$check) { + $check = true; + + // assert correct arguments + Assert::type(ServerRequestInterface::class, $request); + Assert::type(RequestHandlerInterface::class, $next); + + return $next->handle($request); + }); + + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee( + (new RequestFactory())->createRequest('GET', '/'), + new GenericHandler(fn() => (new ResponseFactory())->createResponse()) + ); + Assert::true($check); + + $check = false; + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee( + (new RequestFactory())->createRequest('GET', '/'), + fn() => (new ResponseFactory())->createResponse() // assert the handler is created on the fly + ); + Assert::true($check); + } + + public function testWrapper() + { + $check = false; + $testee = TokenManipulators::callableToMiddleware(function ($request, $next) use (&$check) { + $check = true; + return $next->handle($request); + }); + Assert::type(GenericMiddleware::class, $testee); + Assert::type(MiddlewareInterface::class, $testee); + + Assert::false($check); + /** @noinspection PhpParamsInspection */ + $testee->process( + (new RequestFactory())->createRequest('GET', '/'), + new GenericHandler(fn() => (new ResponseFactory())->createResponse()) + ); + Assert::true($check); + } + + /** @noinspection PhpParamsInspection */ + public function testMiddlewareMustReturnAResponse() + { + $req = (new RequestFactory())->createRequest('GET', '/'); + $next = new GenericHandler(fn() => (new ResponseFactory())->createResponse()); + Assert::throws(fn() => (new GenericMiddleware(fn() => null))->process($req, $next), TypeError::class); + Assert::throws(fn() => (new GenericMiddleware(fn() => 42))->process($req, $next), TypeError::class); + Assert::throws(fn() => (new GenericMiddleware(fn() => 'hello world'))->process($req, $next), TypeError::class); + Assert::throws(fn() => (new GenericMiddleware(fn() => new AuthWizard())) + ->process($req, $next), TypeError::class); + } +} + +// run the test +(new _GenericMiddlewareTest)->run(); diff --git a/tests/ManipulatorsTest.phpt b/tests/ManipulatorsTest.phpt deleted file mode 100644 index 67892ae..0000000 --- a/tests/ManipulatorsTest.phpt +++ /dev/null @@ -1,42 +0,0 @@ - - */ -class _ManipulatorsTest extends TestCase -{ - public function testHandlerWrapper() - { - $check = false; - $handler = TokenManipulators::callableToHandler(function () use (&$check) { - $check = true; - return (new ResponseFactory())->createResponse(); - }); - Assert::type(RequestHandlerInterface::class, $handler); - - Assert::false($check); - /** @noinspection PhpParamsInspection */ - $handler->handle((new RequestFactory())->createRequest('GET', '/')); - Assert::true($check); - } -} - -// run the test -(new _ManipulatorsTest)->run(); diff --git a/tests/NoLibTest.phpt b/tests/NoLibTest.phpt new file mode 100644 index 0000000..6d0081c --- /dev/null +++ b/tests/NoLibTest.phpt @@ -0,0 +1,27 @@ + + */ + +Assert::throws( + fn() => AuthFactory::defaultDecoderFactory('doesntmatter'), + LogicException::class, + 'Firebase JWT is not installed. Requires firebase/php-jwt package (`composer require firebase/php-jwt:"^5.0"`).' +);