From ce753b7cd2962d7e288889526ea4ece22ef7f2f5 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Tue, 17 Mar 2026 20:30:49 -0400 Subject: [PATCH] implement Tests\Server, ServerV2 and groupcompactsyntax namespaces --- .../FlightRouteCompactSyntaxTest.php | 6 +- tests/server-v2/index.php | 409 +++++++++--------- tests/server/AuthCheck.php | 2 + tests/server/LayoutMiddleware.php | 4 + tests/server/OverwriteBodyMiddleware.php | 11 +- tests/server/Pascal_Snake_Case.php | 2 + tests/server/index.php | 72 ++- 7 files changed, 289 insertions(+), 217 deletions(-) diff --git a/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php b/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php index 611b589..84d168f 100644 --- a/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php +++ b/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); +namespace tests\groupcompactsyntax; + +use Flight; use PHPUnit\Framework\TestCase; use tests\groupcompactsyntax\PostsController; use tests\groupcompactsyntax\TodosController; use tests\groupcompactsyntax\UsersController; -require_once __DIR__ . '/UsersController.php'; -require_once __DIR__ . '/PostsController.php'; - final class FlightRouteCompactSyntaxTest extends TestCase { public function setUp(): void diff --git a/tests/server-v2/index.php b/tests/server-v2/index.php index f497fa2..4f45d99 100644 --- a/tests/server-v2/index.php +++ b/tests/server-v2/index.php @@ -9,60 +9,82 @@ declare(strict_types=1); * @author Kristaps Muižnieks https://github.com/krmu */ -require file_exists(__DIR__ . '/../../vendor/autoload.php') ? __DIR__ . '/../../vendor/autoload.php' : __DIR__ . '/../../flight/autoload.php'; - -Flight::set('flight.content_length', false); -Flight::set('flight.views.path', './'); -Flight::set('flight.views.extension', '.phtml'); -// This enables the old functionality of Flight output buffering -Flight::set('flight.v2.output_buffering', true); - -// Test 1: Root route -Flight::route('/', function () { - echo 'Route text: Root route works!'; - if (Flight::request()->query->redirected) { - echo '
Redirected from /redirect route successfully!'; +namespace Tests\ServerV2 { + class AuthCheck + { + public function before(): void + { + if (!isset($_COOKIE['user'])) { + echo 'Middleware text: You are not authorized to access this route!'; + } + } } -}); -Flight::route('/querytestpath', function () { - echo 'Route text: This ir query route
'; - echo "I got such query parameters:
";
-    print_r(Flight::request()->query);
-    echo "
"; -}, false, "querytestpath"); - -// Test 2: Simple route -Flight::route('/test', function () { - echo 'Route text: Test route works!'; -}); - -// Test 3: Route with parameter -Flight::route('/user/@name', function ($name) { - echo "Route text: Hello, $name!"; -}); -Flight::route('POST /postpage', function () { - echo 'Route text: THIS IS POST METHOD PAGE'; -}, false, "postpage"); - -// Test 4: Grouped routes -Flight::group('/group', function () { +} + +namespace { + + use Tests\ServerV2\AuthCheck; + + require_once __DIR__ . '/../phpunit_autoload.php'; + + Flight::set('flight.content_length', false); + Flight::set('flight.views.path', './'); + Flight::set('flight.views.extension', '.phtml'); + // This enables the old functionality of Flight output buffering + Flight::set('flight.v2.output_buffering', true); + + // Test 1: Root route + Flight::route('/', function () { + echo 'Route text: Root route works!'; + + if (Flight::request()->query['redirected']) { + echo '
Redirected from /redirect route successfully!'; + } + }); + + Flight::route('/querytestpath', function () { + echo 'Route text: This ir query route
'; + echo 'I got such query parameters:
';
+        print_r(Flight::request()->query);
+        echo '
'; + }, false, 'querytestpath'); + + // Test 2: Simple route Flight::route('/test', function () { - echo 'Route text: Group test route works!'; + echo 'Route text: Test route works!'; }); + + // Test 3: Route with parameter Flight::route('/user/@name', function ($name) { - echo "Route text: There is variable called name and it is $name"; + echo "Route text: Hello, $name!"; }); - Flight::group('/group1', function () { - Flight::group('/group2', function () { - Flight::group('/group3', function () { - Flight::group('/group4', function () { - Flight::group('/group5', function () { - Flight::group('/group6', function () { - Flight::group('/group7', function () { - Flight::group('/group8', function () { - Flight::route('/final_group', function () { - echo 'Mega Group test route works!'; - }, false, "final_group"); + + Flight::route('POST /postpage', function () { + echo 'Route text: THIS IS POST METHOD PAGE'; + }, false, 'postpage'); + + // Test 4: Grouped routes + Flight::group('/group', function () { + Flight::route('/test', function () { + echo 'Route text: Group test route works!'; + }); + + Flight::route('/user/@name', function ($name) { + echo "Route text: There is variable called name and it is $name"; + }); + + Flight::group('/group1', function () { + Flight::group('/group2', function () { + Flight::group('/group3', function () { + Flight::group('/group4', function () { + Flight::group('/group5', function () { + Flight::group('/group6', function () { + Flight::group('/group7', function () { + Flight::group('/group8', function () { + Flight::route('/final_group', function () { + echo 'Mega Group test route works!'; + }, false, 'final_group'); + }); }); }); }); @@ -71,152 +93,151 @@ Flight::group('/group', function () { }); }); }); -}); - -// Test 5: Route alias -Flight::route('/alias', function () { - echo 'Route text: Alias route works!'; -}, false, 'aliasroute'); -class AuthCheck -{ - public function before(): void - { - if (!isset($_COOKIE['user'])) { - echo 'Middleware text: You are not authorized to access this route!'; + + // Test 5: Route alias + Flight::route('/alias', function () { + echo 'Route text: Alias route works!'; + }, false, 'aliasroute'); + + $middle = new AuthCheck(); + + // Test 6: Route with middleware + Flight::route('/protected', function () { + echo 'Route text: Protected route works!'; + })->addMiddleware([$middle]); + + // Test 7: Route with template + Flight::route('/template/@name', function ($name) { + Flight::render('template.phtml', ['name' => $name]); + }); + + // Test 8: Throw an error + Flight::route('/error', function () { + trigger_error('This is a successful error'); + }); + + // Test 9: JSON output (should not output any other html) + Flight::route('/json', function () { + echo "\n\n\n\n\n"; + Flight::json(['message' => 'JSON renders successfully!']); + echo "\n\n\n\n\n"; + }); + + // Test 13: JSONP output (should not output any other html) + Flight::route('/jsonp', function () { + echo "\n\n\n\n\n"; + Flight::jsonp(['message' => 'JSONP renders successfully!'], 'jsonp'); + echo "\n\n\n\n\n"; + }); + + Flight::route('/json-halt', function () { + Flight::jsonHalt(['message' => 'JSON rendered and halted successfully with no other body content!']); + }); + + // Test 10: Halt + Flight::route('/halt', function () { + Flight::halt(400, 'Halt worked successfully'); + }); + + // Test 11: Redirect + Flight::route('/redirect', function () { + Flight::redirect('/?redirected=1'); + }); + + Flight::set('flight.views.path', './'); + + Flight::map('error', function (Throwable $error) { + echo "

An error occurred, mapped error method worked, error below

"; + echo '
';
+        echo str_replace(getenv('PWD'), "***CLASSIFIED*****", $error->getTraceAsString());
+        echo "
"; + echo "Go back"; + }); + + Flight::map('notFound', function () { + echo 'Route text: The requested URL was not found'; + echo "Go back"; + }); + + echo ' + -'; -Flight::before('start', function ($params) { - echo '
'; -}); -Flight::after('start', function ($params) { - echo '
'; - echo '
'; - echo "Request information
";
-    print_r(Flight::request());
-    echo "
"; - echo "
"; -}); -Flight::start(); + li a:hover { + background-color: #111; + } + #container { + color: #333; + font-size: 16px; + line-height: 1.5; + margin: 20px 0; + padding: 10px; + border: 1px solid #ddd; + background-color: #f9f9f9; + } + #debugrequest { + color: #333; + font-size: 16px; + line-height: 1.5; + margin: 20px 0; + padding: 10px; + border: 1px solid #ddd; + background-color: #f9f9f9; + } + + '; + + Flight::before('start', function () { + echo '
'; + }); + + Flight::after('start', function () { + echo '
'; + echo '
'; + echo "Request information
";
+        print_r(Flight::request());
+        echo "
"; + echo "
"; + }); + + Flight::start(); +} diff --git a/tests/server/AuthCheck.php b/tests/server/AuthCheck.php index 78c87aa..79e8f67 100644 --- a/tests/server/AuthCheck.php +++ b/tests/server/AuthCheck.php @@ -2,6 +2,8 @@ declare(strict_types=1); +namespace Tests\Server; + class AuthCheck { public function before(): void diff --git a/tests/server/LayoutMiddleware.php b/tests/server/LayoutMiddleware.php index 32d6a06..150985b 100644 --- a/tests/server/LayoutMiddleware.php +++ b/tests/server/LayoutMiddleware.php @@ -2,6 +2,10 @@ declare(strict_types=1); +namespace Tests\Server; + +use Flight; + class LayoutMiddleware { public function before(): void diff --git a/tests/server/OverwriteBodyMiddleware.php b/tests/server/OverwriteBodyMiddleware.php index f7ada99..98e8468 100644 --- a/tests/server/OverwriteBodyMiddleware.php +++ b/tests/server/OverwriteBodyMiddleware.php @@ -2,11 +2,20 @@ declare(strict_types=1); +namespace Tests\Server; + +use Flight; + class OverwriteBodyMiddleware { public function after(): void { $response = Flight::response(); - $response->write(str_replace('failed', 'successfully works!', $response->getBody()), true); + + $response->write(str_replace( + 'failed', + 'successfully works!', + $response->getBody() + ), true); } } diff --git a/tests/server/Pascal_Snake_Case.php b/tests/server/Pascal_Snake_Case.php index bbba0d2..d80ed41 100644 --- a/tests/server/Pascal_Snake_Case.php +++ b/tests/server/Pascal_Snake_Case.php @@ -2,6 +2,8 @@ declare(strict_types=1); +namespace Tests\Server; + class Pascal_Snake_Case // phpcs:ignore { public function doILoad() // phpcs:ignore diff --git a/tests/server/index.php b/tests/server/index.php index 5fd7a66..6bb9dca 100644 --- a/tests/server/index.php +++ b/tests/server/index.php @@ -2,10 +2,15 @@ declare(strict_types=1); +use Dice\Dice; use flight\core\Loader; use flight\database\PdoWrapper; use tests\classes\Container; use tests\classes\ContainerDefault; +use Tests\Server\AuthCheck; +use Tests\Server\LayoutMiddleware; +use Tests\Server\OverwriteBodyMiddleware; +use Tests\Server\Pascal_Snake_Case; /* * This is the test file where we can open up a quick test server and make @@ -14,7 +19,7 @@ use tests\classes\ContainerDefault; * @author Kristaps Muižnieks https://github.com/krmu */ -require file_exists(__DIR__ . '/../../vendor/autoload.php') ? __DIR__ . '/../../vendor/autoload.php' : __DIR__ . '/../../flight/autoload.php'; +require_once __DIR__ . '/../phpunit_autoload.php'; Flight::set('flight.content_length', false); Flight::set('flight.views.path', './'); @@ -23,20 +28,20 @@ Loader::setV2ClassLoading(false); Flight::path(__DIR__); Flight::group('', function () { - // Test 1: Root route Flight::route('/', function () { echo 'Route text: Root route works!'; - if (Flight::request()->query->redirected) { + if (Flight::request()->query['redirected']) { echo '
Redirected from /redirect route successfully!'; } }); + Flight::route('/querytestpath', function () { echo 'Route text: This is query route
'; - echo "Query parameters:
";
+        echo 'Query parameters:
';
         print_r(Flight::request()->query);
-        echo "
"; - }, false, "querytestpath"); + echo '
'; + }, false, 'querytestpath'); // Test 2: Simple route Flight::route('/test', function () { @@ -47,18 +52,21 @@ Flight::group('', function () { Flight::route('/user/@name', function ($name) { echo "Route text: Hello, $name!"; }); + Flight::route('POST /postpage', function () { echo 'Route text: THIS IS POST METHOD PAGE'; - }, false, "postpage"); + }, false, 'postpage'); // Test 4: Grouped routes Flight::group('/group', function () { Flight::route('/test', function () { echo 'Route text: Group test route works!'; }); + Flight::route('/user/@name', function ($name) { echo "Route text: There is variable called name and it is $name"; }); + Flight::group('/group1', function () { Flight::group('/group2', function () { Flight::group('/group3', function () { @@ -69,7 +77,7 @@ Flight::group('', function () { Flight::group('/group8', function () { Flight::route('/final_group', function () { echo 'Mega Group test route works!'; - }, false, "final_group"); + }, false, 'final_group'); }); }); }); @@ -86,8 +94,8 @@ Flight::group('', function () { }, false, 'aliasroute'); /** Middleware test */ - include_once 'AuthCheck.php'; $middle = new AuthCheck(); + // Test 6: Route with middleware Flight::route('/protected', function () { echo 'Route text: Protected route works!'; @@ -119,44 +127,68 @@ Flight::group('', function () { // Test 12: Redirect with status code Flight::route('/streamResponse', function () { - echo "Streaming a response"; + echo 'Streaming a response'; + for ($i = 1; $i <= 50; $i++) { echo "."; usleep(50000); ob_flush(); } - echo "is successful!!"; + + echo 'is successful!!'; })->stream(); // Test 12: Redirect with status code Flight::route('/streamWithHeaders', function () { - echo "Streaming a response"; + echo 'Streaming a response'; + for ($i = 1; $i <= 50; $i++) { echo "."; usleep(50000); ob_flush(); } - echo "is successful!!"; + + echo 'is successful!!'; })->streamWithHeaders(['Content-Type' => 'text/html', 'status' => 200]); // Test 14: Overwrite the body with a middleware Flight::route('/overwrite', function () { - echo 'Route text: This route status is that it failed'; + echo <<<'html' + Route text: + This route status is that it + failed + html; })->addMiddleware([new OverwriteBodyMiddleware()]); // Test 15: UTF8 Chars in url Flight::route('/わたしはひとです', function () { - echo 'Route text: This route status is that it succeeded はい!!!'; + echo <<<'html' + Route text: + This route status is that it + succeeded はい!!! + html; }); // Test 16: UTF8 Chars in url with utf8 params Flight::route('/わたしはひとです/@name', function ($name) { - echo 'Route text: This route status is that it ' . ($name === 'ええ' ? 'succeeded' : 'failed') . ' URL Param: ' . $name . ''; + echo 'Route text: This route status is that it ' + . ($name === 'ええ' ? 'succeeded' : 'failed') + . ' URL Param: ' + . $name + . ''; }); // Test 17: Slash in param Flight::route('/redirect/@id', function ($id) { - echo 'Route text: This route status is that it ' . ($id === 'before/after' ? 'succeeded' : 'failed') . ' URL Param: ' . $id . ''; + echo 'Route text: This route status is that it ' + . ($id === 'before/after' ? 'succeeded' : 'failed') + . ' URL Param: ' + . $id + . ''; }); Flight::set('test_me_out', 'You got it boss!'); // used in /no-container route @@ -195,23 +227,25 @@ Flight::map('error', function (Throwable $e) { $e->getCode(), str_replace(getenv('PWD'), '***CONFIDENTIAL***', $e->getTraceAsString()) ); + echo "
Go back"; }); + Flight::map('notFound', function () { echo 'Route text: The requested URL was not found
'; echo "Go back"; }); Flight::map('start', function () { - if (Flight::request()->url === '/dice') { - $dice = new \Dice\Dice(); + $dice = new Dice(); $dice = $dice->addRules([ PdoWrapper::class => [ 'shared' => true, 'constructParams' => ['sqlite::memory:'] ] ]); + Flight::registerContainerHandler(function ($class, $params) use ($dice) { return $dice->create($class, $params); });