From f575f0603084271eb4e71a44e62f6c9facf67ebc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 14 Dec 2021 19:03:03 +0100 Subject: [PATCH] improved exception messages --- src/DI/Container.php | 2 +- src/DI/Definitions/ServiceDefinition.php | 2 +- tests/DI/Container.dynamic.php80.phpt | 2 +- tests/DI/ContainerBuilder.factory.error.phpt | 2 +- .../DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt | 4 ++-- tests/DI/ContainerBuilder.resolveTypes.phpt | 8 ++++---- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/DI/Container.php b/src/DI/Container.php index eccd75cc6..09518b571 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -77,7 +77,7 @@ public function addService(string $name, $service) if ($service instanceof \Closure) { $rt = Nette\Utils\Type::fromReflection(new \ReflectionFunction($service)); - $type = $rt ? Helpers::ensureClassType($rt, 'return type of factory') : ''; + $type = $rt ? Helpers::ensureClassType($rt, 'return type of closure') : ''; } else { $type = get_class($service); } diff --git a/src/DI/Definitions/ServiceDefinition.php b/src/DI/Definitions/ServiceDefinition.php index cdc1c395f..e7c8d91b8 100644 --- a/src/DI/Definitions/ServiceDefinition.php +++ b/src/DI/Definitions/ServiceDefinition.php @@ -185,7 +185,7 @@ public function resolveType(Nette\DI\Resolver $resolver): void } elseif (!$this->getType()) { $type = $resolver->resolveEntityType($this->factory); if (!$type) { - throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory.'); + throw new ServiceCreationException('Unknown service type, specify it or declare return type of factory method.'); } $this->setType($type); diff --git a/tests/DI/Container.dynamic.php80.phpt b/tests/DI/Container.dynamic.php80.phpt index e4794b31a..a4166f118 100644 --- a/tests/DI/Container.dynamic.php80.phpt +++ b/tests/DI/Container.dynamic.php80.phpt @@ -19,4 +19,4 @@ $container = new Container; Assert::exception(function () use ($container) { @$container->addService('six', function (): stdClass|Closure {}); // @ triggers service should be defined as "imported" $container->getService('six'); -}, Nette\InvalidStateException::class, "Return type of factory is not expected to be nullable/union/intersection/built-in, 'stdClass|Closure' given."); +}, Nette\InvalidStateException::class, "Return type of closure is not expected to be nullable/union/intersection/built-in, 'stdClass|Closure' given."); diff --git a/tests/DI/ContainerBuilder.factory.error.phpt b/tests/DI/ContainerBuilder.factory.error.phpt index b4f83c685..b4d6381da 100644 --- a/tests/DI/ContainerBuilder.factory.error.phpt +++ b/tests/DI/ContainerBuilder.factory.error.phpt @@ -117,7 +117,7 @@ Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('one')->setFactory('Bad7::create'); $builder->complete(); -}, Nette\DI\ServiceCreationException::class, "Service 'one': Unknown service type, specify it or declare return type of factory."); +}, Nette\DI\ServiceCreationException::class, "Service 'one': Unknown service type, specify it or declare return type of factory method."); class Bad8 diff --git a/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt b/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt index 9ce792b35..71a9c33e6 100644 --- a/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt +++ b/tests/DI/ContainerBuilder.factory.resolveBuiltinTypes.phpt @@ -136,7 +136,7 @@ namespace $builder->addDefinition('f') ->setFactory('@factory::createObject'); $container = createContainer($builder); - }, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory."); + }, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory method."); Assert::exception(function () { $builder = new DI\ContainerBuilder; @@ -145,6 +145,6 @@ namespace $builder->addDefinition('f') ->setFactory('@factory::createMixed'); $container = createContainer($builder); - }, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory."); + }, Nette\DI\ServiceCreationException::class, "Service 'f': Unknown service type, specify it or declare return type of factory method."); } diff --git a/tests/DI/ContainerBuilder.resolveTypes.phpt b/tests/DI/ContainerBuilder.resolveTypes.phpt index 81e9cb9a3..602cd36c6 100644 --- a/tests/DI/ContainerBuilder.resolveTypes.phpt +++ b/tests/DI/ContainerBuilder.resolveTypes.phpt @@ -140,14 +140,14 @@ Assert::exception(function () { $builder->addDefinition('a') ->setFactory([Factory::class, 'createObjectPhpDoc']); $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory."); +}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('a') ->setFactory([Factory::class, 'createObject']); $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory."); +}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); Assert::exception(function () { $builder = new DI\ContainerBuilder; @@ -161,14 +161,14 @@ Assert::exception(function () { $builder->addDefinition('a') ->setFactory([Factory::class, 'createMixedPhpDoc']); $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory."); +}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); Assert::exception(function () { $builder = new DI\ContainerBuilder; $builder->addDefinition('a') ->setFactory([Factory::class, 'createMixed']); $container = createContainer($builder); -}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory."); +}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method."); Assert::exception(function () { $builder = new DI\ContainerBuilder;