From 6012212b3959e65075be5776c7b1e76799fb62a1 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 3 Nov 2026 21:00:08 +0000 Subject: [PATCH 1/4] test: order tests --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index c040d25..06f7978 100644 --- a/composer.json +++ b/composer.json @@ -33,14 +33,14 @@ "scripts": { "phpunit": "vendor/bin/phpunit --configuration phpunit.xml", - "phpstan": "vendor/bin/phpstan analyse --level 6 src --memory-limit 256M", "phpcs": "vendor/bin/phpcs src --standard=phpcs.xml", "phpmd": "vendor/bin/phpmd src/ text phpmd.xml", + "phpstan": "vendor/bin/phpstan analyse --level 6 src --memory-limit 256M", "test": [ "@phpunit", - "@phpstan", "@phpcs", - "@phpmd" + "@phpmd", + "@phpstan" ] }, From 3439bc38afa6ec22a255cdecae9c71a08f46f54b Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 18 Nov 2026 12:44:00 +0000 Subject: [PATCH 2/4] test: reproduce missing application context in injection errors Add a failing regression test for #145 requiring the application callable, parameter and declaration location in missing-service diagnostics. --- .../Example/PageWithMissingService.php | 10 ++++++++ test/phpunit/InjectorTest.php | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/phpunit/Example/PageWithMissingService.php diff --git a/test/phpunit/Example/PageWithMissingService.php b/test/phpunit/Example/PageWithMissingService.php new file mode 100644 index 0000000..f2ebae0 --- /dev/null +++ b/test/phpunit/Example/PageWithMissingService.php @@ -0,0 +1,10 @@ +invoke($page, "go"); + } + catch(ServiceNotFoundException $exception) { + $message = $exception->getMessage(); + self::assertStringContainsString(Greeter::class, $message); + self::assertStringContainsString(PageWithMissingService::class . "::go", $message); + self::assertStringContainsString('$greeter', $message); + self::assertStringContainsString( + $declaration->getFileName() . ":" . $declaration->getStartLine(), + $message, + ); + return; + } + + self::fail("Expected the missing service to prevent callback invocation."); + } + public function testInvoke():void { $greeter = self::createStub(Greeter::class); $greeter->method("greet") From 43c546e56d57c90fa16a47aa9b2c64ebc7fc45f5 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 18 Nov 2026 13:12:11 +0000 Subject: [PATCH 3/4] fix: include application context in missing service errors --- src/Injector.php | 8 +++- src/ServiceNotFoundException.php | 16 ++++++++ test/phpunit/InjectorTest.php | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/Injector.php b/src/Injector.php index 3d67da5..b09bf40 100644 --- a/src/Injector.php +++ b/src/Injector.php @@ -37,7 +37,13 @@ public function invoke( } foreach($refFunction->getParameters() as $refParam) { - array_push($arguments, $this->resolveParameter($refParam, $extraArgs)); + try { + array_push($arguments, $this->resolveParameter($refParam, $extraArgs)); + } + catch(ServiceNotFoundException $exception) { + $exception->addParameterContext($refParam); + throw $exception; + } } if($instance) { diff --git a/src/ServiceNotFoundException.php b/src/ServiceNotFoundException.php index 514417f..14bbb09 100644 --- a/src/ServiceNotFoundException.php +++ b/src/ServiceNotFoundException.php @@ -2,6 +2,8 @@ namespace GT\ServiceContainer; use Psr\Container\NotFoundExceptionInterface; +use ReflectionMethod; +use ReflectionParameter; use Throwable; class ServiceNotFoundException extends ServiceContainerException implements NotFoundExceptionInterface { @@ -13,4 +15,18 @@ public function __construct( $message = "Service can not be located within Container: \"$className\" - docs: https://www.php.gt/servicecontainer/notfound"; parent::__construct($message, $code, $previous); } + + public function addParameterContext(ReflectionParameter $parameter):void { + $function = $parameter->getDeclaringFunction(); + $callable = $function->getName(); + if($function instanceof ReflectionMethod) { + $callable = $function->getDeclaringClass()->getName() . "::$callable"; + } + + $this->message .= "\nRequested by parameter \$" . $parameter->getName() + . " of $callable()"; + if($file = $function->getFileName()) { + $this->message .= " declared in $file:" . $function->getStartLine(); + } + } } diff --git a/test/phpunit/InjectorTest.php b/test/phpunit/InjectorTest.php index 0b6f1b1..f41bdc5 100644 --- a/test/phpunit/InjectorTest.php +++ b/test/phpunit/InjectorTest.php @@ -8,9 +8,76 @@ use GT\ServiceContainer\Test\Example\Greeter; use GT\ServiceContainer\Test\Example\PageWithMissingService; use PHPUnit\Framework\TestCase; +use ReflectionFunction; use ReflectionMethod; +use RuntimeException; class InjectorTest extends TestCase { + public function testInvoke_missingServicePreservesExceptionAndAddsFunctionContext():void { + $function = function(Greeter $greeter):void { + self::fail("The callback must not run without its dependency."); + }; + $declaration = new ReflectionFunction($function); + $previous = new RuntimeException("Original cause"); + $original = new ServiceNotFoundException(Greeter::class, 123, $previous); + $trace = $original->getTrace(); + $container = self::createMock(Container::class); + $container->expects(self::once())->method("get") + ->with(Greeter::class)->willThrowException($original); + $sut = new Injector($container); + + try { + $sut->invoke(null, $function); + } + catch(ServiceNotFoundException $exception) { + self::assertSame($original, $exception); + self::assertSame($trace, $exception->getTrace()); + self::assertSame(123, $exception->getCode()); + self::assertSame($previous, $exception->getPrevious()); + self::assertStringContainsString('$greeter', $exception->getMessage()); + self::assertStringContainsString($declaration->getName() . "()", $exception->getMessage()); + self::assertStringContainsString( + $declaration->getFileName() . ":" . $declaration->getStartLine(), + $exception->getMessage(), + ); + return; + } + + self::fail("Expected the missing service exception."); + } + + public function testInvoke_missingLoaderDependencyIncludesBothDeclarations():void { + $loader = function(DateTime $clock):Greeter { + self::fail("The loader must not run without its dependency."); + }; + $container = new Container(); + $container->setLoader(Greeter::class, $loader); + $sut = new Injector($container); + $page = new PageWithMissingService(); + $loaderDeclaration = new ReflectionFunction($loader); + $pageDeclaration = new ReflectionMethod($page, "go"); + + try { + $sut->invoke($page, "go"); + } + catch(ServiceNotFoundException $exception) { + $message = $exception->getMessage(); + self::assertStringContainsString('"' . DateTime::class . '"', $message); + self::assertStringContainsString('$clock', $message); + self::assertStringContainsString('$greeter', $message); + self::assertStringContainsString(PageWithMissingService::class . "::go", $message); + foreach([$loaderDeclaration, $pageDeclaration] as $declaration) { + self::assertStringContainsString( + $declaration->getFileName() . ":" . $declaration->getStartLine(), + $message, + ); + } + return; + } + + self::fail("Expected the missing loader dependency exception."); + } + public function testInvoke_missingServiceIdentifiesApplicationDeclaration():void { $page = new PageWithMissingService(); $declaration = new ReflectionMethod($page, "go"); From 02e4750ebf86869a24e32b28017632506168722e Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 18 Nov 2026 13:33:55 +0000 Subject: [PATCH 4/4] tweak: ignore generated files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..02fc092 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/test/phpunit/.phpunit.cache/ +/vendor/ +/.idea/