From f244ca45af7d8da6cd0fcb008546393b48b9a588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Fri, 21 Jun 2019 17:46:42 +0200 Subject: [PATCH] Add standard middleware to handle missing routes Each framework offers a different request handler or middleware to be used as last item in the middleware pipeline, which is called when no route has been matched by the router. This allows us to delegate the formatting of such error to an error handling middleware, granting more flexibility to people. --- src/MissingRouteDispatching.php | 17 +++++++++++++ src/NoRouteMatched.php | 19 ++++++++++++++ tests/MissingRouteDispatchingTest.php | 36 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 src/MissingRouteDispatching.php create mode 100644 src/NoRouteMatched.php create mode 100644 tests/MissingRouteDispatchingTest.php diff --git a/src/MissingRouteDispatching.php b/src/MissingRouteDispatching.php new file mode 100644 index 0000000..e119025 --- /dev/null +++ b/src/MissingRouteDispatching.php @@ -0,0 +1,17 @@ +getMethod(), $request->getUri()), self::HTTP_STATUS); + } +} diff --git a/tests/MissingRouteDispatchingTest.php b/tests/MissingRouteDispatchingTest.php new file mode 100644 index 0000000..399b349 --- /dev/null +++ b/tests/MissingRouteDispatchingTest.php @@ -0,0 +1,36 @@ +expectException(NoRouteMatched::class); + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Cannot GET /testing'); + + $middleware->process( + new ServerRequest([], [], '/testing', 'GET'), + $this->createMock(RequestHandlerInterface::class) + ); + } +}