router = new Router(); // Ensure clean session state $_SESSION = []; } protected function tearDown(): void { $_SESSION = []; } // ── addRoute() ─────────────────────────────────────────────────────────── public function test_addRoute_stores_handler(): void { $handler = $this->createMock(RequestHandlerInterface::class); $this->router->addRoute('list', $handler); $reflection = new ReflectionClass($this->router); $routesProp = $reflection->getProperty('routes'); $routesProp->setAccessible(true); $routes = $routesProp->getValue($this->router); $this->assertArrayHasKey('list', $routes); $this->assertSame($handler, $routes['list']); } public function test_addRoute_overwrites_existing_handler(): void { $handler1 = $this->createMock(RequestHandlerInterface::class); $handler2 = $this->createMock(RequestHandlerInterface::class); $this->router->addRoute('list', $handler1); $this->router->addRoute('list', $handler2); $reflection = new ReflectionClass($this->router); $routesProp = $reflection->getProperty('routes'); $routesProp->setAccessible(true); $routes = $routesProp->getValue($this->router); $this->assertCount(1, $routes); $this->assertSame($handler2, $routes['list']); } // ── addMiddleware() ────────────────────────────────────────────────────── public function test_addMiddleware_stores_middleware(): void { $middleware = $this->createMock(MiddlewareInterface::class); $this->router->addMiddleware($middleware); $reflection = new ReflectionClass($this->router); $mwProp = $reflection->getProperty('middleware'); $mwProp->setAccessible(true); $middlewares = $mwProp->getValue($this->router); $this->assertCount(1, $middlewares); $this->assertSame($middleware, $middlewares[0]); } public function test_addMiddleware_accumulates(): void { $mw1 = $this->createMock(MiddlewareInterface::class); $mw2 = $this->createMock(MiddlewareInterface::class); $this->router->addMiddleware($mw1); $this->router->addMiddleware($mw2); $reflection = new ReflectionClass($this->router); $mwProp = $reflection->getProperty('middleware'); $mwProp->setAccessible(true); $middlewares = $mwProp->getValue($this->router); $this->assertCount(2, $middlewares); $this->assertSame($mw1, $middlewares[0]); $this->assertSame($mw2, $middlewares[1]); } // ── dispatchResponse() — unknown op ───────────────────────────────────── public function test_dispatchResponse_unknown_op_returns_404(): void { $response = $this->router->dispatchResponse('nonexistent'); $body = json_decode($response->getBody(), true); $this->assertArrayHasKey('error', $body); $this->assertEquals('Method not found', $body['error']); } // ── dispatchResponse() — AuthRequired handler ──────────────────────────── public function test_dispatchResponse_auth_required_without_session_returns_401(): void { $handler = new class implements RequestHandlerInterface, AuthRequired { public bool $handleCalled = false; public function handle(Request $request): Response { $this->handleCalled = true; return new Response(); } }; $this->router->addRoute('protected', $handler); $response = $this->router->dispatchResponse('protected'); $body = json_decode($response->getBody(), true); $this->assertArrayHasKey('error', $body); $this->assertEquals('Unauthorized', $body['error']); $this->assertFalse($handler->handleCalled); } public function test_dispatchResponse_auth_required_with_session_calls_handler(): void { $expectedResponse = new Response(); $handler = new class($expectedResponse) implements RequestHandlerInterface, AuthRequired { public bool $handleCalled = false; private readonly Response $expectedResponse; public function __construct(Response $expectedResponse) { $this->expectedResponse = $expectedResponse; } public function handle(Request $request): Response { $this->handleCalled = true; return $this->expectedResponse; } }; $this->router->addRoute('protected', $handler); $_SESSION['owner'] = 'testuser'; $_SESSION['pass_hash'] = 'dummy'; $response = $this->router->dispatchResponse('protected'); $this->assertTrue($handler->handleCalled); $this->assertSame($expectedResponse, $response); } // ── dispatchResponse() — non-AuthRequired handler ──────────────────────── public function test_dispatchResponse_non_auth_handler_without_session_calls_handler(): void { // HealthzHandler equivalent: a handler that does NOT implement AuthRequired $handler = $this->createMock(RequestHandlerInterface::class); $expectedResponse = new Response(); $handler->expects($this->once()) ->method('handle') ->willReturn($expectedResponse); $this->router->addRoute('healthz', $handler); // No session set $response = $this->router->dispatchResponse('healthz'); $this->assertSame($expectedResponse, $response); } public function test_dispatchResponse_non_auth_handler_with_session_calls_handler(): void { $handler = $this->createMock(RequestHandlerInterface::class); $expectedResponse = new Response(); $handler->expects($this->once()) ->method('handle') ->willReturn($expectedResponse); $this->router->addRoute('healthz', $handler); $_SESSION['owner'] = 'testuser'; $response = $this->router->dispatchResponse('healthz'); $this->assertSame($expectedResponse, $response); } // ── dispatchResponse() — middleware wrapping ───────────────────────────── public function test_dispatchResponse_middleware_wraps_handler(): void { $handler = $this->createMock(RequestHandlerInterface::class); $handlerResponse = new Response(); $handler->expects($this->once()) ->method('handle') ->willReturn($handlerResponse); // Middleware that adds a header $middleware = new class implements MiddlewareInterface { public bool $wasCalled = false; public function process(Request $request, callable $next): Response { $this->wasCalled = true; $response = $next($request); return $response->withHeader('X-Middleware', 'applied'); } }; $this->router->addRoute('test', $handler); $this->router->addMiddleware($middleware); $response = $this->router->dispatchResponse('test'); $this->assertTrue($middleware->wasCalled); // Middleware response should be returned (handler response was wrapped) // The middleware returns its own response with the header } public function test_dispatchResponse_middleware_last_added_is_outermost(): void { $handler = $this->createMock(RequestHandlerInterface::class); $handlerResponse = new Response(); $handler->expects($this->once()) ->method('handle') ->willReturn($handlerResponse); // Use a shared array to track execution order $tracker = new class { /** @var list */ public array $order = []; }; // Middleware 1: added first → inner $mw1 = new class($tracker) implements MiddlewareInterface { public function __construct(private readonly object $tracker) { } public function process(Request $request, callable $next): Response { $this->tracker->order[] = 'mw1_start'; $response = $next($request); $this->tracker->order[] = 'mw1_end'; return $response; } }; // Middleware 2: added second → outer $mw2 = new class($tracker) implements MiddlewareInterface { public function __construct(private readonly object $tracker) { } public function process(Request $request, callable $next): Response { $this->tracker->order[] = 'mw2_start'; $response = $next($request); $this->tracker->order[] = 'mw2_end'; return $response; } }; $this->router->addRoute('test', $handler); $this->router->addMiddleware($mw1); // added first = inner $this->router->addMiddleware($mw2); // added second = outer $this->router->dispatchResponse('test'); // Execution order: first-added middleware is outermost // mw1 (added first) wraps mw2 (added second) which wraps handler $this->assertEquals([ 'mw1_start', 'mw2_start', 'mw2_end', 'mw1_end', ], $tracker->order); } // ── dispatchResponse() — request attribute ─────────────────────────────── public function test_dispatchResponse_sets_op_attribute_on_request(): void { // Use a shared tracker object to capture the request $tracker = new class { public ?Request $request = null; }; $handler = new class($tracker) implements RequestHandlerInterface { public function __construct(private readonly object $tracker) { } public function handle(Request $request): Response { $this->tracker->request = $request; return new Response(); } }; $this->router->addRoute('list', $handler); $this->router->dispatchResponse('list'); $this->assertNotNull($tracker->request); $this->assertEquals('list', $tracker->request->getAttribute('op')); } // ── dispatch() — integration with exit ─────────────────────────────────── public function test_dispatch_calls_exit_for_unknown_op(): void { // Since dispatch() calls exit, we can only verify behavior via dispatchResponse // which is the extracted testable method $response = $this->router->dispatchResponse('unknown'); $body = json_decode($response->getBody(), true); $this->assertEquals('Method not found', $body['error']); } public function test_dispatch_calls_exit_for_unauthorized(): void { $handler = new class implements RequestHandlerInterface, AuthRequired { public bool $handleCalled = false; public function handle(Request $request): Response { $this->handleCalled = true; return new Response(); } }; $this->router->addRoute('protected', $handler); $response = $this->router->dispatchResponse('protected'); $body = json_decode($response->getBody(), true); $this->assertEquals('Unauthorized', $body['error']); $this->assertFalse($handler->handleCalled); } }