diff --git a/src/Contract/DefinitionInterface.php b/src/Contract/DefinitionInterface.php index b52e398..4befd67 100644 --- a/src/Contract/DefinitionInterface.php +++ b/src/Contract/DefinitionInterface.php @@ -5,9 +5,9 @@ namespace Yiisoft\Definitions\Contract; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use Yiisoft\Definitions\Exception\CircularReferenceException; use Yiisoft\Definitions\Exception\InvalidConfigException; -use Yiisoft\Definitions\Exception\NotFoundException; use Yiisoft\Definitions\Exception\NotInstantiableException; /** @@ -21,7 +21,7 @@ interface DefinitionInterface * @throws InvalidConfigException If an object of incorrect type was created. * @throws CircularReferenceException If there is a circular reference detected * when resolving the definition. - * @throws NotFoundException If container does not know how to resolve + * @throws NotFoundExceptionInterface If container does not know how to resolve * the definition. * @throws NotInstantiableException If an object can not be instantiated. * diff --git a/src/Exception/NotFoundException.php b/src/Exception/NotFoundException.php deleted file mode 100644 index 9de3b6b..0000000 --- a/src/Exception/NotFoundException.php +++ /dev/null @@ -1,39 +0,0 @@ -id = $id; - - $message = $id; - if ($buildStack !== []) { - $buildStack = array_keys($buildStack); - $last = end($buildStack); - $message = sprintf('%s while building %s', $last, implode(' -> ', $buildStack)); - } - - parent::__construct(sprintf('No definition or class found or resolvable for %s.', $message)); - } - - public function getId(): string - { - return $this->id; - } -} diff --git a/src/Infrastructure/DefinitionExtractor.php b/src/Infrastructure/DefinitionExtractor.php index 4139e4d..7a8841d 100644 --- a/src/Infrastructure/DefinitionExtractor.php +++ b/src/Infrastructure/DefinitionExtractor.php @@ -8,7 +8,6 @@ use ReflectionException; use ReflectionFunctionAbstract; use ReflectionParameter; -use Yiisoft\Definitions\Exception\NotFoundException; use Yiisoft\Definitions\Exception\NotInstantiableClassException; use Yiisoft\Definitions\Exception\NotInstantiableException; use Yiisoft\Definitions\ParameterDefinition; @@ -51,7 +50,6 @@ public static function getInstance(): self * * @psalm-param class-string $class * - * @throws NotFoundException * @throws NotInstantiableException * * @return ParameterDefinition[] @@ -66,7 +64,7 @@ public function fromClassName(string $class): array try { $reflectionClass = new ReflectionClass($class); } catch (ReflectionException $e) { - throw new NotFoundException($class); + throw new NotInstantiableClassException($class); } if (!$reflectionClass->isInstantiable()) { diff --git a/tests/Php8/ParameterDefinitionTest.php b/tests/Php8/ParameterDefinitionTest.php index e9ea81c..7399be5 100644 --- a/tests/Php8/ParameterDefinitionTest.php +++ b/tests/Php8/ParameterDefinitionTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Closure; +use Psr\Container\NotFoundExceptionInterface; use ReflectionClass; use ReflectionFunction; use ReflectionParameter; @@ -14,7 +15,6 @@ use Yiisoft\Definitions\ParameterDefinition; use Yiisoft\Definitions\Tests\Objects\GearBox; use Yiisoft\Definitions\Tests\Objects\UnionCar; -use Yiisoft\Test\Support\Container\Exception\NotFoundException; use Yiisoft\Test\Support\Container\SimpleContainer; final class ParameterDefinitionTest extends TestCase @@ -26,7 +26,7 @@ public function testNotInstantiable(): void ); $container = new SimpleContainer(); - $this->expectException(NotFoundException::class); + $this->expectException(NotFoundExceptionInterface::class); $definition->resolve($container); } diff --git a/tests/Unit/Exception/NotFoundExceptionTest.php b/tests/Unit/Exception/NotFoundExceptionTest.php deleted file mode 100644 index 0add891..0000000 --- a/tests/Unit/Exception/NotFoundExceptionTest.php +++ /dev/null @@ -1,35 +0,0 @@ -assertSame('test', $exception->getId()); - } - - public function testMessage(): void - { - $exception = new NotFoundException('test'); - - $this->assertSame('No definition or class found or resolvable for test.', $exception->getMessage()); - } - - public function testBuildStack(): void - { - $exception = new NotFoundException('test', ['a' => [], 'b' => [], 'test' => []]); - - $this->assertSame( - 'No definition or class found or resolvable for test while building a -> b -> test.', - $exception->getMessage() - ); - } -} diff --git a/tests/Unit/Infrastructure/DefinitionExtractorTest.php b/tests/Unit/Infrastructure/DefinitionExtractorTest.php index 32efc42..5244371 100644 --- a/tests/Unit/Infrastructure/DefinitionExtractorTest.php +++ b/tests/Unit/Infrastructure/DefinitionExtractorTest.php @@ -6,8 +6,8 @@ use DateTime; use PHPUnit\Framework\TestCase; +use Psr\Container\NotFoundExceptionInterface; use Yiisoft\Definitions\Contract\DefinitionInterface; -use Yiisoft\Definitions\Exception\NotFoundException; use Yiisoft\Definitions\Exception\NotInstantiableClassException; use Yiisoft\Definitions\Exception\NotInstantiableException; use Yiisoft\Definitions\Infrastructure\DefinitionExtractor; @@ -58,7 +58,7 @@ public function testResolveCarConstructor(): void $this->assertInstanceOf(ParameterDefinition::class, $dependencies['engine']); $this->assertInstanceOf(ParameterDefinition::class, $dependencies['moreEngines']); - $this->expectException(\Yiisoft\Test\Support\Container\Exception\NotFoundException::class); + $this->expectException(NotFoundExceptionInterface::class); $dependencies['engine']->resolve($container); } @@ -89,7 +89,7 @@ public function testNullableInterfaceDependency(): void /** @var DefinitionInterface[] $dependencies */ $dependencies = $resolver->fromClassName(NullableInterfaceDependency::class); $this->assertCount(1, $dependencies); - $this->expectException(\Yiisoft\Test\Support\Container\Exception\NotFoundException::class); + $this->expectException(NotFoundExceptionInterface::class); $dependencies['engine']->resolve($container); } @@ -110,7 +110,7 @@ public function testNullableConcreteDependency(): void /** @var DefinitionInterface[] $dependencies */ $dependencies = $resolver->fromClassName(NullableConcreteDependency::class); $this->assertCount(1, $dependencies); - $this->expectException(\Yiisoft\Test\Support\Container\Exception\NotFoundException::class); + $this->expectException(NotFoundExceptionInterface::class); $dependencies['car']->resolve($container); } @@ -138,8 +138,8 @@ public function testFromNonExistingClass(): void { $extractor = DefinitionExtractor::getInstance(); - $this->expectException(NotFoundException::class); - $this->expectExceptionMessage('No definition or class found or resolvable for NonExistingClass.'); + $this->expectException(NotInstantiableClassException::class); + $this->expectExceptionMessage('Can not instantiate NonExistingClass.'); $extractor->fromClassName('NonExistingClass'); } diff --git a/tests/Unit/ParameterDefinitionTest.php b/tests/Unit/ParameterDefinitionTest.php index d43caa8..4fc6a3e 100644 --- a/tests/Unit/ParameterDefinitionTest.php +++ b/tests/Unit/ParameterDefinitionTest.php @@ -6,6 +6,7 @@ use Closure; use PHPUnit\Framework\TestCase; +use Psr\Container\NotFoundExceptionInterface; use ReflectionClass; use ReflectionFunction; use ReflectionParameter; @@ -15,7 +16,6 @@ use Yiisoft\Definitions\ParameterDefinition; use Yiisoft\Definitions\Tests\Objects\Car; use Yiisoft\Definitions\Tests\Objects\NullableConcreteDependency; -use Yiisoft\Test\Support\Container\Exception\NotFoundException; use Yiisoft\Test\Support\Container\SimpleContainer; final class ParameterDefinitionTest extends TestCase @@ -119,7 +119,7 @@ public function testNullableParameterNotInstantiable(): void ); $container = new SimpleContainer(); - $this->expectException(NotFoundException::class); + $this->expectException(NotFoundExceptionInterface::class); $definition->resolve($container); } @@ -159,7 +159,7 @@ public function testNotInstantiable(): void ); $container = new SimpleContainer(); - $this->expectException(NotFoundException::class); + $this->expectException(NotFoundExceptionInterface::class); $definition->resolve($container); }