diff --git a/.editorconfig b/.editorconfig index 19ae126f..e8d15f4f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,9 +5,8 @@ indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true [*.md] indent_size = 2 - -[tests/views/*.php] -insert_final_newline = false diff --git a/.gemini/GEMINI.md b/.gemini/GEMINI.md index 59a33e44..03cc3ba4 100644 --- a/.gemini/GEMINI.md +++ b/.gemini/GEMINI.md @@ -16,12 +16,11 @@ This is the main FlightPHP core library for building fast, simple, and extensibl - Run tests: `composer test` (uses phpunit/phpunit and spatie/phpunit-watcher) - Run test server: `composer test-server` or `composer test-server-v2` - Lint code: `composer lint` (uses phpstan/phpstan, level 6) -- Beautify code: `composer beautify` (uses squizlabs/php_codesniffer, PSR1) -- Check code style: `composer phpcs` +- Format code: `composer format` (uses squizlabs/php_codesniffer, PSR12) - Test coverage: `composer test-coverage` ## Coding Standards -- Follow PSR1 coding standards (enforced by PHPCS) +- Follow PSR12 coding standards (enforced by PHPCS) - Use strict comparisons (`===`, `!==`) - PHPStan level 6 compliance - Focus on PHP 7.4 compatibility (avoid PHP 8+ only features) diff --git a/.gitattributes b/.gitattributes index c01e0897..72af06c1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,9 +5,9 @@ /.gitattributes export-ignore /.gitignore export-ignore /CONTRIBUTING.md export-ignore -/index.php export-ignore /phpcs.xml.dist export-ignore /phpstan-baseline.neon export-ignore /phpstan.dist.neon export-ignore -/phpunit-watcher.yml export-ignore +/phpunit-watcher.yml.dist export-ignore /phpunit.xml.dist export-ignore +/README.md export-ignore diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index 59a33e44..03cc3ba4 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -16,12 +16,11 @@ This is the main FlightPHP core library for building fast, simple, and extensibl - Run tests: `composer test` (uses phpunit/phpunit and spatie/phpunit-watcher) - Run test server: `composer test-server` or `composer test-server-v2` - Lint code: `composer lint` (uses phpstan/phpstan, level 6) -- Beautify code: `composer beautify` (uses squizlabs/php_codesniffer, PSR1) -- Check code style: `composer phpcs` +- Format code: `composer format` (uses squizlabs/php_codesniffer, PSR12) - Test coverage: `composer test-coverage` ## Coding Standards -- Follow PSR1 coding standards (enforced by PHPCS) +- Follow PSR12 coding standards (enforced by PHPCS) - Use strict comparisons (`===`, `!==`) - PHPStan level 6 compliance - Focus on PHP 7.4 compatibility (avoid PHP 8+ only features) diff --git a/.gitignore b/.gitignore index cb308cff..ac7efc8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,9 @@ -.idea/ -.vscode/ -vendor/ -composer.phar -composer.lock -.phpunit.result.cache -coverage/ *.sublime* -clover.xml +.phpunit.result.cache +/coverage/ +/vendor/ +composer.lock phpcs.xml phpstan.neon +phpunit-watcher.yml phpunit.xml -.runway-config.json -.runway-creds.json -.DS_Store diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4b1af6db..d6a5accf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,7 @@ Flight aims to be simple and fast. Anything that compromises either of those two * **Coding Standards** - We use PSR1 coding standards enforced by PHPCS. Some standards that either need additional configuration or need to be manually done are: * PHPStan is at level 6. - * `===` instead of truthy or falsey statements like `==` or `!is_array()`. + * `===` instead of truthy or falsey statements like `==`. * **PHP 7.4 Focused** - We do not make PHP 8+ focused enhancements on the framework as the focus is maintaining PHP 7.4. diff --git a/composer.json b/composer.json index 33d2951b..0d4817c2 100644 --- a/composer.json +++ b/composer.json @@ -1,19 +1,19 @@ { "name": "flightphp/core", "description": "Flight is a fast, simple, extensible framework for PHP. Flight enables you to quickly and easily build RESTful web applications. This is the maintained fork of mikecao/flight", - "homepage": "http://flightphp.com", + "homepage": "https://docs.flightphp.com/", "license": "MIT", "authors": [ { "name": "Mike Cao", "email": "mike@mikecao.com", - "homepage": "http://www.mikecao.com/", + "homepage": "https://mikecao.com/", "role": "Original Developer" }, { "name": "Franyer Sánchez", "email": "franyeradriansanchez@gmail.com", - "homepage": "https://faslatam.42web.io", + "homepage": "https://faslatam.42web.io/", "role": "Maintainer" }, { @@ -27,21 +27,16 @@ "ext-json": "*" }, "autoload": { - "files": [ - "flight/autoload.php" - ] - }, - "autoload-dev": { "classmap": [ - "tests/classes/" + "src/Flight.php" ], "psr-4": { - "Tests\\PHP8\\": [ - "tests/named-arguments" - ], - "Tests\\Server\\": "tests/server", - "Tests\\ServerV2\\": "tests/server-v2", - "tests\\groupcompactsyntax\\": "tests/groupcompactsyntax" + "flight\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "tests\\": "tests" } }, "require-dev": { @@ -54,7 +49,9 @@ "phpstan/phpstan": "^2.1", "phpunit/phpunit": "^9.6", "rregeer/phpunit-coverage-check": "^0.3.1", - "squizlabs/php_codesniffer": "^4.0" + "spatie/phpunit-watcher": "^1.23", + "squizlabs/php_codesniffer": "^4.0", + "symfony/var-dumper": "^5.4" }, "config": { "allow-plugins": { @@ -65,15 +62,12 @@ }, "scripts": { "test": "phpunit", - "test-watcher": [ - "phpunit-watcher || composer global require spatie/phpunit-watcher --dev", - "phpunit-watcher watch" - ], + "test-watcher": "phpunit-watcher watch", "test-coverage": [ - "rm -f clover.xml", + "rm -rf coverage", "@putenv XDEBUG_MODE=coverage", - "phpunit --coverage-html=coverage --coverage-clover=clover.xml", - "coverage-check clover.xml 100" + "phpunit --coverage-html coverage --coverage-clover coverage/clover.xml", + "coverage-check coverage/clover.xml 100" ], "test-server": [ "echo \"Running Test Server\"", @@ -81,12 +75,7 @@ ], "test-server-v2": [ "echo \"Running Test Server\"", - "@php -S localhost:8000 -t tests/server-v2" - ], - "test-coverage:win": [ - "del clover.xml", - "phpunit --coverage-html=coverage --coverage-clover=clover.xml", - "coverage-check clover.xml 100" + "@php -S localhost:8000 -t tests/server_v2" ], "test-performance": [ "echo \"Running Performance Tests...\"", @@ -97,13 +86,18 @@ "rm server.pid", "echo \"Performance Tests Completed.\"" ], - "lint": "phpstan --no-progress --memory-limit=256M", - "beautify": "phpcbf", - "phpcs": "phpcs", + "lint": [ + "phpstan --no-progress --memory-limit=256M", + "phpcs" + ], + "format": [ + "phpcbf" + ], "post-install-cmd": [ "@php -r \"if (!file_exists('phpcs.xml')) copy('phpcs.xml.dist', 'phpcs.xml');\"", "@php -r \"if (!file_exists('phpstan.neon')) copy('phpstan.dist.neon', 'phpstan.neon');\"", - "@php -r \"if (!file_exists('phpunit.xml')) copy('phpunit.xml.dist', 'phpunit.xml');\"" + "@php -r \"if (!file_exists('phpunit.xml')) copy('phpunit.xml.dist', 'phpunit.xml');\"", + "@php -r \"if (!file_exists('phpunit-watcher.yml')) copy('phpunit-watcher.yml.dist', 'phpunit-watcher.yml');\"" ] }, "suggest": { diff --git a/flight/autoload.php b/flight/autoload.php deleted file mode 100644 index 0a31c864..00000000 --- a/flight/autoload.php +++ /dev/null @@ -1,10 +0,0 @@ - - */ -class Loader -{ - /** - * Registered classes. - * - * @var array, ?callable}> $classes - */ - protected array $classes = []; - - /** - * If this is disabled, classes can load with underscores - */ - protected static bool $v2ClassLoading = true; - - /** - * Class instances. - * - * @var array - */ - protected array $instances = []; - - /** - * Autoload directories. - * - * @var array - */ - protected static array $dirs = []; - - /** - * Registers a class. - * - * @param string $name Registry name - * @param class-string|Closure(): T $class Class name or function to instantiate class - * @param array $params Class initialization parameters - * @param ?Closure(T $instance): void $callback $callback Function to call after object instantiation - * - * @template T of object - */ - public function register(string $name, $class, array $params = [], ?callable $callback = null): void - { - unset($this->instances[$name]); - - $this->classes[$name] = [$class, $params, $callback]; - } - - /** - * Unregisters a class. - * - * @param string $name Registry name - */ - public function unregister(string $name): void - { - unset($this->classes[$name]); - } - - /** - * Loads a registered class. - * - * @param string $name Method name - * @param bool $shared Shared instance - * - * @throws Exception - * - * @return ?object Class instance - */ - public function load(string $name, bool $shared = true): ?object - { - $obj = null; - - if (isset($this->classes[$name])) { - [0 => $class, 1 => $params, 2 => $callback] = $this->classes[$name]; - - $exists = isset($this->instances[$name]); - - if ($shared) { - $obj = ($exists) ? - $this->getInstance($name) : - $this->newInstance($class, $params); - - if (!$exists) { - $this->instances[$name] = $obj; - } - } else { - $obj = $this->newInstance($class, $params); - } - - if ($callback && (!$shared || !$exists)) { - $ref = [&$obj]; - \call_user_func_array($callback, $ref); - } - } - - return $obj; - } - - /** - * Gets a single instance of a class. - * - * @param string $name Instance name - * - * @return ?object Class instance - */ - public function getInstance(string $name): ?object - { - return $this->instances[$name] ?? null; - } - - /** - * Gets a new instance of a class. - * - * @param class-string|Closure(): class-string $class Class name or callback function to instantiate class - * @param array $params Class initialization parameters - * - * @template T of object - * - * @throws Exception - * - * @return T Class instance - */ - public function newInstance($class, array $params = []) - { - if (\is_callable($class)) { - return \call_user_func_array($class, $params); - } - - return new $class(...$params); - } - - /** - * Gets a registered callable - * - * @param string $name Registry name - * - * @return mixed Class information or null if not registered - */ - public function get(string $name) - { - return $this->classes[$name] ?? null; - } - - /** - * Resets the object to the initial state. - */ - public function reset(): void - { - $this->classes = []; - $this->instances = []; - } - - // Autoloading Functions - - /** - * Starts/stops autoloader. - * - * @param bool $enabled Enable/disable autoloading - * @param string|iterable $dirs Autoload directories - */ - public static function autoload(bool $enabled = true, $dirs = []): void - { - if ($enabled) { - spl_autoload_register([__CLASS__, 'loadClass']); - } else { - spl_autoload_unregister([__CLASS__, 'loadClass']); // @codeCoverageIgnore - } - - if (!empty($dirs)) { - self::addDirectory($dirs); - } - } - - /** - * Autoloads classes. - * - * Classes are not allowed to have underscores in their names. - * - * @param string $class Class name - */ - public static function loadClass(string $class): void - { - $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; - } - } - } - - /** - * Adds a directory for autoloading classes. - * - * @param string|iterable $dir Directory path - */ - public static function addDirectory($dir): void - { - if (\is_array($dir) || \is_object($dir)) { - foreach ($dir as $value) { - self::addDirectory($value); - } - } elseif (\is_string($dir)) { - if (!\in_array($dir, self::$dirs, true)) { - self::$dirs[] = $dir; - } - } - } - - - /** - * 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/index.php b/index.php deleted file mode 100644 index 0db24be2..00000000 --- a/index.php +++ /dev/null @@ -1,12 +0,0 @@ - - - index.php flight tests diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 57ab298b..d104bd58 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -5,6 +5,5 @@ includes: parameters: level: 6 paths: - - flight - - index.php + - src treatPhpDocTypesAsCertain: false diff --git a/phpunit-watcher.yml b/phpunit-watcher.yml deleted file mode 100644 index d31e9129..00000000 --- a/phpunit-watcher.yml +++ /dev/null @@ -1,13 +0,0 @@ -hideManual: true -watch: - directories: - - tests - - flight - fileMask: '*.php' -notifications: - passingTests: false - failingTests: false -phpunit: - binaryPath: ./vendor/bin/phpunit - arguments: '--stop-on-failure' - timeout: 180 \ No newline at end of file diff --git a/phpunit-watcher.yml.dist b/phpunit-watcher.yml.dist new file mode 100644 index 00000000..86c39791 --- /dev/null +++ b/phpunit-watcher.yml.dist @@ -0,0 +1,13 @@ +hideManual: true +watch: + directories: + - tests + - src + fileMask: '*.php' +notifications: + passingTests: false + failingTests: false +phpunit: + binaryPath: ./vendor/bin/phpunit + arguments: '--stop-on-failure' + timeout: 180 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b890bb7f..c1a85f74 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,16 +13,16 @@ colors="true"> - flight/ + src/ - flight/autoload.php + src/autoload.php tests/ - tests/named-arguments/ + tests/named_arguments/ diff --git a/flight/Engine.php b/src/Engine.php similarity index 100% rename from flight/Engine.php rename to src/Engine.php diff --git a/flight/Flight.php b/src/Flight.php similarity index 99% rename from flight/Flight.php rename to src/Flight.php index c2e9a012..ddb536f3 100644 --- a/flight/Flight.php +++ b/src/Flight.php @@ -11,8 +11,6 @@ use flight\core\EventDispatcher; use Psr\Container\ContainerInterface; -require_once __DIR__ . '/autoload.php'; - /** * The Flight class is a static representation of the framework. * diff --git a/flight/commands/AiGenerateInstructionsCommand.php b/src/commands/AiGenerateInstructionsCommand.php similarity index 100% rename from flight/commands/AiGenerateInstructionsCommand.php rename to src/commands/AiGenerateInstructionsCommand.php diff --git a/flight/commands/AiInitCommand.php b/src/commands/AiInitCommand.php similarity index 100% rename from flight/commands/AiInitCommand.php rename to src/commands/AiInitCommand.php diff --git a/flight/commands/ControllerCommand.php b/src/commands/ControllerCommand.php similarity index 100% rename from flight/commands/ControllerCommand.php rename to src/commands/ControllerCommand.php diff --git a/flight/commands/RouteCommand.php b/src/commands/RouteCommand.php similarity index 100% rename from flight/commands/RouteCommand.php rename to src/commands/RouteCommand.php diff --git a/flight/core/Dispatcher.php b/src/core/Dispatcher.php similarity index 100% rename from flight/core/Dispatcher.php rename to src/core/Dispatcher.php diff --git a/flight/core/EventDispatcher.php b/src/core/EventDispatcher.php similarity index 65% rename from flight/core/EventDispatcher.php rename to src/core/EventDispatcher.php index 8e60d1a6..251217c2 100644 --- a/flight/core/EventDispatcher.php +++ b/src/core/EventDispatcher.php @@ -6,51 +6,44 @@ class EventDispatcher { - /** @var self|null Singleton instance of the EventDispatcher */ + /** Singleton instance of the EventDispatcher */ private static ?self $instance = null; - /** @var array> */ + /** @var array> */ protected array $listeners = []; - /** - * Singleton instance of the EventDispatcher. - * - * @return self - */ + /** Singleton instance of the EventDispatcher. */ public static function getInstance(): self { - if (self::$instance === null) { + if (!self::$instance) { self::$instance = new self(); } + return self::$instance; } /** * Register a callback for an event. - * * @param string $event Event name - * @param callable $callback Callback function + * @param callable(): mixed $callback Callback function */ public function on(string $event, callable $callback): void { - if (isset($this->listeners[$event]) === false) { - $this->listeners[$event] = []; - } + $this->listeners[$event] ??= []; $this->listeners[$event][] = $callback; } /** * Trigger an event with optional arguments. - * * @param string $event Event name * @param mixed ...$args Arguments to pass to the callbacks - * * @return mixed */ public function trigger(string $event, ...$args) { $result = null; - if (isset($this->listeners[$event]) === true) { + + if (isset($this->listeners[$event])) { foreach ($this->listeners[$event] as $callback) { $result = call_user_func_array($callback, $args); @@ -60,27 +53,24 @@ public function trigger(string $event, ...$args) } } } + return $result; } /** * Check if an event has any registered listeners. - * * @param string $event Event name - * * @return bool True if the event has listeners, false otherwise */ public function hasListeners(string $event): bool { - return isset($this->listeners[$event]) === true && count($this->listeners[$event]) > 0; + return isset($this->listeners[$event]) && count($this->listeners[$event]) > 0; } /** * Get all listeners registered for a specific event. - * * @param string $event Event name - * - * @return array Array of callbacks registered for the event + * @return array Array of callbacks registered for the event */ public function getListeners(string $event): array { @@ -89,7 +79,6 @@ public function getListeners(string $event): array /** * Get a list of all events that have registered listeners. - * * @return array Array of event names */ public function getAllRegisteredEvents(): array @@ -99,41 +88,31 @@ public function getAllRegisteredEvents(): array /** * Remove a specific listener for an event. - * * @param string $event the event name - * @param callable $callback the exact callback to remove - * - * @return void + * @param callable(): mixed $callback the exact callback to remove */ public function removeListener(string $event, callable $callback): void { - if (isset($this->listeners[$event]) === true && count($this->listeners[$event]) > 0) { - $this->listeners[$event] = array_filter($this->listeners[$event], function ($listener) use ($callback) { - return $listener !== $callback; - }); + if ($this->hasListeners($event)) { + $this->listeners[$event] = array_filter( + $this->listeners[$event], + static fn(callable $listener): bool => $listener !== $callback, + ); + $this->listeners[$event] = array_values($this->listeners[$event]); // Re-index the array } } /** * Remove all listeners for a specific event. - * * @param string $event the event name - * - * @return void */ public function removeAllListeners(string $event): void { - if (isset($this->listeners[$event]) === true) { - unset($this->listeners[$event]); - } + unset($this->listeners[$event]); } - /** - * Remove the current singleton instance of the EventDispatcher. - * - * @return void - */ + /** Remove the current singleton instance of the EventDispatcher. */ public static function resetInstance(): void { self::$instance = null; diff --git a/src/core/Loader.php b/src/core/Loader.php new file mode 100644 index 00000000..c9656f14 --- /dev/null +++ b/src/core/Loader.php @@ -0,0 +1,130 @@ + + */ +class Loader +{ + /** @var array, ?callable(object): void}> */ + protected array $classes = []; + + /** @var array */ + protected array $instances = []; + + /** + * Registers a class. + * @template T of object + * @param string $name Registry name + * @param class-string|callable(): T $class Class name or function to instantiate class + * @param array $params Class initialization parameters + * @param ?callable(T): void $callback $callback Function to call after object instantiation + */ + public function register( + string $name, + $class, + array $params = [], + ?callable $callback = null + ): void { + unset($this->instances[$name]); + $this->classes[$name] = [$class, $params, $callback]; + } + + /** + * Unregisters a class. + * @param string $name Registry name + */ + public function unregister(string $name): void + { + unset($this->classes[$name]); + } + + /** + * Loads a registered class. + * @param string $name Method name + * @param bool $shared Shared instance + * @throws Throwable + * @return ?object Class instance + */ + public function load(string $name, bool $shared = true): ?object + { + $obj = null; + + if (isset($this->classes[$name])) { + [$class, $params, $callback] = $this->classes[$name]; + $exists = isset($this->instances[$name]); + + if ($shared) { + $obj = $exists + ? $this->getInstance($name) + : $this->newInstance($class, $params); + + if (!$exists) { + $this->instances[$name] = $obj; + } + } else { + $obj = $this->newInstance($class, $params); + } + + if ($callback && (!$shared || !$exists)) { + call_user_func_array($callback, [$obj]); + } + } + + return $obj; + } + + /** + * Gets a single instance of a class. + * @param string $name Instance name + * @return ?object Class instance + */ + public function getInstance(string $name): ?object + { + return $this->instances[$name] ?? null; + } + + /** + * Gets a new instance of a class. + * @template T of object + * @param class-string|callable(): T $class Class name or callback function to instantiate class + * @param array $params Class initialization parameters + * @throws Throwable + * @return T Class instance + */ + public function newInstance($class, array $params = []): object + { + if (is_callable($class)) { + return call_user_func_array($class, $params); + } + + return new $class(...$params); + } + + /** + * Gets a registered callable. + * @param string $name Registry name + * @return ?array{class-string|callable(): object, array, ?callable(object): void} + * Class information or null if not registered + */ + public function get(string $name): ?array + { + return $this->classes[$name] ?? null; + } + + /** Resets the object to the initial state */ + public function reset(): void + { + $this->classes = []; + $this->instances = []; + } +} diff --git a/flight/database/PdoWrapper.php b/src/database/PdoWrapper.php similarity index 100% rename from flight/database/PdoWrapper.php rename to src/database/PdoWrapper.php diff --git a/flight/database/SimplePdo.php b/src/database/SimplePdo.php similarity index 100% rename from flight/database/SimplePdo.php rename to src/database/SimplePdo.php diff --git a/flight/net/Request.php b/src/net/Request.php similarity index 100% rename from flight/net/Request.php rename to src/net/Request.php diff --git a/flight/net/Response.php b/src/net/Response.php similarity index 100% rename from flight/net/Response.php rename to src/net/Response.php diff --git a/flight/net/Route.php b/src/net/Route.php similarity index 100% rename from flight/net/Route.php rename to src/net/Route.php diff --git a/flight/net/Router.php b/src/net/Router.php similarity index 100% rename from flight/net/Router.php rename to src/net/Router.php diff --git a/flight/net/UploadedFile.php b/src/net/UploadedFile.php similarity index 100% rename from flight/net/UploadedFile.php rename to src/net/UploadedFile.php diff --git a/flight/template/View.php b/src/template/View.php similarity index 100% rename from flight/template/View.php rename to src/template/View.php diff --git a/flight/util/Collection.php b/src/util/Collection.php similarity index 100% rename from flight/util/Collection.php rename to src/util/Collection.php diff --git a/flight/util/Json.php b/src/util/Json.php similarity index 100% rename from flight/util/Json.php rename to src/util/Json.php diff --git a/flight/util/ReturnTypeWillChange.php b/src/util/ReturnTypeWillChange.php similarity index 100% rename from flight/util/ReturnTypeWillChange.php rename to src/util/ReturnTypeWillChange.php diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php index 97ee31aa..77fb0143 100644 --- a/tests/AutoloadTest.php +++ b/tests/AutoloadTest.php @@ -15,7 +15,6 @@ class AutoloadTest extends TestCase protected function setUp(): void { $this->app = new Engine(); - $this->app->path(__DIR__ . '/classes'); } // Autoload a class diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 5b0b516f..c74feaaf 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -71,8 +71,6 @@ public function testGetAndSet(): void // Register a class public function testRegister(): void { - Flight::path(__DIR__ . '/classes'); - Flight::register('user', User::class); $user = Flight::user(); diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php index de37efa8..7016130f 100644 --- a/tests/LoaderTest.php +++ b/tests/LoaderTest.php @@ -5,9 +5,9 @@ namespace tests; use flight\core\Loader; -use tests\classes\Factory; -use tests\classes\User; use PHPUnit\Framework\TestCase; +use tests\classes\User; +use tests\classes\Factory; use tests\classes\TesterClass; class LoaderTest extends TestCase @@ -17,63 +17,41 @@ class LoaderTest extends TestCase protected function setUp(): void { $this->loader = new Loader(); - $this->loader->autoload(true, __DIR__ . '/classes'); - } - - // Autoload a class - public function testAutoload(): void - { - $this->loader->register('tests', User::class); - - $test = $this->loader->load('tests'); - - self::assertIsObject($test); - self::assertInstanceOf(User::class, $test); } - // Register a class public function testRegister(): void { $this->loader->register('a', User::class); - $user = $this->loader->load('a'); - self::assertIsObject($user); self::assertInstanceOf(User::class, $user); - self::assertEquals('', $user->name); + self::assertSame('', $user->name); } - // Register a class with constructor parameters public function testRegisterWithConstructor(): void { $this->loader->register('b', User::class, ['Bob']); - $user = $this->loader->load('b'); - self::assertIsObject($user); self::assertInstanceOf(User::class, $user); - self::assertEquals('Bob', $user->name); + self::assertSame('Bob', $user->name); } - // Register a class with initialization public function testRegisterWithInitialization(): void { - $this->loader->register('c', User::class, ['Bob'], function ($user) { + $this->loader->register('c', User::class, ['Bob'], static function (User $user): void { $user->name = 'Fred'; }); $user = $this->loader->load('c'); - self::assertIsObject($user); self::assertInstanceOf(User::class, $user); self::assertEquals('Fred', $user->name); } - // Get a non-shared instance of a class public function testSharedInstance(): void { $this->loader->register('d', User::class); - $user1 = $this->loader->load('d'); $user2 = $this->loader->load('d'); $user3 = $this->loader->load('d', false); @@ -82,38 +60,24 @@ public function testSharedInstance(): void self::assertNotSame($user1, $user3); } - // Gets an object from a factory method public function testRegisterUsingCallable(): void { - $this->loader->register('e', ['\tests\classes\Factory', 'create']); - + $this->loader->register('e', [Factory::class, 'create']); $obj = $this->loader->load('e'); - - self::assertIsObject($obj); - self::assertInstanceOf(Factory::class, $obj); - $obj2 = $this->loader->load('e'); + $obj3 = $this->loader->load('e', false); - self::assertIsObject($obj2); - self::assertInstanceOf(Factory::class, $obj2); + self::assertInstanceOf(Factory::class, $obj); self::assertSame($obj, $obj2); - - $obj3 = $this->loader->load('e', false); - self::assertIsObject($obj3); self::assertInstanceOf(Factory::class, $obj3); self::assertNotSame($obj, $obj3); } - // Gets an object from a callback function public function testRegisterUsingCallback(): void { - $this->loader->register('f', function () { - return Factory::create(); - }); - + $this->loader->register('f', static fn(): Factory => Factory::create()); $obj = $this->loader->load('f'); - self::assertIsObject($obj); self::assertInstanceOf(Factory::class, $obj); } @@ -121,15 +85,22 @@ public function testUnregisterClass(): void { $this->loader->register('g', User::class); $current_class = $this->loader->get('g'); - $this->assertEquals([User::class, [], null], $current_class); + + $this->assertSame([User::class, [], null], $current_class); + $this->loader->unregister('g'); $unregistered_class_result = $this->loader->get('g'); + $this->assertNull($unregistered_class_result); } public function testNewInstance6Params(): void { - $TesterClass = $this->loader->newInstance(TesterClass::class, ['Bob', 'Fred', 'Joe', 'Jane', 'Sally', 'Suzie']); + $TesterClass = $this->loader->newInstance( + TesterClass::class, + ['Bob', 'Fred', 'Joe', 'Jane', 'Sally', 'Suzie'] + ); + $this->assertEquals('Bob', $TesterClass->param1); $this->assertEquals('Fred', $TesterClass->param2); $this->assertEquals('Joe', $TesterClass->param3); @@ -137,32 +108,4 @@ public function testNewInstance6Params(): void $this->assertEquals('Sally', $TesterClass->param5); $this->assertEquals('Suzie', $TesterClass->param6); } - - public function testAddDirectoryAsArray(): void - { - $loader = new class extends Loader { - public function getDirectories() - { - return self::$dirs; - } - }; - $loader->addDirectory([__DIR__ . '/classes']); - self::assertEquals([ - dirname(__DIR__), - __DIR__ . '/classes' - ], $loader->getDirectories()); - } - - public function testV2ClassLoading(): void - { - $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/classes/TesterClass.php b/tests/classes/TesterClass.php index 289ee04c..a0066f41 100644 --- a/tests/classes/TesterClass.php +++ b/tests/classes/TesterClass.php @@ -4,17 +4,23 @@ namespace tests\classes; -class TesterClass +final class TesterClass { - public $param1; - public $param2; - public $param3; - public $param4; - public $param5; - public $param6; + public ?string $param1; + public ?string $param2; + public ?string $param3; + public ?string $param4; + public ?string $param5; + public ?string $param6; - public function __construct($param1, $param2, $param3, $param4, $param5, $param6) - { + public function __construct( + ?string $param1, + ?string $param2, + ?string $param3, + ?string $param4, + ?string $param5, + ?string $param6 + ) { $this->param1 = $param1; $this->param2 = $param2; $this->param3 = $param3; diff --git a/tests/classes/User.php b/tests/classes/User.php index 0d141025..1a6c30b5 100644 --- a/tests/classes/User.php +++ b/tests/classes/User.php @@ -4,7 +4,7 @@ namespace tests\classes; -class User +final class User { public string $name; diff --git a/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php b/tests/group_compact_syntax/FlightRouteCompactSyntaxTest.php similarity index 96% rename from tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php rename to tests/group_compact_syntax/FlightRouteCompactSyntaxTest.php index 84d168f5..2c8d3a16 100644 --- a/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php +++ b/tests/group_compact_syntax/FlightRouteCompactSyntaxTest.php @@ -2,13 +2,10 @@ declare(strict_types=1); -namespace tests\groupcompactsyntax; +namespace tests\group_compact_syntax; use Flight; use PHPUnit\Framework\TestCase; -use tests\groupcompactsyntax\PostsController; -use tests\groupcompactsyntax\TodosController; -use tests\groupcompactsyntax\UsersController; final class FlightRouteCompactSyntaxTest extends TestCase { diff --git a/tests/groupcompactsyntax/PostsController.php b/tests/group_compact_syntax/PostsController.php similarity index 93% rename from tests/groupcompactsyntax/PostsController.php rename to tests/group_compact_syntax/PostsController.php index f95f60d4..5e693fbc 100644 --- a/tests/groupcompactsyntax/PostsController.php +++ b/tests/group_compact_syntax/PostsController.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace tests\groupcompactsyntax; +namespace tests\group_compact_syntax; final class PostsController { diff --git a/tests/groupcompactsyntax/TodosController.php b/tests/group_compact_syntax/TodosController.php similarity index 83% rename from tests/groupcompactsyntax/TodosController.php rename to tests/group_compact_syntax/TodosController.php index 91c30cf7..744f6b75 100644 --- a/tests/groupcompactsyntax/TodosController.php +++ b/tests/group_compact_syntax/TodosController.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace tests\groupcompactsyntax; +namespace tests\group_compact_syntax; final class TodosController { diff --git a/tests/groupcompactsyntax/UsersController.php b/tests/group_compact_syntax/UsersController.php similarity index 84% rename from tests/groupcompactsyntax/UsersController.php rename to tests/group_compact_syntax/UsersController.php index d6372b56..7323771b 100644 --- a/tests/groupcompactsyntax/UsersController.php +++ b/tests/group_compact_syntax/UsersController.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace tests\groupcompactsyntax; +namespace tests\group_compact_syntax; final class UsersController { 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 508068c1..95550476 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 6593450d..9053b02f 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/server/AuthCheck.php b/tests/server/AuthCheck.php index 79e8f677..c042c1b3 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 150985bd..01058c0b 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 98e84685..6a3dfa8a 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 d80ed416..df9a66ff 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 6bb9dcac..f4193e86 100644 --- a/tests/server/index.php +++ b/tests/server/index.php @@ -7,10 +7,10 @@ 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; /* * This is the test file where we can open up a quick test server and make diff --git a/tests/server-v2/index.php b/tests/server_v2/index.php similarity index 99% rename from tests/server-v2/index.php rename to tests/server_v2/index.php index 4f45d995..1aadcc1a 100644 --- a/tests/server-v2/index.php +++ b/tests/server_v2/index.php @@ -9,7 +9,7 @@ * @author Kristaps Muižnieks https://github.com/krmu */ -namespace Tests\ServerV2 { +namespace tests\server_v2 { class AuthCheck { public function before(): void @@ -23,7 +23,7 @@ public function before(): void namespace { - use Tests\ServerV2\AuthCheck; + use tests\server_v2\AuthCheck; require_once __DIR__ . '/../phpunit_autoload.php'; 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