Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/test/phpunit/.phpunit.cache/
/vendor/
/.idea/
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},

Expand Down
8 changes: 7 additions & 1 deletion src/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions src/ServiceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace GT\ServiceContainer;

use Psr\Container\NotFoundExceptionInterface;
use ReflectionMethod;
use ReflectionParameter;
use Throwable;

class ServiceNotFoundException extends ServiceContainerException implements NotFoundExceptionInterface {
Expand All @@ -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();
}
}
}
10 changes: 10 additions & 0 deletions test/phpunit/Example/PageWithMissingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace GT\ServiceContainer\Test\Example;

use LogicException;

class PageWithMissingService {
public function go(Greeter $greeter):void {
throw new LogicException("The callback must not run when its dependency is missing.");
}
}
92 changes: 92 additions & 0 deletions test/phpunit/InjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,101 @@
use GT\ServiceContainer\Injector;
use GT\ServiceContainer\ServiceNotFoundException;
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");
$sut = new Injector(new Container());

try {
$sut->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")
Expand Down
Loading