diff --git a/composer.json b/composer.json index 536aa44..8332e94 100644 --- a/composer.json +++ b/composer.json @@ -55,6 +55,7 @@ "scripts": { "test": "phpunit", "test-coverage": "rm clover.xml && XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100", + "test-server": "echo \"Running Test Server\" && COMPOSER_PROCESS_TIMEOUT=60000 php -S localhost:8000 -t tests/server/", "test-coverage:win": "del clover.xml && phpunit --coverage-html=coverage --coverage-clover=clover.xml && coverage-check clover.xml 100", "lint": "phpstan --no-progress -cphpstan.neon", "beautify": "phpcbf --standard=phpcs.xml", diff --git a/tests/server/index.php b/tests/server/index.php new file mode 100644 index 0000000..675c278 --- /dev/null +++ b/tests/server/index.php @@ -0,0 +1,199 @@ + + ul { + list-style-type: none; + margin: 0; + padding: 0; + overflow: hidden; + background-color: #333; + } + + li { + float: left; + } + #infotext { + font-weight: bold; + color: blueviolet; + } + li a { + display: block; + color: white; + text-align: center; + padding: 14px 16px; + text-decoration: none; + } + + 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; + } + + + HTML; + echo '
'; + } + + public function after() { + echo '
'; + echo '
'; + echo "

Request Information

";
+		print_r(Flight::request());
+		echo '

Raw Request Information

'; + print_r($_SERVER); + echo "

Response Information

";
+		print_r(Flight::response());
+		echo "
"; + echo "
"; + } +} + +Flight::group('', function() { + + // Test 1: Root route + Flight::route('/', function(){ + echo 'Route text: Root route works!'; + }); + 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'); + + /** middleware test */ + class authCheck { + public function before(){ + if(!isset($_COOKIE['user'])){ + echo 'Middleware text: You are not authorized to access this route!'; + } + } + } + $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.php', ['name' => $name]); + }); + + // Test 8: Throw an error + Flight::route('/error', function() { + trigger_error('This is a successful error'); + }); +}, [ new LayoutMiddleware ]); + +Flight::map('error', function (Throwable $e) { + echo sprintf( + '

500 Internal Server Error

' . + '

%s (%s)

' . + '
%s
', + $e->getMessage(), + $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::start(); diff --git a/tests/server/template.php b/tests/server/template.php new file mode 100644 index 0000000..7cb97b4 --- /dev/null +++ b/tests/server/template.php @@ -0,0 +1 @@ +Route text: Template works! \ No newline at end of file