From f19020bb1e12cbb1d31ceff82ec86c5fe2b47ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Mon, 31 Jul 2023 16:30:51 +0200 Subject: [PATCH] Tests: cover more handlers usecases --- .../Cases/DI/MessengerExtension.handler.phpt | 64 +++++++++++++++++++ .../Handler/MultipleAttributesHandler.php | 12 ++++ .../Handler/MultipleParametersHandler.php | 16 +++++ tests/Mocks/Handler/NoMethodHandler.php | 11 ++++ 4 files changed, 103 insertions(+) create mode 100644 tests/Mocks/Handler/MultipleAttributesHandler.php create mode 100644 tests/Mocks/Handler/MultipleParametersHandler.php create mode 100644 tests/Mocks/Handler/NoMethodHandler.php diff --git a/tests/Cases/DI/MessengerExtension.handler.phpt b/tests/Cases/DI/MessengerExtension.handler.phpt index 19ed50a..bc56698 100644 --- a/tests/Cases/DI/MessengerExtension.handler.phpt +++ b/tests/Cases/DI/MessengerExtension.handler.phpt @@ -2,6 +2,7 @@ namespace Tests\Cases\DI; +use Contributte\Messenger\Exception\LogicalException; use Contributte\Tester\Toolkit; use Nette\DI\Compiler; use Symfony\Component\Messenger\Transport\TransportInterface; @@ -36,3 +37,66 @@ Toolkit::test(function (): void { Assert::count(1, $container->findByType(TransportInterface::class)); Assert::count(1, $container->findByType(SimpleHandler::class)); }); + +// Error: no invoke method +Toolkit::test(function (): void { + Assert::exception( + static function (): void { + Container::of() + ->withDefaults() + ->withCompiler(function (Compiler $compiler): void { + $compiler->addConfig(Helpers::neon(<<<'NEON' + messenger: + services: + - { factory: Tests\Mocks\Handler\NoMethodHandler, tags: [contributte.messenger.handler] } + NEON + )); + }) + ->build(); + }, + LogicalException::class, + 'Handler must have "Tests\Mocks\Handler\NoMethodHandler::__invoke()" method.' + ); +}); + +// Error: multiple attributes +Toolkit::test(function (): void { + Assert::exception( + static function (): void { + Container::of() + ->withDefaults() + ->withCompiler(function (Compiler $compiler): void { + $compiler->addConfig(Helpers::neon(<<<'NEON' + messenger: + services: + - Tests\Mocks\Handler\MultipleAttributesHandler + NEON + )); + }) + ->build(); + }, + LogicalException::class, + 'Only attribute #[AsMessageHandler] can be used on class "Tests\Mocks\Handler\MultipleAttributesHandler"' + ); +}); + +// Error: multiple parameters handler +Toolkit::test(function (): void { + Assert::exception( + static function (): void { + Container::of() + ->withDefaults() + ->withCompiler(function (Compiler $compiler): void { + $compiler->addConfig(Helpers::neon(<<<'NEON' + messenger: + services: + - Tests\Mocks\Handler\MultipleParametersHandler + NEON + )); + }) + ->build(); + }, + LogicalException::class, + 'Only one parameter is allowed in "Tests\Mocks\Handler\MultipleParametersHandler::__invoke()."' + ); +}); diff --git a/tests/Mocks/Handler/MultipleAttributesHandler.php b/tests/Mocks/Handler/MultipleAttributesHandler.php new file mode 100644 index 0000000..799e9c1 --- /dev/null +++ b/tests/Mocks/Handler/MultipleAttributesHandler.php @@ -0,0 +1,12 @@ +