From 85d2f82f771e6d5d4187e1bcf56053f6ad0ea41b Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Tue, 23 Jun 2026 12:48:02 -0400 Subject: [PATCH] autoload reworked - removed autoload-dev.classmap - normalize Tests/* namespace to tests - rename tests/server-v2 to tests/server_v2 - remove tests/phpunit_autoload.php - update tests files namespaces - update tests/server/index.php and tests/server_v2/index.php to use composer autoloader - extract tests/server_v2/AuthCheck.php --- composer.json | 12 +- phpunit.xml.dist | 3 +- tests/EventSystemTest.php | 2 +- .../ExampleClass.php | 2 + .../FlightTest.php | 3 +- tests/phpunit_autoload.php | 9 - tests/server-v2/index.php | 243 ------------------ tests/server/AuthCheck.php | 2 +- tests/server/LayoutMiddleware.php | 2 +- tests/server/OverwriteBodyMiddleware.php | 2 +- tests/server/Pascal_Snake_Case.php | 2 +- tests/server/index.php | 12 +- tests/server_v2/AuthCheck.php | 15 ++ tests/server_v2/index.php | 228 ++++++++++++++++ tests/{server-v2 => server_v2}/template.phtml | 0 15 files changed, 260 insertions(+), 277 deletions(-) rename tests/{named-arguments => named_arguments}/ExampleClass.php (78%) rename tests/{named-arguments => named_arguments}/FlightTest.php (98%) delete mode 100644 tests/phpunit_autoload.php delete mode 100644 tests/server-v2/index.php create mode 100644 tests/server_v2/AuthCheck.php create mode 100644 tests/server_v2/index.php rename tests/{server-v2 => server_v2}/template.phtml (100%) diff --git a/composer.json b/composer.json index 70a09ca..19e57f5 100644 --- a/composer.json +++ b/composer.json @@ -32,16 +32,8 @@ ] }, "autoload-dev": { - "classmap": [ - "tests/classes/" - ], "psr-4": { - "Tests\\PHP8\\": [ - "tests/named-arguments" - ], - "Tests\\Server\\": "tests/server", - "Tests\\ServerV2\\": "tests/server-v2", - "tests\\groupcompactsyntax\\": "tests/groupcompactsyntax" + "tests\\": "tests" } }, "require-dev": { @@ -83,7 +75,7 @@ ], "test-server-v2": [ "echo \"Running Test Server\"", - "@php -S localhost:8000 -t tests/server-v2" + "@php -S localhost:8000 -t tests/server_v2" ], "test-coverage:win": [ "del clover.xml", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b890bb7..ddbd446 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,7 +2,6 @@ tests/ - tests/named-arguments/ + tests/named_arguments/ diff --git a/tests/EventSystemTest.php b/tests/EventSystemTest.php index 9a229b6..7af48f4 100644 --- a/tests/EventSystemTest.php +++ b/tests/EventSystemTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace flight\tests; +namespace tests; use Flight; use PHPUnit\Framework\TestCase; diff --git a/tests/named-arguments/ExampleClass.php b/tests/named_arguments/ExampleClass.php similarity index 78% rename from tests/named-arguments/ExampleClass.php rename to tests/named_arguments/ExampleClass.php index 508068c..9555047 100644 --- a/tests/named-arguments/ExampleClass.php +++ b/tests/named_arguments/ExampleClass.php @@ -2,6 +2,8 @@ declare(strict_types=1); +namespace tests\named_arguments; + // phpcs:ignore PSR1.Classes.ClassDeclaration.MissingNamespace class ExampleClass { diff --git a/tests/named-arguments/FlightTest.php b/tests/named_arguments/FlightTest.php similarity index 98% rename from tests/named-arguments/FlightTest.php rename to tests/named_arguments/FlightTest.php index 6593450..9053b02 100644 --- a/tests/named-arguments/FlightTest.php +++ b/tests/named_arguments/FlightTest.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace Tests\PHP8; +namespace tests\named_arguments; use DateTimeImmutable; -use ExampleClass; use Flight; use flight\Container; use flight\Engine; diff --git a/tests/phpunit_autoload.php b/tests/phpunit_autoload.php deleted file mode 100644 index 5bf47da..0000000 --- a/tests/phpunit_autoload.php +++ /dev/null @@ -1,9 +0,0 @@ -Middleware text: You are not authorized to access this route!'; - } - } - } -} - -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: 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 () { - 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'); - }); - }); - }); - }); - }); - }); - }); - }); - }); - - // 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 () { - 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 79e8f67..c042c1b 100644 --- a/tests/server/AuthCheck.php +++ b/tests/server/AuthCheck.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Server; +namespace tests\server; class AuthCheck { diff --git a/tests/server/LayoutMiddleware.php b/tests/server/LayoutMiddleware.php index 150985b..01058c0 100644 --- a/tests/server/LayoutMiddleware.php +++ b/tests/server/LayoutMiddleware.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Server; +namespace tests\server; use Flight; diff --git a/tests/server/OverwriteBodyMiddleware.php b/tests/server/OverwriteBodyMiddleware.php index 98e8468..6a3dfa8 100644 --- a/tests/server/OverwriteBodyMiddleware.php +++ b/tests/server/OverwriteBodyMiddleware.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Server; +namespace tests\server; use Flight; diff --git a/tests/server/Pascal_Snake_Case.php b/tests/server/Pascal_Snake_Case.php index d80ed41..df9a66f 100644 --- a/tests/server/Pascal_Snake_Case.php +++ b/tests/server/Pascal_Snake_Case.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Server; +namespace tests\server; class Pascal_Snake_Case // phpcs:ignore { diff --git a/tests/server/index.php b/tests/server/index.php index 6bb9dca..c34822e 100644 --- a/tests/server/index.php +++ b/tests/server/index.php @@ -7,10 +7,12 @@ 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; +use tests\server\AuthCheck; +use tests\server\LayoutMiddleware; +use tests\server\OverwriteBodyMiddleware; +use tests\server\Pascal_Snake_Case; + +require_once __DIR__ . '/../../vendor/autoload.php'; /* * This is the test file where we can open up a quick test server and make @@ -19,8 +21,6 @@ use Tests\Server\Pascal_Snake_Case; * @author Kristaps Muižnieks https://github.com/krmu */ -require_once __DIR__ . '/../phpunit_autoload.php'; - Flight::set('flight.content_length', false); Flight::set('flight.views.path', './'); Flight::set('flight.views.extension', '.phtml'); diff --git a/tests/server_v2/AuthCheck.php b/tests/server_v2/AuthCheck.php new file mode 100644 index 0000000..6ae651d --- /dev/null +++ b/tests/server_v2/AuthCheck.php @@ -0,0 +1,15 @@ +Middleware text: You are not authorized to access this route!'; + } + } +} diff --git a/tests/server_v2/index.php b/tests/server_v2/index.php new file mode 100644 index 0000000..edd0cb6 --- /dev/null +++ b/tests/server_v2/index.php @@ -0,0 +1,228 @@ +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: 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 () { + 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'); + }); + }); + }); + }); + }); + }); + }); + }); +}); + +// 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 () { + echo '
'; +}); + +Flight::after('start', function () { + echo '
'; + echo '
'; + echo "Request information
";
+    print_r(Flight::request());
+    echo "
"; + echo "
"; +}); + +Flight::start(); diff --git a/tests/server-v2/template.phtml b/tests/server_v2/template.phtml similarity index 100% rename from tests/server-v2/template.phtml rename to tests/server_v2/template.phtml