From 7dd52c9ee6dcdba91766bfdfbece1bac3c545b1d Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Thu, 20 Feb 2025 19:31:36 -0700 Subject: [PATCH] Reset router to beginning --- flight/Engine.php | 2 +- tests/FlightAsyncTest.php | 83 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/FlightAsyncTest.php diff --git a/flight/Engine.php b/flight/Engine.php index e2c1dc3..a26c1ff 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -490,6 +490,7 @@ class Engine $this->unregister('response'); $this->register('request', Request::class); $this->register('response', Response::class); + $this->router()->reset(); } $request = $this->request(); $response = $this->response(); @@ -513,7 +514,6 @@ class Engine // Route the request $failedMiddlewareCheck = false; - while ($route = $router->route($request)) { $params = array_values($route->params); diff --git a/tests/FlightAsyncTest.php b/tests/FlightAsyncTest.php new file mode 100644 index 0000000..1a53f44 --- /dev/null +++ b/tests/FlightAsyncTest.php @@ -0,0 +1,83 @@ +expectOutputString('hello world'); + Flight::start(); + } + + public function testMultipleRoutes() + { + Flight::route('GET /', function () { + echo 'hello world'; + }); + + Flight::route('GET /test', function () { + echo 'test'; + }); + + $this->expectOutputString('test'); + $_SERVER['REQUEST_URI'] = '/test'; + Flight::start(); + } + + public function testMultipleStartsSingleRoute() + { + Flight::route('GET /', function () { + echo 'hello world'; + }); + + $this->expectOutputString('hello worldhello world'); + Flight::start(); + Flight::start(); + } + + public function testMultipleStartsMultipleRoutes() + { + Flight::route('GET /', function () { + echo 'hello world'; + }); + + Flight::route('GET /test', function () { + echo 'test'; + }); + + $this->expectOutputString('testhello world'); + $_SERVER['REQUEST_URI'] = '/test'; + Flight::start(); + $_SERVER['REQUEST_URI'] = '/'; + Flight::start(); + } +}