diff --git a/src/Middleware/Dispatcher.php b/src/Middleware/Dispatcher.php index 5d379343..61f8afc9 100644 --- a/src/Middleware/Dispatcher.php +++ b/src/Middleware/Dispatcher.php @@ -14,7 +14,7 @@ class Dispatcher implements MiddlewareInterface, RequestHandlerInterface { use ResolvesMiddlewareTrait; - protected RequestHandlerInterface $next; + protected ?RequestHandlerInterface $next = null; /** * @param MiddlewareInterface[]|string[] $stack diff --git a/src/Middleware/RequestHandler.php b/src/Middleware/RequestHandler.php index 24d1500a..1e15a972 100644 --- a/src/Middleware/RequestHandler.php +++ b/src/Middleware/RequestHandler.php @@ -6,7 +6,6 @@ use Engelsystem\Application; use Engelsystem\Controllers\BaseController; use Engelsystem\Helpers\Authenticator; use Engelsystem\Http\Exceptions\HttpForbidden; -use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -28,6 +27,7 @@ class RequestHandler implements MiddlewareInterface { $requestHandler = $request->getAttribute('route-request-handler'); + /** @var CallableHandler|MiddlewareInterface|RequestHandlerInterface $requestHandler */ $requestHandler = $this->resolveRequestHandler($requestHandler); if ($requestHandler instanceof CallableHandler) { @@ -42,11 +42,11 @@ class RequestHandler implements MiddlewareInterface return $requestHandler->process($request, $handler); } - if ($requestHandler instanceof RequestHandlerInterface) { - return $requestHandler->handle($request); - } - - throw new InvalidArgumentException('Unable to process request handler of type ' . gettype($requestHandler)); + /** + * Is RequestHandlerInterface + * @see RequestHandlerInterface + */ + return $requestHandler->handle($request); } /** diff --git a/src/Middleware/ResolvesMiddlewareTrait.php b/src/Middleware/ResolvesMiddlewareTrait.php index a220a312..c8129d2c 100644 --- a/src/Middleware/ResolvesMiddlewareTrait.php +++ b/src/Middleware/ResolvesMiddlewareTrait.php @@ -42,7 +42,7 @@ trait ResolvesMiddlewareTrait } /** - * Checks if the given object is a middleware or middleware or request handler + * Checks if the given object is a middleware or request handler */ protected function isMiddleware(mixed $middleware): bool { diff --git a/tests/Unit/Middleware/DispatcherTest.php b/tests/Unit/Middleware/DispatcherTest.php index 82acd76a..92cdebbf 100644 --- a/tests/Unit/Middleware/DispatcherTest.php +++ b/tests/Unit/Middleware/DispatcherTest.php @@ -3,8 +3,12 @@ namespace Engelsystem\Test\Unit\Middleware; use Engelsystem\Application; +use Engelsystem\Http\Request; +use Engelsystem\Http\Response; use Engelsystem\Middleware\Dispatcher; -use Error; +use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler; +use InvalidArgumentException; +use LogicException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; @@ -122,7 +126,7 @@ class DispatcherTest extends TestCase /** @var ServerRequestInterface|MockObject $request */ $request = $this->createMock(ServerRequestInterface::class); - $this->expectException(Error::class); + $this->expectException(LogicException::class); $dispatcher = new Dispatcher(); $dispatcher->handle($request); @@ -163,6 +167,17 @@ class DispatcherTest extends TestCase $dispatcher->handle($request); } + /** + * @covers \Engelsystem\Middleware\Dispatcher::handle + */ + public function testHandleCallResolveInvalidTypeResolved(): void + { + $instance = new Dispatcher([new ReturnResponseMiddlewareHandler(new Response())]); + + $this->expectException(InvalidArgumentException::class); + $instance->handle(new Request()); + } + /** * @covers \Engelsystem\Middleware\Dispatcher::setContainer */