diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 18b1f72..0dd7632 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -48,14 +48,16 @@ public function dispatch(ServerRequestInterface $request): DispatchInterface (string) message( 'No route found for `%uri%`', uri: $uri, - ) + ), + 404 ), GroupCountBased::METHOD_NOT_ALLOWED => throw new MethodNotAllowedException( (string) message( 'Method `%method%` is not in the list of allowed methods: `%allowed%`', method: $method, allowed: implode(', ', $format[1]), - ) + ), + 405 ), // @codeCoverageIgnoreStart default => throw new LogicException( diff --git a/src/functions.php b/src/functions.php index 47538f3..a3f3c8c 100644 --- a/src/functions.php +++ b/src/functions.php @@ -157,7 +157,8 @@ function route( 'Unknown HTTP method `%provided%` provided for `%controller%` controller.', provided: $httpMethod, controller: $controllerName->__toString(), - ) + ), + 405 ); } /** @var MethodInterface $object */ @@ -236,6 +237,8 @@ function controllerName(BindInterface|string $item): ControllerNameInterface * Executes the request on router. * * @param array $container Service container (name => instance). + * + * @throws NotFoundException|MethodNotAllowedException */ function routed( ServerRequestInterface $serverRequest, @@ -244,17 +247,7 @@ function routed( array $container = [], ): RoutedInterface { $container['responseFactory'] = $responseFactory; - - try { - $routed = $router->dispatcher()->dispatch($serverRequest); - } catch (NotFoundException|MethodNotAllowedException $e) { - $code = $e instanceof MethodNotAllowedException ? 405 : 404; - - return (new Routed( - $responseFactory->createResponse($code), - bind(NullController::class), - ))->withThrowable($e); - } + $routed = $router->dispatcher()->dispatch($serverRequest); $queue = []; $middlewares = $routed->bind()->middlewares(); foreach ($middlewares as $middlewareName) { diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index c5fe817..ec58980 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -13,7 +13,6 @@ namespace Chevere\Tests; -use Chevere\Http\Controllers\NullController; use Chevere\Http\Exceptions\MethodNotAllowedException; use Chevere\Router\Exceptions\NotFoundException; use Chevere\Router\Exceptions\VariableInvalidException; @@ -245,13 +244,9 @@ public function testRoutedNull(string $method, string $path, int $code, string $ route('/foo', PATCH: ControllerNoParameters::class) ) ); - $routed = routed($request, $router); - $this->assertSame($code, $routed->response()->getStatusCode()); - $this->assertInstanceOf($exception, $routed->throwable()); - $this->assertSame($reason, $routed->throwable()->getMessage()); - $this->assertSame( - NullController::class, - $routed->bind()->controllerName()->__toString() - ); + $this->expectException($exception); + $this->expectExceptionMessage($reason); + $this->expectExceptionCode($code); + routed($request, $router); } }