aboutsummaryrefslogtreecommitdiff
path: root/tests/RouterTest.php
diff options
context:
space:
mode:
authorBrett Parson <brett@brett-parson.com>2026-08-21 13:55:37 -0500
committerBrett Parson <brett@brett-parson.com>2026-08-21 14:00:11 -0500
commitd039a5d62a9ac6ababae70d9d96422ca59f2b2d4 (patch)
treef0db4750a571e8040a5da02cc7190079c0d62914 /tests/RouterTest.php
parent0de77a6334cee1f6e15f7e0fd35602514560be33 (diff)
downloadminiroute-d039a5d62a9ac6ababae70d9d96422ca59f2b2d4.tar.gz
miniroute-d039a5d62a9ac6ababae70d9d96422ca59f2b2d4.zip
Scaffold miniroute routing/middleware kernel
Diffstat (limited to 'tests/RouterTest.php')
-rw-r--r--tests/RouterTest.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/RouterTest.php b/tests/RouterTest.php
new file mode 100644
index 0000000..b8d4d49
--- /dev/null
+++ b/tests/RouterTest.php
@@ -0,0 +1,81 @@
+<?php
+
+declare(strict_types=1);
+
+namespace BrettParson\MiniRoute\Tests;
+
+use ArrayObject;
+use BrettParson\MiniRoute\Routing\RouteNotFoundException;
+use BrettParson\MiniRoute\Routing\RouteRegistrationException;
+use BrettParson\MiniRoute\Routing\Router;
+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 PHPUnit\Framework\TestCase;
+
+final class RouterTest extends TestCase
+{
+ /**
+ * @return array{Router, FakeResolver, ArrayObject}
+ */
+ private function makeRouter(): array
+ {
+ $resolver = new FakeResolver();
+ $resolver->bind(FakeController::class, new FakeController());
+
+ $log = new ArrayObject();
+ $resolver->bind(FakeMiddleware::class, new FakeMiddleware('m', $log));
+
+ return [new Router($resolver), $resolver, $log];
+ }
+
+ public function testDispatchesToController(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $response = $router->dispatch(new TestRequest('GET', '/entries'));
+
+ $this->assertSame(200, $response->status());
+ $this->assertSame('index', $response->body());
+ }
+
+ public function testInjectsRouteParameters(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $response = $router->dispatch(new TestRequest('GET', '/entries/hello-world'));
+
+ $this->assertSame('show:hello-world', $response->body());
+ }
+
+ public function testAppliesRouteMiddleware(): void
+ {
+ [$router, , $log] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $router->dispatch(new TestRequest('POST', '/entries'));
+
+ $this->assertSame(['m'], $log->getArrayCopy());
+ }
+
+ public function testDuplicateRouteThrows(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $this->expectException(RouteRegistrationException::class);
+ $router->registerController(FakeController::class);
+ }
+
+ public function testNotFoundThrowsWithoutHandler(): void
+ {
+ [$router] = $this->makeRouter();
+ $router->registerController(FakeController::class);
+
+ $this->expectException(RouteNotFoundException::class);
+ $router->dispatch(new TestRequest('GET', '/missing'));
+ }
+}