|
16 | 16 | use Psr\Http\Message\ResponseInterface; |
17 | 17 | use Psr\Http\Message\ServerRequestInterface; |
18 | 18 | use Psr\Http\Server\RequestHandlerInterface; |
| 19 | +use ReflectionProperty; |
19 | 20 |
|
20 | 21 | /** |
21 | 22 | * Tests for ControllerMiddleware routing and injection behavior. |
@@ -174,8 +175,9 @@ public function check(ServerRequestInterface $request): ResponseInterface { |
174 | 175 | /** |
175 | 176 | * Ensure controller discovery only runs once even across multiple requests. |
176 | 177 | * |
177 | | - * A temp directory is created with one controller file. Both requests match |
178 | | - * the same route, confirming discovery persisted after the first request. |
| 178 | + * After the first request triggers discovery, the route table is inspected |
| 179 | + * via reflection before and after a second request to confirm the entries |
| 180 | + * were not duplicated or cleared. |
179 | 181 | */ |
180 | 182 | public function testDiscoveryRunsOnlyOnce(): void |
181 | 183 | { |
@@ -213,9 +215,113 @@ public function index(): ResponseInterface { |
213 | 215 | $response1 = $middleware->process($request, $this->fallbackHandler()); |
214 | 216 | $this->assertSame(200, $response1->getStatusCode()); |
215 | 217 |
|
216 | | - // Second request — discovery must NOT clear and re-run (route still matched). |
| 218 | + // Capture route table size immediately after discovery. |
| 219 | + $routesProp = new ReflectionProperty($router, 'routes'); |
| 220 | + /** @var array<mixed> $routesAfterFirst */ |
| 221 | + $routesAfterFirst = $routesProp->getValue($router); |
| 222 | + $routeCountAfterFirstRequest = count($routesAfterFirst); |
| 223 | + $this->assertGreaterThan(0, $routeCountAfterFirstRequest, 'Routes must be registered after first request.'); |
| 224 | + |
| 225 | + // Second request — discovery must NOT re-run; the route table must be unchanged. |
217 | 226 | $response2 = $middleware->process($request, $this->fallbackHandler()); |
218 | 227 | $this->assertSame(200, $response2->getStatusCode()); |
| 228 | + /** @var array<mixed> $routesAfterSecond */ |
| 229 | + $routesAfterSecond = $routesProp->getValue($router); |
| 230 | + $this->assertCount( |
| 231 | + $routeCountAfterFirstRequest, |
| 232 | + $routesAfterSecond, |
| 233 | + 'Route count must not change after the second request; discoverOnce must not have re-run.', |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Ensure the original (basePath-prefixed) request is forwarded unchanged when no route matches. |
| 239 | + * |
| 240 | + * Downstream handlers such as DevPageRequestHandler rely on the full original |
| 241 | + * path (including any basePath prefix) for canonical redirects and Location headers. |
| 242 | + */ |
| 243 | + public function testProcessForwardsOriginalRequestPathOnMiss(): void |
| 244 | + { |
| 245 | + $projectRoot = $this->createProjectRoot(); |
| 246 | + file_put_contents($projectRoot . '/glaze.neon', "site:\n basePath: /app\n"); |
| 247 | + $config = BuildConfig::fromProjectRoot($projectRoot, true); |
| 248 | + |
| 249 | + $router = new ControllerRouter(); |
| 250 | + $middleware = new ControllerMiddleware( |
| 251 | + $router, |
| 252 | + $this->makeViewRenderer($config), |
| 253 | + $this->container(), |
| 254 | + $config, |
| 255 | + $this->createTempDirectory(), |
| 256 | + ); |
| 257 | + |
| 258 | + $capturedPath = null; |
| 259 | + $capturingHandler = new class ($capturedPath) implements RequestHandlerInterface { |
| 260 | + public function __construct(public ?string &$path) |
| 261 | + { |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * @inheritDoc |
| 266 | + */ |
| 267 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 268 | + { |
| 269 | + $this->path = $request->getUri()->getPath(); |
| 270 | + |
| 271 | + return (new Response(['charset' => 'UTF-8']))->withStatus(404)->withStringBody('miss'); |
| 272 | + } |
| 273 | + }; |
| 274 | + |
| 275 | + $request = (new ServerRequestFactory())->createServerRequest('GET', '/app/about/'); |
| 276 | + $middleware->process($request, $capturingHandler); |
| 277 | + |
| 278 | + $this->assertSame('/app/about/', $capturedPath, 'The original basePath-prefixed path must be forwarded unchanged.'); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Ensure path parameters are coerced to declared scalar types (int, float, bool). |
| 283 | + */ |
| 284 | + public function testProcessCoercesScalarPathParams(): void |
| 285 | + { |
| 286 | + $projectRoot = $this->createProjectRoot(); |
| 287 | + $config = BuildConfig::fromProjectRoot($projectRoot, true); |
| 288 | + |
| 289 | + $controllersDir = $projectRoot . '/controllers'; |
| 290 | + mkdir($controllersDir, 0755, true); |
| 291 | + file_put_contents($controllersDir . '/TypedController.php', <<<'PHP' |
| 292 | + <?php |
| 293 | + declare(strict_types=1); |
| 294 | + use Cake\Http\Response; |
| 295 | + use Glaze\Http\Attribute\Route; |
| 296 | + use Psr\Http\Message\ResponseInterface; |
| 297 | + final class TypedController { |
| 298 | + #[Route('/items/{id}/{score}/{active}')] |
| 299 | + public function show(int $id, float $score, bool $active): ResponseInterface { |
| 300 | + return (new Response(['charset' => 'UTF-8'])) |
| 301 | + ->withStatus(200) |
| 302 | + ->withStringBody(json_encode(['id' => $id, 'score' => $score, 'active' => $active])); |
| 303 | + } |
| 304 | + } |
| 305 | + PHP); |
| 306 | + |
| 307 | + $router = new ControllerRouter(); |
| 308 | + $middleware = new ControllerMiddleware( |
| 309 | + $router, |
| 310 | + $this->makeViewRenderer($config), |
| 311 | + $this->container(), |
| 312 | + $config, |
| 313 | + $controllersDir, |
| 314 | + ); |
| 315 | + |
| 316 | + $request = (new ServerRequestFactory())->createServerRequest('GET', '/items/42/3.14/true'); |
| 317 | + $response = $middleware->process($request, $this->fallbackHandler()); |
| 318 | + |
| 319 | + $this->assertSame(200, $response->getStatusCode()); |
| 320 | + $decoded = json_decode((string)$response->getBody(), true); |
| 321 | + $this->assertIsArray($decoded); |
| 322 | + $this->assertSame(42, $decoded['id']); |
| 323 | + $this->assertEqualsWithDelta(3.14, $decoded['score'], PHP_FLOAT_EPSILON); |
| 324 | + $this->assertTrue($decoded['active']); |
219 | 325 | } |
220 | 326 |
|
221 | 327 | /** |
|
0 commit comments