-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from chimeraphp/standardise-missing-route-dispa…
…tching Add standard middleware to handle missing routes
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Chimera\Routing; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
final class MissingRouteDispatching implements MiddlewareInterface | ||
{ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
throw NoRouteMatched::fromRequest($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Chimera\Routing; | ||
|
||
use Chimera\Exception; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use RuntimeException; | ||
use function sprintf; | ||
|
||
final class NoRouteMatched extends RuntimeException implements Exception | ||
{ | ||
private const HTTP_STATUS = 404; | ||
|
||
public static function fromRequest(ServerRequestInterface $request): self | ||
{ | ||
return new self(sprintf('Cannot %s %s', $request->getMethod(), $request->getUri()), self::HTTP_STATUS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Chimera\Routing\Tests; | ||
|
||
use Chimera\Routing\MissingRouteDispatching; | ||
use Chimera\Routing\NoRouteMatched; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\ServerRequest; | ||
|
||
/** | ||
* @coversDefaultClass \Chimera\Routing\MissingRouteDispatching | ||
*/ | ||
final class MissingRouteDispatchingTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @covers ::process | ||
* @covers \Chimera\Routing\NoRouteMatched::fromRequest | ||
*/ | ||
public function processShouldThrowAnExceptionWithRequestInformation(): void | ||
{ | ||
$middleware = new MissingRouteDispatching(); | ||
|
||
$this->expectException(NoRouteMatched::class); | ||
$this->expectExceptionCode(404); | ||
$this->expectExceptionMessage('Cannot GET /testing'); | ||
|
||
$middleware->process( | ||
new ServerRequest([], [], '/testing', 'GET'), | ||
$this->createMock(RequestHandlerInterface::class) | ||
); | ||
} | ||
} |