Skip to content

Commit

Permalink
Tests: cover more handlers usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 31, 2023
1 parent f60b129 commit f19020b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/Cases/DI/MessengerExtension.handler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()."'
);
});
12 changes: 12 additions & 0 deletions tests/Mocks/Handler/MultipleAttributesHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Tests\Mocks\Handler;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
#[AsMessageHandler]
final class MultipleAttributesHandler
{

}
16 changes: 16 additions & 0 deletions tests/Mocks/Handler/MultipleParametersHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Tests\Mocks\Handler;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final class MultipleParametersHandler
{

public function __invoke(string $foo, string $bar): void
{
// For tests
}

}
11 changes: 11 additions & 0 deletions tests/Mocks/Handler/NoMethodHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace Tests\Mocks\Handler;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final class NoMethodHandler
{

}

0 comments on commit f19020b

Please sign in to comment.