aboutsummaryrefslogtreecommitdiff
path: root/tests/RouterTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/RouterTest.php')
-rw-r--r--tests/RouterTest.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/RouterTest.php b/tests/RouterTest.php
index b8d4d49..3279ede 100644
--- a/tests/RouterTest.php
+++ b/tests/RouterTest.php
@@ -8,10 +8,12 @@ use ArrayObject;
use BrettParson\MiniRoute\Routing\RouteNotFoundException;
use BrettParson\MiniRoute\Routing\RouteRegistrationException;
use BrettParson\MiniRoute\Routing\Router;
+use BrettParson\MiniRoute\Tests\Fixtures\CollidingController;
use BrettParson\MiniRoute\Tests\Fixtures\FakeController;
use BrettParson\MiniRoute\Tests\Fixtures\FakeMiddleware;
use BrettParson\MiniRoute\Tests\Fixtures\FakeResolver;
use BrettParson\MiniRoute\Tests\Fixtures\TestRequest;
+use BrettParson\MiniRoute\Tests\Fixtures\TestResponse;
use PHPUnit\Framework\TestCase;
final class RouterTest extends TestCase
@@ -78,4 +80,63 @@ final class RouterTest extends TestCase
$this->expectException(RouteNotFoundException::class);
$router->dispatch(new TestRequest('GET', '/missing'));
}
+
+ public function testNotFoundHandlerIsCalledWhenSet(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+ $router->setNotFoundHandler(static fn (): TestResponse => new TestResponse(404, 'custom-nf'));
+
+ $response = $router->dispatch(new TestRequest('GET', '/missing'));
+
+ $this->assertSame(404, $response->status());
+ $this->assertSame('custom-nf', $response->body());
+ }
+
+ public function testTrailingSlashIsNormalized(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $response = $router->dispatch(new TestRequest('GET', '/entries/'));
+
+ $this->assertSame('index', $response->body());
+ }
+
+ public function testMethodIsCaseInsensitive(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $response = $router->dispatch(new TestRequest('get', '/entries'));
+
+ $this->assertSame('index', $response->body());
+ }
+
+ public function testLeadingDoubleSlashDoesNotMatch(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $this->expectException(RouteNotFoundException::class);
+ $router->dispatch(new TestRequest('GET', '//entries'));
+ }
+
+ public function testPathsAreCaseSensitive(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $this->expectException(RouteNotFoundException::class);
+ $router->dispatch(new TestRequest('GET', '/Entries'));
+ }
+
+ public function testDuplicateRouteAcrossControllersThrows(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $this->expectException(RouteRegistrationException::class);
+ $router->registerController(CollidingController::class);
+ }
}