From 43300d5758f3c7d190167a192cfd89e8a1f0e757 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Fri, 29 Mar 2024 22:19:24 -0600 Subject: [PATCH] added ability to load classes with _ in them --- flight/core/Loader.php | 22 ++++++++++++++++++-- flight/util/ReturnTypeWillChange.php | 9 -------- tests/EngineTest.php | 10 +++++++-- tests/LoaderTest.php | 13 ++++++++++++ tests/run_all_tests.sh | 31 +++++++++++++++++++++------- tests/server/LayoutMiddleware.php | 1 + tests/server/Pascal_Snake_Case.php | 11 ++++++++++ tests/server/index.php | 8 +++---- 8 files changed, 81 insertions(+), 24 deletions(-) delete mode 100644 flight/util/ReturnTypeWillChange.php create mode 100644 tests/server/Pascal_Snake_Case.php diff --git a/flight/core/Loader.php b/flight/core/Loader.php index 9792949a..1824b9c7 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -25,6 +25,11 @@ class Loader */ protected array $classes = []; + /** + * If this is disabled, classes can load with underscores + */ + protected static bool $v2ClassLoading = true; + /** * Class instances. * @@ -190,14 +195,14 @@ public static function autoload(bool $enabled = true, $dirs = []): void */ public static function loadClass(string $class): void { - $classFile = str_replace(['\\', '_'], '/', $class) . '.php'; + $replace_chars = self::$v2ClassLoading === true ? ['\\', '_'] : ['\\']; + $classFile = str_replace($replace_chars, '/', $class) . '.php'; foreach (self::$dirs as $dir) { $filePath = "$dir/$classFile"; if (file_exists($filePath)) { require_once $filePath; - return; } } @@ -220,4 +225,17 @@ public static function addDirectory($dir): void } } } + + + /** + * Sets the value for V2 class loading. + * + * @param bool $value The value to set for V2 class loading. + * + * @return void + */ + public static function setV2ClassLoading(bool $value): void + { + self::$v2ClassLoading = $value; + } } diff --git a/flight/util/ReturnTypeWillChange.php b/flight/util/ReturnTypeWillChange.php deleted file mode 100644 index 31a929ba..00000000 --- a/flight/util/ReturnTypeWillChange.php +++ /dev/null @@ -1,9 +0,0 @@ -route('/container', Container::class.'->testThePdoWrapper'); $engine->request()->url = '/container'; - $this->expectException(ErrorException::class); - $this->expectExceptionMessageMatches("/Passing null to parameter/"); + // php 7.4 will throw a PDO exception, but php 8 will throw an ErrorException + if(version_compare(PHP_VERSION, '8.0.0', '<')) { + $this->expectException(PDOException::class); + $this->expectExceptionMessageMatches("/invalid data source name/"); + } else { + $this->expectException(ErrorException::class); + $this->expectExceptionMessageMatches("/Passing null to parameter/"); + } $engine->start(); } diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php index 44a89d01..9b6047c0 100644 --- a/tests/LoaderTest.php +++ b/tests/LoaderTest.php @@ -152,4 +152,17 @@ public function getDirectories() __DIR__ . '/classes' ], $loader->getDirectories()); } + + public function testV2ClassLoading() + { + $loader = new class extends Loader { + public static function getV2ClassLoading() + { + return self::$v2ClassLoading; + } + }; + $this->assertTrue($loader::getV2ClassLoading()); + $loader::setV2ClassLoading(false); + $this->assertFalse($loader::getV2ClassLoading()); + } } diff --git a/tests/run_all_tests.sh b/tests/run_all_tests.sh index d38bf0dd..72ec8ba1 100644 --- a/tests/run_all_tests.sh +++ b/tests/run_all_tests.sh @@ -1,9 +1,26 @@ #!/bin/bash -# Run all tests -composer lint -composer beautify -composer phpcs -composer test-coverage -xdg-open http://localhost:8000 -composer test-server \ No newline at end of file +php_versions=("php7.4" "php8.0" "php8.1" "php8.2" "php8.3") + +count=${#php_versions[@]} + + +echo "Prettifying code first" +vendor/bin/phpcbf --standard=phpcs.xml + +set -e +for ((i = 0; i < count; i++)); do + if type "${php_versions[$i]}" &> /dev/null; then + echo "Running tests for ${php_versions[$i]}" + echo " ${php_versions[$i]} vendor/bin/phpunit" + ${php_versions[$i]} vendor/bin/phpunit + + echo "Running PHPStan" + echo " ${php_versions[$i]} vendor/bin/phpstan" + ${php_versions[$i]} vendor/bin/phpstan + + echo "Running PHPCS" + echo " ${php_versions[$i]} vendor/bin/phpcs --standard=phpcs.xml -n" + ${php_versions[$i]} vendor/bin/phpcs --standard=phpcs.xml -n + fi +done \ No newline at end of file diff --git a/tests/server/LayoutMiddleware.php b/tests/server/LayoutMiddleware.php index 500cbd7e..b89c4e0c 100644 --- a/tests/server/LayoutMiddleware.php +++ b/tests/server/LayoutMiddleware.php @@ -83,6 +83,7 @@ public function before()
  • UTF8 URL w/ Param
  • Dice Container
  • No Container Registered
  • +
  • Pascal_Snake_Case
  • HTML; echo '
    '; diff --git a/tests/server/Pascal_Snake_Case.php b/tests/server/Pascal_Snake_Case.php new file mode 100644 index 00000000..bbba0d25 --- /dev/null +++ b/tests/server/Pascal_Snake_Case.php @@ -0,0 +1,11 @@ +testUi'); Flight::route('/dice', Container::class . '->testThePdoWrapper'); + Flight::route('/Pascal_Snake_Case', Pascal_Snake_Case::class . '->doILoad'); }, [ new LayoutMiddleware() ]); // Test 9: JSON output (should not output any other html)