Skip to content

Commit b07f9de

Browse files
authored
Merge pull request #262 from patchlevel/unwrap-handler-failed-exception
unwrap handler failed exception in symfony command bus
2 parents 5835425 + a1e646e commit b07f9de

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/CommandBus/SymfonyCommandBus.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Patchlevel\EventSourcingBundle\CommandBus;
66

77
use Patchlevel\EventSourcing\CommandBus\CommandBus;
8+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
89
use Symfony\Component\Messenger\MessageBusInterface;
910

1011
final class SymfonyCommandBus implements CommandBus
@@ -16,6 +17,10 @@ public function __construct(
1617

1718
public function dispatch(object $command): void
1819
{
19-
$this->messageBus->dispatch($command);
20+
try {
21+
$this->messageBus->dispatch($command);
22+
} catch (HandlerFailedException $e) {
23+
throw $e->getWrappedExceptions(null, true)[0] ?? $e;
24+
}
2025
}
2126
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcingBundle\Tests\Unit\CommandBus;
6+
7+
use Patchlevel\EventSourcing\Aggregate\CustomId;
8+
use Patchlevel\EventSourcingBundle\CommandBus\SymfonyCommandBus;
9+
use Patchlevel\EventSourcingBundle\Tests\Fixtures\CreateProfile;
10+
use PHPUnit\Framework\TestCase;
11+
use Prophecy\PhpUnit\ProphecyTrait;
12+
use RuntimeException;
13+
use Symfony\Component\Messenger\Envelope;
14+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
15+
use Symfony\Component\Messenger\MessageBusInterface;
16+
17+
/** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */
18+
final class SymfonyCommandtBusTest extends TestCase
19+
{
20+
use ProphecyTrait;
21+
22+
public function testDispatch(): void
23+
{
24+
$command = new CreateProfile(
25+
CustomId::fromString('1'),
26+
);
27+
28+
$symfony = $this->prophesize(MessageBusInterface::class);
29+
30+
$envelope = new Envelope($command);
31+
32+
$symfony->dispatch($command)->willReturn($envelope)->shouldBeCalled();
33+
34+
$commandBus = new SymfonyCommandBus($symfony->reveal());
35+
$commandBus->dispatch($command);
36+
}
37+
38+
public function testException(): void
39+
{
40+
$command = new CreateProfile(
41+
CustomId::fromString('1'),
42+
);
43+
44+
$symfony = $this->prophesize(MessageBusInterface::class);
45+
46+
$internalException = new class extends RuntimeException {
47+
};
48+
49+
$envelope = new Envelope($command);
50+
51+
$symfony
52+
->dispatch($command)
53+
->willThrow(new HandlerFailedException($envelope, [$internalException]))
54+
->shouldBeCalled();
55+
56+
$commandBus = new SymfonyCommandBus($symfony->reveal());
57+
58+
$this->expectException($internalException::class);
59+
60+
$commandBus->dispatch($command);
61+
}
62+
63+
64+
public function testRecursiveException(): void
65+
{
66+
$command = new CreateProfile(
67+
CustomId::fromString('1'),
68+
);
69+
70+
$symfony = $this->prophesize(MessageBusInterface::class);
71+
72+
$internalException = new class extends RuntimeException {
73+
};
74+
75+
$envelope = new Envelope($command);
76+
77+
$symfony
78+
->dispatch($command)
79+
->willThrow(new HandlerFailedException($envelope, [new HandlerFailedException($envelope, [$internalException])]))
80+
->shouldBeCalled();
81+
82+
$commandBus = new SymfonyCommandBus($symfony->reveal());
83+
84+
$this->expectException($internalException::class);
85+
86+
$commandBus->dispatch($command);
87+
}
88+
}

0 commit comments

Comments
 (0)