Skip to content

Commit 3b2ca94

Browse files
author
Bl00D4NGEL
committed
test: add regression test for exception handling
1 parent db7e2b5 commit 3b2ca94

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

tests/Unit/Infrastructure/Messenger/CommandBusTest.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111
use Symfony\Component\Messenger\MessageBus;
1212
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
1313

14-
/**
15-
* Test fixture for CommandBus.
16-
*/
1714
class TestCommand implements Command
1815
{
1916
}
2017

21-
/**
22-
* Test fixture for CommandBus.
23-
*/
2418
class TestCommandHandler
2519
{
2620
public function __invoke(TestCommand $command): mixed
@@ -29,6 +23,18 @@ public function __invoke(TestCommand $command): mixed
2923
}
3024
}
3125

26+
class ThrowingCommandHandler
27+
{
28+
public function __construct(private readonly \Exception $exceptionToThrow)
29+
{
30+
}
31+
32+
public function __invoke(TestCommand $command): mixed
33+
{
34+
throw $this->exceptionToThrow;
35+
}
36+
}
37+
3238
class CommandBusTest extends TestCase
3339
{
3440
public function testDispatch(): void
@@ -47,6 +53,23 @@ public function testDispatch(): void
4753
$this->assertSame(TestCommand::class, $result);
4854
}
4955

56+
public function testDispatchFailsAndRethrowsException(): void
57+
{
58+
// Given
59+
$expectedException = new \Exception('Not good enough');
60+
$bus = $this->createMessageBus(
61+
TestCommand::class,
62+
new ThrowingCommandHandler($expectedException)
63+
);
64+
$commandBus = new CommandBus($bus);
65+
66+
// Then
67+
$this->expectExceptionObject($expectedException);
68+
69+
// When
70+
$commandBus->dispatch(new TestCommand());
71+
}
72+
5073
private function createMessageBus(
5174
string $type,
5275
callable $handler

tests/Unit/Infrastructure/Messenger/QueryBusTest.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
namespace GeekCell\DddBundle\Tests\Unit\Infrastructure\Messenger;
66

77
use GeekCell\Ddd\Contracts\Application\Query;
8+
use GeekCell\DddBundle\Infrastructure\Messenger\CommandBus;
89
use GeekCell\DddBundle\Infrastructure\Messenger\QueryBus;
910
use PHPUnit\Framework\TestCase;
1011
use Symfony\Component\Messenger\Handler\HandlersLocator;
1112
use Symfony\Component\Messenger\MessageBus;
1213
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
1314

14-
/**
15-
* Test fixture for QueryBus.
16-
*/
1715
class TestQuery implements Query
1816
{
1917
}
2018

21-
/**
22-
* Test fixture for CommandBus.
23-
*/
19+
class ThrowingQueryHandler
20+
{
21+
public function __construct(private readonly \Exception $exceptionToThrow)
22+
{
23+
}
24+
25+
public function __invoke(TestQuery $command): mixed
26+
{
27+
throw $this->exceptionToThrow;
28+
}
29+
}
30+
2431
class TestQueryHandler
2532
{
2633
public function __invoke(TestQuery $query): mixed
@@ -47,6 +54,23 @@ public function testDispatch(): void
4754
$this->assertSame(TestQuery::class, $result);
4855
}
4956

57+
public function testDispatchFailsAndRethrowsException(): void
58+
{
59+
// Given
60+
$expectedException = new \Exception('Not good enough');
61+
$bus = $this->createMessageBus(
62+
TestQuery::class,
63+
new ThrowingQueryHandler($expectedException)
64+
);
65+
$commandBus = new QueryBus($bus);
66+
67+
// Then
68+
$this->expectExceptionObject($expectedException);
69+
70+
// When
71+
$commandBus->dispatch(new TestQuery());
72+
}
73+
5074
private function createMessageBus(
5175
string $type,
5276
callable $handler

0 commit comments

Comments
 (0)