From 6d41115e9af83a0ec9b827b14b2cbab4d1f68093 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Mon, 18 Mar 2024 23:14:24 -0600 Subject: [PATCH 1/7] initial commit for containerization --- composer.json | 6 +- flight/Engine.php | 42 ++++++---- flight/Flight.php | 40 ++------- flight/core/Dispatcher.php | 158 ++++++++++++++++++++++++++++++++---- flight/net/Route.php | 4 +- flight/net/Router.php | 24 +++--- tests/DispatcherTest.php | 16 ++-- tests/EngineTest.php | 29 +++++++ tests/classes/Container.php | 23 ++++++ 9 files changed, 251 insertions(+), 91 deletions(-) create mode 100644 tests/classes/Container.php diff --git a/composer.json b/composer.json index 028f2da..2be3e4b 100644 --- a/composer.json +++ b/composer.json @@ -41,9 +41,11 @@ }, "require-dev": { "ext-pdo_sqlite": "*", - "phpunit/phpunit": "^9.5", - "phpstan/phpstan": "^1.10", + "league/container": "^4.2", + "level-2/dice": "^4.0", "phpstan/extension-installer": "^1.3", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^9.5", "rregeer/phpunit-coverage-check": "^0.3.1", "squizlabs/php_codesniffer": "^3.8" }, diff --git a/flight/Engine.php b/flight/Engine.php index effb0af..b22456e 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -30,17 +30,17 @@ use flight\net\Route; * @method void halt(int $code = 200, string $message = '', bool $actuallyExit = true) Stops processing and returns a given response. * * # Routing - * @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method Route route(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a URL to a callback function with all applicable methods * @method void group(string $pattern, callable $callback, array $group_middlewares = []) * Groups a set of routes together under a common prefix. - * @method Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method Route post(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a POST URL to a callback function. - * @method Route put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method Route put(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a PUT URL to a callback function. - * @method Route patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method Route patch(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a PATCH URL to a callback function. - * @method Route delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a DELETE URL to a callback function. * @method Router router() Gets router * @method string getUrl(string $alias) Gets a url from an alias @@ -217,6 +217,18 @@ class Engine $this->error($e); } + /** + * Registers the container handler + * + * @param callable $callback Callback function that sets the container and how it will inject classes + * + * @return void + */ + public function registerContainerHandler($callback): void + { + $this->dispatcher->setContainerHandler($callback); + } + /** * Maps a callback to a framework method. * @@ -605,11 +617,11 @@ class Engine * Routes a URL to a callback function. * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function * @param bool $pass_route Pass the matching route object to the callback * @param string $alias The alias for the route */ - public function _route(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route + public function _route(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route { return $this->router()->map($pattern, $callback, $pass_route, $alias); } @@ -630,10 +642,10 @@ class Engine * Routes a URL to a callback function. * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback */ - public function _post(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void + public function _post(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void { $this->router()->map('POST ' . $pattern, $callback, $pass_route, $route_alias); } @@ -642,10 +654,10 @@ class Engine * Routes a URL to a callback function. * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback */ - public function _put(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void + public function _put(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void { $this->router()->map('PUT ' . $pattern, $callback, $pass_route, $route_alias); } @@ -654,10 +666,10 @@ class Engine * Routes a URL to a callback function. * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback */ - public function _patch(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void + public function _patch(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void { $this->router()->map('PATCH ' . $pattern, $callback, $pass_route, $route_alias); } @@ -666,10 +678,10 @@ class Engine * Routes a URL to a callback function. * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback */ - public function _delete(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): void + public function _delete(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): void { $this->router()->map('DELETE ' . $pattern, $callback, $pass_route, $route_alias); } diff --git a/flight/Flight.php b/flight/Flight.php index 6e29781..9a3042c 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -26,17 +26,17 @@ require_once __DIR__ . '/autoload.php'; * Stop the framework with an optional status code and message. * * # Routing - * @method static Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method static Route route(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Maps a URL pattern to a callback with all applicable methods. * @method static void group(string $pattern, callable $callback, callable[] $group_middlewares = []) * Groups a set of routes together under a common prefix. - * @method static Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method static Route post(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a POST URL to a callback function. - * @method static Route put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method static Route put(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a PUT URL to a callback function. - * @method static Route patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method static Route patch(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a PATCH URL to a callback function. - * @method static Route delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') + * @method static Route delete(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') * Routes a DELETE URL to a callback function. * @method static Router router() Returns Router instance. * @method static string getUrl(string $alias, array $params = []) Gets a url from an alias @@ -101,34 +101,6 @@ class Flight { } - /** - * Registers a class to a framework method. - * - * # Usage example: - * ``` - * Flight::register('user', User::class); - * - * Flight::user(); # <- Return a User instance - * ``` - * - * @param string $name Static method name - * @param class-string $class Fully Qualified Class Name - * @param array $params Class constructor params - * @param ?Closure(T $instance): void $callback Perform actions with the instance - * - * @template T of object - */ - public static function register($name, $class, $params = [], $callback = null): void - { - static::__callStatic('register', [$name, $class, $params, $callback]); - } - - /** Unregisters a class. */ - public static function unregister(string $methodName): void - { - static::__callStatic('unregister', [$methodName]); - } - /** * Handles calls to static methods. * @@ -140,7 +112,7 @@ class Flight */ public static function __callStatic(string $name, array $params) { - return Dispatcher::invokeMethod([self::app(), $name], $params); + return self::app()->{$name}(...$params); } /** @return Engine Application instance */ diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 6af0d66..67a92f9 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -35,6 +35,25 @@ class Dispatcher */ protected array $filters = []; + /** + * This is a container for the dependency injection. + * + * @var callable|object|null + */ + protected $container_handler = null; + + /** + * Sets the dependency injection container handler. + * + * @param callable|object $container_handler Dependency injection container + * + * @return void + */ + public function setContainerHandler($container_handler): void + { + $this->container_handler = $container_handler; + } + /** * Dispatches an event. * @@ -80,10 +99,10 @@ class Dispatcher $requestedMethod = $this->get($eventName); if ($requestedMethod === null) { - throw new Exception("Event '$eventName' isn't found."); + throw new Exception("Event '{$eventName}' isn't found."); } - return $requestedMethod(...$params); + return $this->execute($requestedMethod, $params); } /** @@ -194,7 +213,7 @@ class Dispatcher * * @throws Exception If an event throws an `Exception` or if `$filters` contains an invalid filter. */ - public static function filter(array $filters, array &$params, &$output): void + public function filter(array $filters, array &$params, &$output): void { foreach ($filters as $key => $callback) { if (!is_callable($callback)) { @@ -219,22 +238,39 @@ class Dispatcher * @return mixed Function results * @throws Exception If `$callback` also throws an `Exception`. */ - public static function execute($callback, array &$params = []) + public function execute($callback, array &$params = []) { - $isInvalidFunctionName = ( - is_string($callback) - && !function_exists($callback) - ); + if (is_string($callback) === true && (strpos($callback, '->') !== false || strpos($callback, '::') !== false)) { + $callback = $this->parseStringClassAndMethod($callback); + } - if ($isInvalidFunctionName) { - throw new InvalidArgumentException('Invalid callback specified.'); + $this->handleInvalidCallbackType($callback); + + if (is_array($callback) === true) { + return $this->invokeMethod($callback, $params); } - if (is_array($callback)) { - return self::invokeMethod($callback, $params); + return $this->callFunction($callback, $params); + } + + /** + * Parses a string into a class and method. + * + * @param string $classAndMethod Class and method + * + * @return array{class-string|object, string} Class and method + */ + public function parseStringClassAndMethod(string $classAndMethod): array + { + $class_parts = explode('->', $classAndMethod); + if (count($class_parts) === 1) { + $class_parts = explode('::', $class_parts[0]); } - return self::callFunction($callback, $params); + $class = $class_parts[0]; + $method = $class_parts[1]; + + return [ $class, $method ]; } /** @@ -244,10 +280,11 @@ class Dispatcher * @param array &$params Function parameters * * @return mixed Function results + * @deprecated 3.7.0 Use invokeCallable instead */ - public static function callFunction(callable $func, array &$params = []) + public function callFunction(callable $func, array &$params = []) { - return call_user_func_array($func, $params); + return $this->invokeCallable($func, $params); } /** @@ -257,12 +294,48 @@ class Dispatcher * @param array &$params Class method parameters * * @return mixed Function results - * @throws TypeError For unexistent class name. + * @throws TypeError For nonexistent class name. + * @deprecated 3.7.0 Use invokeCallable instead + */ + public function invokeMethod(array $func, array &$params = []) + { + return $this->invokeCallable($func, $params); + } + + /** + * Invokes a callable (anonymous function or Class->method). + * + * @param array{class-string|object, string}|Callable $func Class method + * @param array &$params Class method parameters + * + * @return mixed Function results + * @throws TypeError For nonexistent class name. + * @throws InvalidArgumentException If the constructor requires parameters */ - public static function invokeMethod(array $func, array &$params = []) + public function invokeCallable($func, array &$params = []) { + // If this is a directly callable function, call it + if (is_array($func) === false) { + return call_user_func_array($func, $params); + } + [$class, $method] = $func; + // Only execute this if it's not a Flight class + if ( + $this->container_handler !== null && + ( + ( + is_object($class) === true && + strpos(get_class($class), 'flight\\') === false + ) || + is_string($class) === true + ) + ) { + $container_handler = $this->container_handler; + $class = $this->resolveContainerClass($container_handler, $class, $params); + } + if (is_string($class) && class_exists($class)) { $constructor = (new ReflectionClass($class))->getConstructor(); $constructorParamsNumber = 0; @@ -284,7 +357,56 @@ class Dispatcher $class = new $class(); } - return call_user_func_array([$class, $method], $params); + return call_user_func_array([ $class, $method ], $params); + } + + /** + * Handles invalid callback types. + * + * @param callable-string|(Closure(): mixed)|array{class-string|object, string} $callback + * Callback function + * + * @throws InvalidArgumentException If `$callback` is an invalid type + */ + protected function handleInvalidCallbackType($callback): void + { + $isInvalidFunctionName = ( + is_string($callback) + && !function_exists($callback) + ); + + if ($isInvalidFunctionName) { + throw new InvalidArgumentException('Invalid callback specified.'); + } + } + + /** + * Resolves the container class. + * + * @param callable|object $container_handler Dependency injection container + * @param class-string $class Class name + * @param array &$params Class constructor parameters + * + * @return object Class object + */ + protected function resolveContainerClass($container_handler, $class, array &$params) + { + $class_object = null; + + // PSR-11 + if ( + is_object($container_handler) === true && + method_exists($container_handler, 'has') === true && + $container_handler->has($class) + ) { + $class_object = call_user_func([$container_handler, 'get'], $class); + + // Just a callable where you configure the behavior (Dice, PHP-DI, etc.) + } elseif (is_callable($container_handler) === true) { + $class_object = call_user_func($container_handler, $class, $params); + } + + return $class_object; } /** diff --git a/flight/net/Route.php b/flight/net/Route.php index 4abf2ae..0b8b9d7 100644 --- a/flight/net/Route.php +++ b/flight/net/Route.php @@ -81,11 +81,11 @@ class Route * Constructor. * * @param string $pattern URL pattern - * @param callable $callback Callback function + * @param callable|string $callback Callback function * @param array $methods HTTP methods * @param bool $pass Pass self in callback parameters */ - public function __construct(string $pattern, callable $callback, array $methods, bool $pass, string $alias = '') + public function __construct(string $pattern, $callback, array $methods, bool $pass, string $alias = '') { $this->pattern = $pattern; $this->callback = $callback; diff --git a/flight/net/Router.php b/flight/net/Router.php index 81556cd..d494dbb 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -80,11 +80,11 @@ class Router * Maps a URL pattern to a callback function. * * @param string $pattern URL pattern to match. - * @param callable $callback Callback function. + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback. * @param string $route_alias Alias for the route. */ - public function map(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): Route + public function map(string $pattern, $callback, bool $pass_route = false, string $route_alias = ''): Route { // This means that the route ies defined in a group, but the defined route is the base @@ -133,11 +133,11 @@ class Router * Creates a GET based route * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback * @param string $alias Alias for the route */ - public function get(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route + public function get(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route { return $this->map('GET ' . $pattern, $callback, $pass_route, $alias); } @@ -146,11 +146,11 @@ class Router * Creates a POST based route * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback * @param string $alias Alias for the route */ - public function post(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route + public function post(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route { return $this->map('POST ' . $pattern, $callback, $pass_route, $alias); } @@ -159,11 +159,11 @@ class Router * Creates a PUT based route * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback * @param string $alias Alias for the route */ - public function put(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route + public function put(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route { return $this->map('PUT ' . $pattern, $callback, $pass_route, $alias); } @@ -172,11 +172,11 @@ class Router * Creates a PATCH based route * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback * @param string $alias Alias for the route */ - public function patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route + public function patch(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route { return $this->map('PATCH ' . $pattern, $callback, $pass_route, $alias); } @@ -185,11 +185,11 @@ class Router * Creates a DELETE based route * * @param string $pattern URL pattern to match - * @param callable $callback Callback function + * @param callable|string $callback Callback function or string class->method * @param bool $pass_route Pass the matching route object to the callback * @param string $alias Alias for the route */ - public function delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route + public function delete(string $pattern, $callback, bool $pass_route = false, string $alias = ''): Route { return $this->map('DELETE ' . $pattern, $callback, $pass_route, $alias); } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 826c190..2fd0d90 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -107,11 +107,11 @@ class DispatcherTest extends TestCase }); $this->dispatcher - ->hook('hello', $this->dispatcher::FILTER_BEFORE, function (array &$params): void { + ->hook('hello', Dispatcher::FILTER_BEFORE, function (array &$params): void { // Manipulate the parameter $params[0] = 'Fred'; }) - ->hook('hello', $this->dispatcher::FILTER_AFTER, function (array &$params, string &$output): void { + ->hook('hello', Dispatcher::FILTER_AFTER, function (array &$params, string &$output): void { // Manipulate the output $output .= ' Have a nice day!'; }); @@ -125,7 +125,7 @@ class DispatcherTest extends TestCase { $this->expectException(TypeError::class); - Dispatcher::execute(['NonExistentClass', 'nonExistentMethod']); + $this->dispatcher->execute(['NonExistentClass', 'nonExistentMethod']); } public function testInvalidCallableString(): void @@ -133,7 +133,7 @@ class DispatcherTest extends TestCase $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid callback specified.'); - Dispatcher::execute('inexistentGlobalFunction'); + $this->dispatcher->execute('inexistentGlobalFunction'); } public function testInvalidCallbackBecauseConstructorParameters(): void @@ -147,13 +147,13 @@ class DispatcherTest extends TestCase $this->expectExceptionMessage($exceptionMessage); static $params = []; - Dispatcher::invokeMethod([$class, $method], $params); + $this->dispatcher->invokeMethod([$class, $method], $params); } // It will be useful for executing instance Controller methods statically public function testCanExecuteAnNonStaticMethodStatically(): void { - $this->assertSame('hello', Dispatcher::execute([Hello::class, 'sayHi'])); + $this->assertSame('hello', $this->dispatcher->execute([Hello::class, 'sayHi'])); } public function testItThrowsAnExceptionWhenRunAnUnregistedEventName(): void @@ -237,7 +237,7 @@ class DispatcherTest extends TestCase $validCallable = function (): void { }; - Dispatcher::filter([$validCallable, $invalidCallable], $params, $output); + $this->dispatcher->filter([$validCallable, $invalidCallable], $params, $output); } public function testCallFunction6Params(): void @@ -247,7 +247,7 @@ class DispatcherTest extends TestCase }; $params = ['param1', 'param2', 'param3', 'param4', 'param5', 'param6']; - $result = Dispatcher::callFunction($func, $params); + $result = $this->dispatcher->callFunction($func, $params); $this->assertSame('helloparam1param2param3param4param5param6', $result); } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index a977e3a..157bbb6 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -11,6 +11,7 @@ use flight\net\Request; use flight\net\Response; use flight\util\Collection; use PHPUnit\Framework\TestCase; +use tests\classes\Container; // phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore class EngineTest extends TestCase @@ -676,4 +677,32 @@ class EngineTest extends TestCase $engine->start(); $this->expectOutputString('before456before123OKafter123456after123'); } + + public function testContainerDice() { + $engine = new Engine(); + $dice = new \Dice\Dice(); + $engine->registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + + $engine->route('/container', Container::class.'->testTheContainer'); + $engine->request()->url = '/container'; + $engine->start(); + + $this->expectOutputString('yay! I injected a collection, and it has 1 items'); + } + + public function testContainerPsr11() { + $engine = new Engine(); + $container = new \League\Container\Container(); + $container->add(Container::class)->addArgument(Collection::class); + $container->add(Collection::class); + $engine->registerContainerHandler($container); + + $engine->route('/container', Container::class.'->testTheContainer'); + $engine->request()->url = '/container'; + $engine->start(); + + $this->expectOutputString('yay! I injected a collection, and it has 1 items'); + } } diff --git a/tests/classes/Container.php b/tests/classes/Container.php new file mode 100644 index 0000000..ffb6fe1 --- /dev/null +++ b/tests/classes/Container.php @@ -0,0 +1,23 @@ +collection = $collection; + } + + public function testTheContainer() + { + $this->collection->whatever = 'yay!'; + echo 'yay! I injected a collection, and it has ' . $this->collection->count() . ' items'; + } +} From f610adfc145f20177af6c95196e46837784f13af Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Tue, 19 Mar 2024 09:03:41 -0600 Subject: [PATCH 2/7] lots more testing around containers --- flight/core/Dispatcher.php | 29 +++++++++++++++-- tests/DispatcherTest.php | 5 +-- tests/EngineTest.php | 64 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 67a92f9..41d9d39 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -333,7 +333,10 @@ class Dispatcher ) ) { $container_handler = $this->container_handler; - $class = $this->resolveContainerClass($container_handler, $class, $params); + $resolved_class = $this->resolveContainerClass($container_handler, $class, $params); + if($resolved_class !== null) { + $class = $resolved_class; + } } if (is_string($class) && class_exists($class)) { @@ -357,6 +360,21 @@ class Dispatcher $class = new $class(); } + // Final check to make sure it's actually a class and a method, or throw an error + if (is_object($class) === false) { + if(ob_get_level() > 1) { + ob_end_clean(); + } + throw new Exception("Class '$class' not found. Is it being correctly autoloaded with Flight::path()?"); + } + + if (method_exists($class, $method) === false) { + if(ob_get_level() > 1) { + ob_end_clean(); + } + throw new Exception("Method '".get_class($class)."::$method' not found."); + } + return call_user_func_array([ $class, $method ], $params); } @@ -403,7 +421,14 @@ class Dispatcher // Just a callable where you configure the behavior (Dice, PHP-DI, etc.) } elseif (is_callable($container_handler) === true) { - $class_object = call_user_func($container_handler, $class, $params); + + // This is to catch all the error that could be thrown by whatever container you are using + try { + $class_object = call_user_func($container_handler, $class, $params); + } catch (Exception $e) { + // could not resolve a class for some reason + $class_object = null; + } } return $class_object; diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 2fd0d90..7cf2c24 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -123,7 +123,8 @@ class DispatcherTest extends TestCase public function testInvalidCallback(): void { - $this->expectException(TypeError::class); + $this->expectException(Exception::class); + $this->expectExceptionMessage("Class 'NonExistentClass' not found. Is it being correctly autoloaded with Flight::path()?"); $this->dispatcher->execute(['NonExistentClass', 'nonExistentMethod']); } @@ -133,7 +134,7 @@ class DispatcherTest extends TestCase $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid callback specified.'); - $this->dispatcher->execute('inexistentGlobalFunction'); + $this->dispatcher->execute('nonexistentGlobalFunction'); } public function testInvalidCallbackBecauseConstructorParameters(): void diff --git a/tests/EngineTest.php b/tests/EngineTest.php index 157bbb6..dc5947b 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -692,6 +692,38 @@ class EngineTest extends TestCase $this->expectOutputString('yay! I injected a collection, and it has 1 items'); } + public function testContainerDiceBadClass() { + $engine = new Engine(); + $dice = new \Dice\Dice(); + $engine->registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + + $engine->route('/container', 'BadClass->testTheContainer'); + $engine->request()->url = '/container'; + + $this->expectException(Exception::class); + $this->expectExceptionMessage("Class 'BadClass' not found. Is it being correctly autoloaded with Flight::path()?"); + + $engine->start(); + } + + public function testContainerDiceBadMethod() { + $engine = new Engine(); + $dice = new \Dice\Dice(); + $engine->registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + + $engine->route('/container', Container::class.'->badMethod'); + $engine->request()->url = '/container'; + + $this->expectException(Exception::class); + $this->expectExceptionMessage("Method 'tests\classes\Container::badMethod' not found."); + + $engine->start(); + } + public function testContainerPsr11() { $engine = new Engine(); $container = new \League\Container\Container(); @@ -705,4 +737,36 @@ class EngineTest extends TestCase $this->expectOutputString('yay! I injected a collection, and it has 1 items'); } + + public function testContainerPsr11ClassNotFound() { + $engine = new Engine(); + $container = new \League\Container\Container(); + $container->add(Container::class)->addArgument(Collection::class); + $container->add(Collection::class); + $engine->registerContainerHandler($container); + + $engine->route('/container', 'BadClass->testTheContainer'); + $engine->request()->url = '/container'; + + $this->expectException(Exception::class); + $this->expectExceptionMessage("Class 'BadClass' not found. Is it being correctly autoloaded with Flight::path()?"); + + $engine->start(); + } + + public function testContainerPsr11MethodNotFound() { + $engine = new Engine(); + $container = new \League\Container\Container(); + $container->add(Container::class)->addArgument(Collection::class); + $container->add(Collection::class); + $engine->registerContainerHandler($container); + + $engine->route('/container', Container::class.'->badMethod'); + $engine->request()->url = '/container'; + + $this->expectException(Exception::class); + $this->expectExceptionMessage("Method 'tests\classes\Container::badMethod' not found."); + + $engine->start(); + } } From 3f887e305f5c0bd52ff96ca16fe325327f878423 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Tue, 19 Mar 2024 22:09:46 -0600 Subject: [PATCH 3/7] Added more tests. Captured callable exception thrown --- flight/Engine.php | 6 ++--- flight/core/Dispatcher.php | 47 +++++++++++++++++++++++--------------- tests/DispatcherTest.php | 8 +++---- tests/EngineTest.php | 9 ++++++-- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index b22456e..7f45daa 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -220,13 +220,13 @@ class Engine /** * Registers the container handler * - * @param callable $callback Callback function that sets the container and how it will inject classes + * @param callable|object $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes * * @return void */ - public function registerContainerHandler($callback): void + public function registerContainerHandler($containerHandler): void { - $this->dispatcher->setContainerHandler($callback); + $this->dispatcher->setContainerHandler($containerHandler); } /** diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 41d9d39..00a521b 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -25,6 +25,9 @@ class Dispatcher public const FILTER_AFTER = 'after'; private const FILTER_TYPES = [self::FILTER_BEFORE, self::FILTER_AFTER]; + /** @var mixed $container_exception Exception message if thrown by setting the container as a callable method */ + public static $container_exception = null; + /** @var array Mapped events. */ protected array $events = []; @@ -40,18 +43,18 @@ class Dispatcher * * @var callable|object|null */ - protected $container_handler = null; + protected $containerHandler = null; /** * Sets the dependency injection container handler. * - * @param callable|object $container_handler Dependency injection container + * @param callable|object $containerHandler Dependency injection container * * @return void */ - public function setContainerHandler($container_handler): void + public function setContainerHandler($containerHandler): void { - $this->container_handler = $container_handler; + $this->containerHandler = $containerHandler; } /** @@ -246,11 +249,7 @@ class Dispatcher $this->handleInvalidCallbackType($callback); - if (is_array($callback) === true) { - return $this->invokeMethod($callback, $params); - } - - return $this->callFunction($callback, $params); + return $this->invokeCallable($callback, $params); } /** @@ -311,6 +310,7 @@ class Dispatcher * @return mixed Function results * @throws TypeError For nonexistent class name. * @throws InvalidArgumentException If the constructor requires parameters + * @version 3.7.0 */ public function invokeCallable($func, array &$params = []) { @@ -321,9 +321,9 @@ class Dispatcher [$class, $method] = $func; - // Only execute this if it's not a Flight class + // Only execute the container handler if it's not a Flight class if ( - $this->container_handler !== null && + $this->containerHandler !== null && ( ( is_object($class) === true && @@ -332,13 +332,15 @@ class Dispatcher is_string($class) === true ) ) { - $container_handler = $this->container_handler; - $resolved_class = $this->resolveContainerClass($container_handler, $class, $params); - if($resolved_class !== null) { - $class = $resolved_class; + $containerHandler = $this->containerHandler; + $resolvedClass = $this->resolveContainerClass($containerHandler, $class, $params); + if($resolvedClass !== null) { + $class = $resolvedClass; } } + // If there's no container handler set, you can use [ 'className', 'methodName' ] + // to call a method dynamically. Nothing is injected into the class though. if (is_string($class) && class_exists($class)) { $constructor = (new ReflectionClass($class))->getConstructor(); $constructorParamsNumber = 0; @@ -362,17 +364,20 @@ class Dispatcher // Final check to make sure it's actually a class and a method, or throw an error if (is_object($class) === false) { - if(ob_get_level() > 1) { + // Cause PHPUnit has 1 level of output buffering by default + if(ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { ob_end_clean(); } throw new Exception("Class '$class' not found. Is it being correctly autoloaded with Flight::path()?"); } + // Class is there, but no method if (method_exists($class, $method) === false) { - if(ob_get_level() > 1) { + // Cause PHPUnit has 1 level of output buffering by default + if(ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { ob_end_clean(); } - throw new Exception("Method '".get_class($class)."::$method' not found."); + throw new Exception("Class found, but method '".get_class($class)."::$method' not found."); } return call_user_func_array([ $class, $method ], $params); @@ -428,6 +433,12 @@ class Dispatcher } catch (Exception $e) { // could not resolve a class for some reason $class_object = null; + + // If the container throws an exception, we need to catch it + // and store it somewhere. If we just let it throw itself, it + // doesn't properly close the output buffers and can cause other + // issues. + self::$container_exception = $e; } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 7cf2c24..9e3289e 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -192,7 +192,7 @@ class DispatcherTest extends TestCase { set_error_handler(function (int $errno, string $errstr): void { $this->assertSame(E_USER_NOTICE, $errno); - $this->assertSame("Event 'myMethod' has been overriden!", $errstr); + $this->assertSame("Event 'myMethod' has been overridden!", $errstr); }); $this->dispatcher->set('myMethod', function (): string { @@ -200,10 +200,10 @@ class DispatcherTest extends TestCase }); $this->dispatcher->set('myMethod', function (): string { - return 'Overriden'; + return 'Overridden'; }); - $this->assertSame('Overriden', $this->dispatcher->run('myMethod')); + $this->assertSame('Overridden', $this->dispatcher->run('myMethod')); restore_error_handler(); } @@ -219,7 +219,7 @@ class DispatcherTest extends TestCase return 'Original'; }) ->hook('myMethod', 'invalid', function (array &$params, &$output): void { - $output = 'Overriden'; + $output = 'Overridden'; }); $this->assertSame('Original', $this->dispatcher->run('myMethod')); diff --git a/tests/EngineTest.php b/tests/EngineTest.php index dc5947b..8ce6dfc 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -6,6 +6,7 @@ namespace tests; use Exception; use Flight; +use flight\core\Dispatcher; use flight\Engine; use flight\net\Request; use flight\net\Response; @@ -706,6 +707,8 @@ class EngineTest extends TestCase $this->expectExceptionMessage("Class 'BadClass' not found. Is it being correctly autoloaded with Flight::path()?"); $engine->start(); + + $this->assertEquals('Class BadClass not found', Dispatcher::$container_exception->getMessage()); } public function testContainerDiceBadMethod() { @@ -719,9 +722,11 @@ class EngineTest extends TestCase $engine->request()->url = '/container'; $this->expectException(Exception::class); - $this->expectExceptionMessage("Method 'tests\classes\Container::badMethod' not found."); + $this->expectExceptionMessage("Class found, but method 'tests\classes\Container::badMethod' not found."); $engine->start(); + + $this->assertNull(Dispatcher::$container_exception); } public function testContainerPsr11() { @@ -765,7 +770,7 @@ class EngineTest extends TestCase $engine->request()->url = '/container'; $this->expectException(Exception::class); - $this->expectExceptionMessage("Method 'tests\classes\Container::badMethod' not found."); + $this->expectExceptionMessage("Class found, but method 'tests\classes\Container::badMethod' not found."); $engine->start(); } From f752073f7d1fa6a5b80b474899e840bc6a9ff037 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Tue, 19 Mar 2024 23:52:39 -0600 Subject: [PATCH 4/7] more testing and catching things. --- flight/core/Dispatcher.php | 57 ++++++++++++++++-------------------- tests/DispatcherTest.php | 23 ++++++--------- tests/EngineTest.php | 58 +++++++++++++++++++++++++++++++++++-- tests/MapTest.php | 2 ++ tests/classes/Container.php | 11 ++++++- 5 files changed, 101 insertions(+), 50 deletions(-) diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 00a521b..e36644c 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -339,44 +339,24 @@ class Dispatcher } } - // If there's no container handler set, you can use [ 'className', 'methodName' ] - // to call a method dynamically. Nothing is injected into the class though. - if (is_string($class) && class_exists($class)) { - $constructor = (new ReflectionClass($class))->getConstructor(); - $constructorParamsNumber = 0; - - if ($constructor !== null) { - $constructorParamsNumber = count($constructor->getParameters()); - } - - if ($constructorParamsNumber > 0) { - $exceptionMessage = "Method '$class::$method' cannot be called statically. "; - $exceptionMessage .= sprintf( - "$class::__construct require $constructorParamsNumber parameter%s", - $constructorParamsNumber > 1 ? 's' : '' - ); - - throw new InvalidArgumentException($exceptionMessage, E_ERROR); - } - - $class = new $class(); - } - // Final check to make sure it's actually a class and a method, or throw an error - if (is_object($class) === false) { - // Cause PHPUnit has 1 level of output buffering by default - if(ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { - ob_end_clean(); - } + if (is_object($class) === false && class_exists($class) === false) { + $this->fixOutputBuffering(); throw new Exception("Class '$class' not found. Is it being correctly autoloaded with Flight::path()?"); } + // If this tried to resolve a class in a container and failed somehow, throw the exception + if (isset($resolvedClass) === false && self::$container_exception !== null) { + $this->fixOutputBuffering(); + throw self::$container_exception; + } + + // If we made it this far, we can forget the container exception and move on... + self::$container_exception = null; + // Class is there, but no method if (method_exists($class, $method) === false) { - // Cause PHPUnit has 1 level of output buffering by default - if(ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { - ob_end_clean(); - } + $this->fixOutputBuffering(); throw new Exception("Class found, but method '".get_class($class)."::$method' not found."); } @@ -445,6 +425,19 @@ class Dispatcher return $class_object; } + /** + * Because this could throw an exception in the middle of an output buffer, + * + * @return void + */ + protected function fixOutputBuffering(): void + { + // Cause PHPUnit has 1 level of output buffering by default + if(ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { + ob_end_clean(); + } + } + /** * Resets the object to the initial state. * diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 9e3289e..5fb902c 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -21,6 +21,7 @@ class DispatcherTest extends TestCase protected function setUp(): void { $this->dispatcher = new Dispatcher(); + Dispatcher::$container_exception = null; } public function testClosureMapping(): void @@ -137,20 +138,6 @@ class DispatcherTest extends TestCase $this->dispatcher->execute('nonexistentGlobalFunction'); } - public function testInvalidCallbackBecauseConstructorParameters(): void - { - $class = TesterClass::class; - $method = 'instanceMethod'; - $exceptionMessage = "Method '$class::$method' cannot be called statically. "; - $exceptionMessage .= "$class::__construct require 6 parameters"; - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage($exceptionMessage); - - static $params = []; - $this->dispatcher->invokeMethod([$class, $method], $params); - } - // It will be useful for executing instance Controller methods statically public function testCanExecuteAnNonStaticMethodStatically(): void { @@ -252,4 +239,12 @@ class DispatcherTest extends TestCase $this->assertSame('helloparam1param2param3param4param5param6', $result); } + + public function testInvokeMethod(): void + { + $class = new TesterClass('param1', 'param2', 'param3', 'param4', 'param5', 'param6'); + $result = $this->dispatcher->invokeMethod([ $class, 'instanceMethod' ]); + + $this->assertSame('param1', $class->param2); + } } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index 8ce6dfc..ba8739b 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -5,12 +5,13 @@ declare(strict_types=1); namespace tests; use Exception; -use Flight; use flight\core\Dispatcher; +use flight\database\PdoWrapper; use flight\Engine; use flight\net\Request; use flight\net\Response; use flight\util\Collection; +use PDOException; use PHPUnit\Framework\TestCase; use tests\classes\Container; @@ -20,6 +21,7 @@ class EngineTest extends TestCase public function setUp(): void { $_SERVER = []; + Dispatcher::$container_exception = null; } public function tearDown(): void @@ -682,6 +684,12 @@ class EngineTest extends TestCase public function testContainerDice() { $engine = new Engine(); $dice = new \Dice\Dice(); + $dice = $dice->addRules([ + PdoWrapper::class => [ + 'shared' => true, + 'constructParams' => [ 'sqlite::memory:' ] + ] + ]); $engine->registerContainerHandler(function ($class, $params) use ($dice) { return $dice->create($class, $params); }); @@ -693,6 +701,42 @@ class EngineTest extends TestCase $this->expectOutputString('yay! I injected a collection, and it has 1 items'); } + public function testContainerDicePdoWrapperTest() { + $engine = new Engine(); + $dice = new \Dice\Dice(); + $dice = $dice->addRules([ + PdoWrapper::class => [ + 'shared' => true, + 'constructParams' => [ 'sqlite::memory:' ] + ] + ]); + $engine->registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + + $engine->route('/container', Container::class.'->testThePdoWrapper'); + $engine->request()->url = '/container'; + $engine->start(); + + $this->expectOutputString('Yay! I injected a PdoWrapper, and it returned the number 5 from the database!'); + } + + public function testContainerDicePdoWrapperTestBadParams() { + $engine = new Engine(); + $dice = new \Dice\Dice(); + $engine->registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + + $engine->route('/container', Container::class.'->testThePdoWrapper'); + $engine->request()->url = '/container'; + + $this->expectException(PDOException::class); + $this->expectExceptionMessage("invalid data source name"); + + $engine->start(); + } + public function testContainerDiceBadClass() { $engine = new Engine(); $dice = new \Dice\Dice(); @@ -714,6 +758,12 @@ class EngineTest extends TestCase public function testContainerDiceBadMethod() { $engine = new Engine(); $dice = new \Dice\Dice(); + $dice = $dice->addRules([ + PdoWrapper::class => [ + 'shared' => true, + 'constructParams' => [ 'sqlite::memory:' ] + ] + ]); $engine->registerContainerHandler(function ($class, $params) use ($dice) { return $dice->create($class, $params); }); @@ -732,8 +782,9 @@ class EngineTest extends TestCase public function testContainerPsr11() { $engine = new Engine(); $container = new \League\Container\Container(); - $container->add(Container::class)->addArgument(Collection::class); + $container->add(Container::class)->addArgument(Collection::class)->addArgument(PdoWrapper::class); $container->add(Collection::class); + $container->add(PdoWrapper::class)->addArgument('sqlite::memory:'); $engine->registerContainerHandler($container); $engine->route('/container', Container::class.'->testTheContainer'); @@ -762,8 +813,9 @@ class EngineTest extends TestCase public function testContainerPsr11MethodNotFound() { $engine = new Engine(); $container = new \League\Container\Container(); - $container->add(Container::class)->addArgument(Collection::class); + $container->add(Container::class)->addArgument(Collection::class)->addArgument(PdoWrapper::class); $container->add(Collection::class); + $container->add(PdoWrapper::class)->addArgument('sqlite::memory:'); $engine->registerContainerHandler($container); $engine->route('/container', Container::class.'->badMethod'); diff --git a/tests/MapTest.php b/tests/MapTest.php index 445e8eb..79c39dc 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace tests; use Exception; +use flight\core\Dispatcher; use flight\Engine; use tests\classes\Hello; use PHPUnit\Framework\TestCase; @@ -15,6 +16,7 @@ class MapTest extends TestCase protected function setUp(): void { + Dispatcher::$container_exception = null; $this->app = new Engine(); } diff --git a/tests/classes/Container.php b/tests/classes/Container.php index ffb6fe1..7e67518 100644 --- a/tests/classes/Container.php +++ b/tests/classes/Container.php @@ -4,15 +4,18 @@ declare(strict_types=1); namespace tests\classes; +use flight\database\PdoWrapper; use flight\util\Collection; class Container { protected Collection $collection; + protected PdoWrapper $pdoWrapper; - public function __construct(Collection $collection) + public function __construct(Collection $collection, PdoWrapper $pdoWrapper) { $this->collection = $collection; + $this->pdoWrapper = $pdoWrapper; } public function testTheContainer() @@ -20,4 +23,10 @@ class Container $this->collection->whatever = 'yay!'; echo 'yay! I injected a collection, and it has ' . $this->collection->count() . ' items'; } + + public function testThePdoWrapper() + { + $value = (int) $this->pdoWrapper->fetchField('SELECT 5'); + echo 'Yay! I injected a PdoWrapper, and it returned the number ' . $value . ' from the database!'; + } } From dbf05ebf4044b8b778b7073e00f0da2b9ef953ff Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Wed, 20 Mar 2024 08:42:56 -0600 Subject: [PATCH 5/7] Lots more tests. Now will dynamically create object with engine instance. --- flight/Engine.php | 3 ++ flight/core/Dispatcher.php | 27 +++++++++----- tests/DispatcherTest.php | 57 +++++++++++++++++++++++++++++- tests/EngineTest.php | 5 --- tests/MapTest.php | 1 - tests/classes/ContainerDefault.php | 25 +++++++++++++ 6 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 tests/classes/ContainerDefault.php diff --git a/flight/Engine.php b/flight/Engine.php index 7f45daa..62342b6 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -138,6 +138,9 @@ class Engine $this->dispatcher->reset(); } + // Add this class to Dispatcher + $this->dispatcher->setEngine($this); + // Register default components $this->loader->register('request', Request::class); $this->loader->register('response', Response::class); diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index e36644c..672cb82 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -6,6 +6,7 @@ namespace flight\core; use Closure; use Exception; +use flight\Engine; use InvalidArgumentException; use ReflectionClass; use TypeError; @@ -26,7 +27,10 @@ class Dispatcher private const FILTER_TYPES = [self::FILTER_BEFORE, self::FILTER_AFTER]; /** @var mixed $container_exception Exception message if thrown by setting the container as a callable method */ - public static $container_exception = null; + protected $container_exception = null; + + /** @var ?Engine $engine Engine instance */ + protected ?Engine $engine = null; /** @var array Mapped events. */ protected array $events = []; @@ -57,6 +61,11 @@ class Dispatcher $this->containerHandler = $containerHandler; } + public function setEngine(Engine $engine): void + { + $this->engine = $engine; + } + /** * Dispatches an event. * @@ -346,20 +355,22 @@ class Dispatcher } // If this tried to resolve a class in a container and failed somehow, throw the exception - if (isset($resolvedClass) === false && self::$container_exception !== null) { + if (isset($resolvedClass) === false && $this->container_exception !== null) { $this->fixOutputBuffering(); - throw self::$container_exception; + throw $this->container_exception; } - // If we made it this far, we can forget the container exception and move on... - self::$container_exception = null; - // Class is there, but no method - if (method_exists($class, $method) === false) { + if (is_object($class) === true && method_exists($class, $method) === false) { $this->fixOutputBuffering(); throw new Exception("Class found, but method '".get_class($class)."::$method' not found."); } + // Class is a string, and method exists, create the object by hand and inject only the Engine + if (is_string($class) === true) { + $class = new $class($this->engine); + } + return call_user_func_array([ $class, $method ], $params); } @@ -418,7 +429,7 @@ class Dispatcher // and store it somewhere. If we just let it throw itself, it // doesn't properly close the output buffers and can cause other // issues. - self::$container_exception = $e; + $this->container_exception = $e; } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 5fb902c..a75f18f 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -4,13 +4,16 @@ declare(strict_types=1); namespace tests; +use ArgumentCountError; use Closure; use Exception; use flight\core\Dispatcher; +use flight\Engine; use InvalidArgumentException; use PharIo\Manifest\InvalidEmailException; use tests\classes\Hello; use PHPUnit\Framework\TestCase; +use tests\classes\ContainerDefault; use tests\classes\TesterClass; use TypeError; @@ -21,7 +24,6 @@ class DispatcherTest extends TestCase protected function setUp(): void { $this->dispatcher = new Dispatcher(); - Dispatcher::$container_exception = null; } public function testClosureMapping(): void @@ -247,4 +249,57 @@ class DispatcherTest extends TestCase $this->assertSame('param1', $class->param2); } + + public function testExecuteStringClassBadConstructParams(): void + { + $this->expectException(ArgumentCountError::class); + $this->expectExceptionMessageMatches('#Too few arguments to function tests\\\\classes\\\\TesterClass::__construct\(\), 1 passed .+ and exactly 6 expected#'); + $this->dispatcher->execute(TesterClass::class . '->instanceMethod'); + } + + public function testExecuteStringClassNoConstruct(): void + { + $result = $this->dispatcher->execute(Hello::class . '->sayHi'); + $this->assertSame('hello', $result); + } + + public function testExecuteStringClassNoConstructDoubleColon(): void + { + $result = $this->dispatcher->execute(Hello::class . '::sayHi'); + $this->assertSame('hello', $result); + } + + public function testExecuteStringClassNoConstructArraySyntax(): void + { + $result = $this->dispatcher->execute([ Hello::class, 'sayHi' ]); + $this->assertSame('hello', $result); + } + + public function testExecuteStringClassDefaultContainer(): void + { + $this->dispatcher->setEngine(new Engine); + $result = $this->dispatcher->execute(ContainerDefault::class . '->testTheContainer'); + $this->assertSame('You got it boss!', $result); + } + + public function testExecuteStringClassDefaultContainerDoubleColon(): void + { + $this->dispatcher->setEngine(new Engine); + $result = $this->dispatcher->execute(ContainerDefault::class . '::testTheContainer'); + $this->assertSame('You got it boss!', $result); + } + + public function testExecuteStringClassDefaultContainerArraySyntax(): void + { + $this->dispatcher->setEngine(new Engine); + $result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]); + $this->assertSame('You got it boss!', $result); + } + + public function testExecuteStringClassDefaultContainerButForgotInjectingEngine(): void + { + $this->expectException(TypeError::class); + $this->expectExceptionMessageMatches('#Argument 1 passed to tests\\\\classes\\\\ContainerDefault::__construct\(\) must be an instance of flight\\\\Engine, null given#'); + $result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]); + } } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index ba8739b..64c8911 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -21,7 +21,6 @@ class EngineTest extends TestCase public function setUp(): void { $_SERVER = []; - Dispatcher::$container_exception = null; } public function tearDown(): void @@ -751,8 +750,6 @@ class EngineTest extends TestCase $this->expectExceptionMessage("Class 'BadClass' not found. Is it being correctly autoloaded with Flight::path()?"); $engine->start(); - - $this->assertEquals('Class BadClass not found', Dispatcher::$container_exception->getMessage()); } public function testContainerDiceBadMethod() { @@ -775,8 +772,6 @@ class EngineTest extends TestCase $this->expectExceptionMessage("Class found, but method 'tests\classes\Container::badMethod' not found."); $engine->start(); - - $this->assertNull(Dispatcher::$container_exception); } public function testContainerPsr11() { diff --git a/tests/MapTest.php b/tests/MapTest.php index 79c39dc..bf276fb 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -16,7 +16,6 @@ class MapTest extends TestCase protected function setUp(): void { - Dispatcher::$container_exception = null; $this->app = new Engine(); } diff --git a/tests/classes/ContainerDefault.php b/tests/classes/ContainerDefault.php new file mode 100644 index 0000000..28abf4a --- /dev/null +++ b/tests/classes/ContainerDefault.php @@ -0,0 +1,25 @@ +set('test_me_out', 'You got it boss!'); + $this->app = $engine; + } + + public function testTheContainer() + { + return $this->app->get('test_me_out'); + } + +} From 1b6c5b942bc28975ff64de008b32852d4b929c98 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Wed, 20 Mar 2024 22:51:55 -0600 Subject: [PATCH 6/7] refactored some errors. UI Tests and cleanup. --- flight/Flight.php | 9 ++-- flight/core/Dispatcher.php | 77 +++++++++++++++++++----------- tests/DispatcherTest.php | 6 +-- tests/EngineTest.php | 1 - tests/MapTest.php | 1 - tests/classes/Container.php | 2 +- tests/classes/ContainerDefault.php | 5 +- tests/server/LayoutMiddleware.php | 2 + tests/server/index.php | 26 ++++++++++ 9 files changed, 89 insertions(+), 40 deletions(-) diff --git a/flight/Flight.php b/flight/Flight.php index 9a3042c..0a42489 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -19,11 +19,12 @@ require_once __DIR__ . '/autoload.php'; * @copyright Copyright (c) 2011, Mike Cao * * # Core methods - * @method static void start() Starts the framework. - * @method static void path(string $path) Adds a path for autoloading classes. - * @method static void stop(?int $code = null) Stops the framework and sends a response. - * @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true) + * @method static void start() Starts the framework. + * @method static void path(string $path) Adds a path for autoloading classes. + * @method static void stop(?int $code = null) Stops the framework and sends a response. + * @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true) * Stop the framework with an optional status code and message. + * @method static void registerContainerHandler(callable|object $containerHandler) Registers a container handler. * * # Routing * @method static Route route(string $pattern, callable|string $callback, bool $pass_route = false, string $alias = '') diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 672cb82..2841dd7 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -26,8 +26,8 @@ class Dispatcher public const FILTER_AFTER = 'after'; private const FILTER_TYPES = [self::FILTER_BEFORE, self::FILTER_AFTER]; - /** @var mixed $container_exception Exception message if thrown by setting the container as a callable method */ - protected $container_exception = null; + /** @var mixed $containerException Exception message if thrown by setting the container as a callable method */ + protected $containerException = null; /** @var ?Engine $engine Engine instance */ protected ?Engine $engine = null; @@ -256,8 +256,6 @@ class Dispatcher $callback = $this->parseStringClassAndMethod($callback); } - $this->handleInvalidCallbackType($callback); - return $this->invokeCallable($callback, $params); } @@ -325,10 +323,12 @@ class Dispatcher { // If this is a directly callable function, call it if (is_array($func) === false) { + $this->verifyValidFunction($func); return call_user_func_array($func, $params); } [$class, $method] = $func; + $resolvedClass = null; // Only execute the container handler if it's not a Flight class if ( @@ -343,28 +343,12 @@ class Dispatcher ) { $containerHandler = $this->containerHandler; $resolvedClass = $this->resolveContainerClass($containerHandler, $class, $params); - if($resolvedClass !== null) { + if ($resolvedClass !== null) { $class = $resolvedClass; } } - // Final check to make sure it's actually a class and a method, or throw an error - if (is_object($class) === false && class_exists($class) === false) { - $this->fixOutputBuffering(); - throw new Exception("Class '$class' not found. Is it being correctly autoloaded with Flight::path()?"); - } - - // If this tried to resolve a class in a container and failed somehow, throw the exception - if (isset($resolvedClass) === false && $this->container_exception !== null) { - $this->fixOutputBuffering(); - throw $this->container_exception; - } - - // Class is there, but no method - if (is_object($class) === true && method_exists($class, $method) === false) { - $this->fixOutputBuffering(); - throw new Exception("Class found, but method '".get_class($class)."::$method' not found."); - } + $this->verifyValidClassCallable($class, $method, $resolvedClass); // Class is a string, and method exists, create the object by hand and inject only the Engine if (is_string($class) === true) { @@ -382,7 +366,7 @@ class Dispatcher * * @throws InvalidArgumentException If `$callback` is an invalid type */ - protected function handleInvalidCallbackType($callback): void + protected function verifyValidFunction($callback): void { $isInvalidFunctionName = ( is_string($callback) @@ -394,6 +378,41 @@ class Dispatcher } } + + /** + * Verifies if the provided class and method are valid callable. + * + * @param string|object $class The class name. + * @param string $method The method name. + * @param object|null $resolvedClass The resolved class. + * + * @throws Exception If the class or method is not found. + * + * @return void + */ + protected function verifyValidClassCallable($class, $method, $resolvedClass): void + { + $final_exception = null; + + // Final check to make sure it's actually a class and a method, or throw an error + if (is_object($class) === false && class_exists($class) === false) { + $final_exception = new Exception("Class '$class' not found. Is it being correctly autoloaded with Flight::path()?"); + + // If this tried to resolve a class in a container and failed somehow, throw the exception + } elseif (isset($resolvedClass) === false && $this->containerException !== null) { + $final_exception = $this->containerException; + + // Class is there, but no method + } elseif (is_object($class) === true && method_exists($class, $method) === false) { + $final_exception = new Exception("Class found, but method '" . get_class($class) . "::$method' not found."); + } + + if ($final_exception !== null) { + $this->fixOutputBuffering(); + throw $final_exception; + } + } + /** * Resolves the container class. * @@ -417,7 +436,6 @@ class Dispatcher // Just a callable where you configure the behavior (Dice, PHP-DI, etc.) } elseif (is_callable($container_handler) === true) { - // This is to catch all the error that could be thrown by whatever container you are using try { $class_object = call_user_func($container_handler, $class, $params); @@ -425,11 +443,12 @@ class Dispatcher // could not resolve a class for some reason $class_object = null; - // If the container throws an exception, we need to catch it - // and store it somewhere. If we just let it throw itself, it - // doesn't properly close the output buffers and can cause other + // If the container throws an exception, we need to catch it + // and store it somewhere. If we just let it throw itself, it + // doesn't properly close the output buffers and can cause other // issues. - $this->container_exception = $e; + // This is thrown in the verifyValidClassCallable method + $this->containerException = $e; } } @@ -444,7 +463,7 @@ class Dispatcher protected function fixOutputBuffering(): void { // Cause PHPUnit has 1 level of output buffering by default - if(ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { + if (ob_get_level() > (getenv('PHPUNIT_TEST') ? 1 : 0)) { ob_end_clean(); } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index a75f18f..f2f3107 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -277,21 +277,21 @@ class DispatcherTest extends TestCase public function testExecuteStringClassDefaultContainer(): void { - $this->dispatcher->setEngine(new Engine); + $this->dispatcher->setEngine(new Engine()); $result = $this->dispatcher->execute(ContainerDefault::class . '->testTheContainer'); $this->assertSame('You got it boss!', $result); } public function testExecuteStringClassDefaultContainerDoubleColon(): void { - $this->dispatcher->setEngine(new Engine); + $this->dispatcher->setEngine(new Engine()); $result = $this->dispatcher->execute(ContainerDefault::class . '::testTheContainer'); $this->assertSame('You got it boss!', $result); } public function testExecuteStringClassDefaultContainerArraySyntax(): void { - $this->dispatcher->setEngine(new Engine); + $this->dispatcher->setEngine(new Engine()); $result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]); $this->assertSame('You got it boss!', $result); } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index 64c8911..b79e344 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace tests; use Exception; -use flight\core\Dispatcher; use flight\database\PdoWrapper; use flight\Engine; use flight\net\Request; diff --git a/tests/MapTest.php b/tests/MapTest.php index bf276fb..445e8eb 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace tests; use Exception; -use flight\core\Dispatcher; use flight\Engine; use tests\classes\Hello; use PHPUnit\Framework\TestCase; diff --git a/tests/classes/Container.php b/tests/classes/Container.php index 7e67518..5b9d216 100644 --- a/tests/classes/Container.php +++ b/tests/classes/Container.php @@ -26,7 +26,7 @@ class Container public function testThePdoWrapper() { - $value = (int) $this->pdoWrapper->fetchField('SELECT 5'); + $value = intval($this->pdoWrapper->fetchField('SELECT 5')); echo 'Yay! I injected a PdoWrapper, and it returned the number ' . $value . ' from the database!'; } } diff --git a/tests/classes/ContainerDefault.php b/tests/classes/ContainerDefault.php index 28abf4a..183d287 100644 --- a/tests/classes/ContainerDefault.php +++ b/tests/classes/ContainerDefault.php @@ -8,7 +8,6 @@ use flight\Engine; class ContainerDefault { - protected Engine $app; public function __construct(Engine $engine) @@ -22,4 +21,8 @@ class ContainerDefault return $this->app->get('test_me_out'); } + public function testUi() + { + echo 'Route text: The container successfully injected a value into the engine! Engine class: ' . get_class($this->app) . ' test_me_out Value: ' . $this->app->get('test_me_out') . ''; + } } diff --git a/tests/server/LayoutMiddleware.php b/tests/server/LayoutMiddleware.php index b2e59bb..500cbd7 100644 --- a/tests/server/LayoutMiddleware.php +++ b/tests/server/LayoutMiddleware.php @@ -81,6 +81,8 @@ class LayoutMiddleware
  • Slash in Param
  • UTF8 URL
  • UTF8 URL w/ Param
  • +
  • Dice Container
  • +
  • No Container Registered
  • HTML; echo '
    '; diff --git a/tests/server/index.php b/tests/server/index.php index f00144f..8417759 100644 --- a/tests/server/index.php +++ b/tests/server/index.php @@ -2,6 +2,10 @@ declare(strict_types=1); +use flight\database\PdoWrapper; +use tests\classes\Container; +use tests\classes\ContainerDefault; + /* * This is the test file where we can open up a quick test server and make * sure that the UI is really working the way we would expect it to. @@ -139,6 +143,9 @@ Flight::group('', function () { Flight::route('/redirect/@id', function ($id) { echo 'Route text: This route status is that it ' . ($id === 'before/after' ? 'succeeded' : 'failed') . ' URL Param: ' . $id . ''; }); + + Flight::route('/no-container', ContainerDefault::class . '->testUi'); + Flight::route('/dice', Container::class . '->testThePdoWrapper'); }, [ new LayoutMiddleware() ]); // Test 9: JSON output (should not output any other html) @@ -167,4 +174,23 @@ Flight::map('notFound', function () { echo "Go back"; }); +Flight::map('start', function () { + + if (Flight::request()->url === '/dice') { + $dice = new \Dice\Dice(); + $dice = $dice->addRules([ + PdoWrapper::class => [ + 'shared' => true, + 'constructParams' => [ 'sqlite::memory:' ] + ] + ]); + Flight::registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + } + + // Default start behavior now + Flight::_start(); +}); + Flight::start(); From 54403bede8b5041f81788e592059156160de94ad Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Thu, 21 Mar 2024 08:45:02 -0600 Subject: [PATCH 7/7] One last test to make sure Dice maintains the same instance --- flight/core/Dispatcher.php | 1 - tests/DispatcherTest.php | 12 +++++++++--- tests/EngineTest.php | 21 +++++++++++++++++++++ tests/classes/ContainerDefault.php | 6 +++++- tests/server/index.php | 1 + 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 2841dd7..6a167a8 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -8,7 +8,6 @@ use Closure; use Exception; use flight\Engine; use InvalidArgumentException; -use ReflectionClass; use TypeError; /** diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index f2f3107..d4e1ca1 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -277,21 +277,27 @@ class DispatcherTest extends TestCase public function testExecuteStringClassDefaultContainer(): void { - $this->dispatcher->setEngine(new Engine()); + $engine = new Engine(); + $engine->set('test_me_out', 'You got it boss!'); + $this->dispatcher->setEngine($engine); $result = $this->dispatcher->execute(ContainerDefault::class . '->testTheContainer'); $this->assertSame('You got it boss!', $result); } public function testExecuteStringClassDefaultContainerDoubleColon(): void { - $this->dispatcher->setEngine(new Engine()); + $engine = new Engine(); + $engine->set('test_me_out', 'You got it boss!'); + $this->dispatcher->setEngine($engine); $result = $this->dispatcher->execute(ContainerDefault::class . '::testTheContainer'); $this->assertSame('You got it boss!', $result); } public function testExecuteStringClassDefaultContainerArraySyntax(): void { - $this->dispatcher->setEngine(new Engine()); + $engine = new Engine(); + $engine->set('test_me_out', 'You got it boss!'); + $this->dispatcher->setEngine($engine); $result = $this->dispatcher->execute([ ContainerDefault::class, 'testTheContainer' ]); $this->assertSame('You got it boss!', $result); } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index b79e344..a8d58ef 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -13,6 +13,7 @@ use flight\util\Collection; use PDOException; use PHPUnit\Framework\TestCase; use tests\classes\Container; +use tests\classes\ContainerDefault; // phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore class EngineTest extends TestCase @@ -719,6 +720,26 @@ class EngineTest extends TestCase $this->expectOutputString('Yay! I injected a PdoWrapper, and it returned the number 5 from the database!'); } + public function testContainerDiceFlightEngine() { + $engine = new Engine(); + $engine->set('test_me_out', 'You got it boss!'); + $dice = new \Dice\Dice(); + $dice = $dice->addRule('*', [ + 'substitutions' => [ + Engine::class => $engine + ] + ]); + $engine->registerContainerHandler(function ($class, $params) use ($dice) { + return $dice->create($class, $params); + }); + + $engine->route('/container', ContainerDefault::class.'->echoTheContainer'); + $engine->request()->url = '/container'; + $engine->start(); + + $this->expectOutputString('You got it boss!'); + } + public function testContainerDicePdoWrapperTestBadParams() { $engine = new Engine(); $dice = new \Dice\Dice(); diff --git a/tests/classes/ContainerDefault.php b/tests/classes/ContainerDefault.php index 183d287..a71ff1f 100644 --- a/tests/classes/ContainerDefault.php +++ b/tests/classes/ContainerDefault.php @@ -12,7 +12,6 @@ class ContainerDefault public function __construct(Engine $engine) { - $engine->set('test_me_out', 'You got it boss!'); $this->app = $engine; } @@ -21,6 +20,11 @@ class ContainerDefault return $this->app->get('test_me_out'); } + public function echoTheContainer() + { + echo $this->app->get('test_me_out'); + } + public function testUi() { echo 'Route text: The container successfully injected a value into the engine! Engine class: ' . get_class($this->app) . ' test_me_out Value: ' . $this->app->get('test_me_out') . ''; diff --git a/tests/server/index.php b/tests/server/index.php index 8417759..769dc0c 100644 --- a/tests/server/index.php +++ b/tests/server/index.php @@ -144,6 +144,7 @@ Flight::group('', function () { 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 Flight::route('/no-container', ContainerDefault::class . '->testUi'); Flight::route('/dice', Container::class . '->testThePdoWrapper'); }, [ new LayoutMiddleware() ]);