From 1926646cd44659c3f1ce2ea7bc4bf018fcd823a1 Mon Sep 17 00:00:00 2001 From: Phil Bennett Date: Wed, 20 Nov 2024 09:28:10 +0000 Subject: [PATCH] Fix type check for array based callables --- src/Route.php | 2 +- tests/RouteTest.php | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Route.php b/src/Route.php index 82ba8b2..db9f11f 100644 --- a/src/Route.php +++ b/src/Route.php @@ -28,7 +28,7 @@ class Route implements public function __construct( protected array|string $method, protected string $path, - callable|string|RequestHandlerInterface $handler, + callable|array|string|RequestHandlerInterface $handler, protected ?RouteGroup $group = null, protected array $vars = [] ) { diff --git a/tests/RouteTest.php b/tests/RouteTest.php index e8fb4ce..8d6f663 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -20,13 +20,43 @@ public function testRouteSetsAndResolvesInvokableClassCallable(): void $this->assertIsCallable($route->getCallable()); } - public function testRouteSetsAndResolvesClassMethodCallable(): void + public function testRouteSetsAndResolvesClassMethodArrayCallable(): void { $callable = [new Controller(), 'action']; $route = new Route('GET', '/', $callable); $this->assertIsCallable($route->getCallable()); } + public function testRouteSetsAndResolvesLazilyLoadedClassMethodArrayCallableWithoutContainer(): void + { + $callable = [new Controller(), 'action']; + $route = new Route('GET', '/', $callable); + $this->assertIsCallable($route->getCallable()); + } + + public function testRouteSetsAndResolvesLazilyLoadedClassMethodArrayCallableWithContainer(): void + { + $container = $this->createMock(ContainerInterface::class); + + $container + ->expects($this->once()) + ->method('has') + ->with($this->equalTo(Controller::class)) + ->willReturn(true) + ; + + $container + ->expects($this->once()) + ->method('get') + ->with($this->equalTo(Controller::class)) + ->willReturn(new Controller()) + ; + + $callable = [Controller::class, 'action']; + $route = new Route('GET', '/', $callable); + $this->assertIsCallable($route->getCallable($container)); + } + public function testRouteSetsAndResolvesNamedFunctionCallable(): void { $callable = 'League\Route\Fixture\namedFunctionCallable'; @@ -53,7 +83,7 @@ public function testRouteSetsAndResolvesClassMethodCallableAsStringViaContainer( ; $callable = 'League\Route\Fixture\Controller::action'; - $route = new Route('GET', '/', $callable); + $route = new Route('GET', '/', $callable); $newCallable = $route->getCallable($container); $this->assertIsArray($newCallable);