From d039a5d62a9ac6ababae70d9d96422ca59f2b2d4 Mon Sep 17 00:00:00 2001 From: Brett Parson Date: Fri, 21 Aug 2026 13:55:37 -0500 Subject: Scaffold miniroute routing/middleware kernel --- tests/RouterTest.php | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/RouterTest.php (limited to 'tests/RouterTest.php') 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 @@ +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')); + } +} -- cgit v1.2.3