-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSymfonyCommandBusTest.php
More file actions
125 lines (95 loc) · 4.07 KB
/
Copy pathSymfonyCommandBusTest.php
File metadata and controls
125 lines (95 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcingBundle\Tests\Unit\CommandBus;
use Patchlevel\EventSourcing\Aggregate\CustomId;
use Patchlevel\EventSourcingBundle\CommandBus\SymfonyCommandBus;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\CreateProfile;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
/** @covers \Patchlevel\EventSourcingBundle\EventBus\SymfonyEventBus */
final class SymfonyCommandBusTest extends TestCase
{
public function testDispatch(): void
{
$command = new CreateProfile(CustomId::fromString('1'));
$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();
return $envelope;
}
};
$messageBus = new MessageBus([$middleware]);
$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);
self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}
public function testException(): void
{
$command = new CreateProfile(CustomId::fromString('1'));
$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();
throw new RuntimeException('test');
}
};
$messageBus = new MessageBus([$middleware]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/^test$/');
$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);
self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}
public function testRecursiveException(): void
{
$command = new CreateProfile(CustomId::fromString('1'));
$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();
throw new HandlerFailedException($envelope, [new RuntimeException('test')]);
}
};
$messageBus = new MessageBus([$middleware]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/^test$/');
$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);
self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}
public function testRecursiveExceptionStringKey(): void
{
$command = new CreateProfile(CustomId::fromString('1'));
$middleware = new class implements MiddlewareInterface
{
public object|null $command = null;
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$this->command = $envelope->getMessage();
throw new HandlerFailedException($envelope, ['controller-class' => new RuntimeException('test')]);
}
};
$messageBus = new MessageBus([$middleware]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/^test$/');
$commandBus = new SymfonyCommandBus($messageBus);
$commandBus->dispatch($command);
self::assertNotNull($middleware->command);
self::assertSame($command, $middleware->command);
}
}