Skip to content

Commit 455ca31

Browse files
authored
Merge pull request #329 from patchlevel/3.17.x-merge-up-into-3.18.x_zLycyEQB
Merge release 3.17.1 into 3.18.x
2 parents 85f959e + c4fcaf1 commit 455ca31

3 files changed

Lines changed: 128 additions & 85 deletions

File tree

src/CommandBus/SymfonyCommandBus.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Symfony\Component\Messenger\Exception\HandlerFailedException;
99
use Symfony\Component\Messenger\MessageBusInterface;
1010

11+
use function array_values;
12+
1113
final class SymfonyCommandBus implements CommandBus
1214
{
1315
public function __construct(
@@ -20,7 +22,7 @@ public function dispatch(object $command): void
2022
try {
2123
$this->messageBus->dispatch($command);
2224
} catch (HandlerFailedException $e) {
23-
throw $e->getWrappedExceptions(null, true)[0] ?? $e;
25+
throw array_values($e->getWrappedExceptions(null, true))[0] ?? $e;
2426
}
2527
}
2628
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 RuntimeException;
12+
use Symfony\Component\Messenger\Envelope;
13+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
14+
use Symfony\Component\Messenger\MessageBus;
15+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
16+
use Symfony\Component\Messenger\Middleware\StackInterface;
17+
18+
/** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */
19+
final class SymfonyCommandBusTest extends TestCase
20+
{
21+
public function testDispatch(): void
22+
{
23+
$command = new CreateProfile(CustomId::fromString('1'));
24+
25+
$middleware = new class implements MiddlewareInterface
26+
{
27+
public object|null $command = null;
28+
29+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
30+
{
31+
$this->command = $envelope->getMessage();
32+
33+
return $envelope;
34+
}
35+
};
36+
$messageBus = new MessageBus([$middleware]);
37+
38+
$commandBus = new SymfonyCommandBus($messageBus);
39+
$commandBus->dispatch($command);
40+
41+
self::assertNotNull($middleware->command);
42+
self::assertSame($command, $middleware->command);
43+
}
44+
45+
public function testException(): void
46+
{
47+
$command = new CreateProfile(CustomId::fromString('1'));
48+
49+
$middleware = new class implements MiddlewareInterface
50+
{
51+
public object|null $command = null;
52+
53+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
54+
{
55+
$this->command = $envelope->getMessage();
56+
57+
throw new RuntimeException('test');
58+
}
59+
};
60+
$messageBus = new MessageBus([$middleware]);
61+
62+
$this->expectException(RuntimeException::class);
63+
$this->expectExceptionMessageMatches('/^test$/');
64+
65+
$commandBus = new SymfonyCommandBus($messageBus);
66+
$commandBus->dispatch($command);
67+
68+
self::assertNotNull($middleware->command);
69+
self::assertSame($command, $middleware->command);
70+
}
71+
72+
public function testRecursiveException(): void
73+
{
74+
$command = new CreateProfile(CustomId::fromString('1'));
75+
76+
$middleware = new class implements MiddlewareInterface
77+
{
78+
public object|null $command = null;
79+
80+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
81+
{
82+
$this->command = $envelope->getMessage();
83+
84+
throw new HandlerFailedException($envelope, [new RuntimeException('test')]);
85+
}
86+
};
87+
$messageBus = new MessageBus([$middleware]);
88+
89+
$this->expectException(RuntimeException::class);
90+
$this->expectExceptionMessageMatches('/^test$/');
91+
92+
$commandBus = new SymfonyCommandBus($messageBus);
93+
$commandBus->dispatch($command);
94+
95+
self::assertNotNull($middleware->command);
96+
self::assertSame($command, $middleware->command);
97+
}
98+
99+
public function testRecursiveExceptionStringKey(): void
100+
{
101+
$command = new CreateProfile(CustomId::fromString('1'));
102+
103+
$middleware = new class implements MiddlewareInterface
104+
{
105+
public object|null $command = null;
106+
107+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
108+
{
109+
$this->command = $envelope->getMessage();
110+
111+
throw new HandlerFailedException($envelope, ['controller-class' => new RuntimeException('test')]);
112+
}
113+
};
114+
$messageBus = new MessageBus([$middleware]);
115+
116+
$this->expectException(RuntimeException::class);
117+
$this->expectExceptionMessageMatches('/^test$/');
118+
119+
$commandBus = new SymfonyCommandBus($messageBus);
120+
$commandBus->dispatch($command);
121+
122+
self::assertNotNull($middleware->command);
123+
self::assertSame($command, $middleware->command);
124+
}
125+
}

tests/Unit/CommandBus/SymfonyCommandtBusTest.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)