-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAggregateRootTestCase.php
More file actions
221 lines (176 loc) · 6.78 KB
/
AggregateRootTestCase.php
File metadata and controls
221 lines (176 loc) · 6.78 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\PhpUnit\Test;
use Closure;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Constraint\Exception as ExceptionConstraint;
use PHPUnit\Framework\Constraint\ExceptionMessageIsOrContains;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionFunction;
use Throwable;
use function array_filter;
use function array_values;
use function sprintf;
abstract class AggregateRootTestCase extends TestCase
{
/** @var array<object> */
private array $givenEvents = [];
private object|null $when = null;
/** @var array<mixed> */
private array $parameters = [];
private object|null $aggregate = null;
/** @var array<object|Closure> */
private array $thenExpectations = [];
private Throwable|null $thrownException = null;
/** @var class-string<Throwable>|null */
private string|null $expectedException = null;
private string|null $expectedExceptionMessage = null;
/** @return class-string<AggregateRoot> */
abstract protected function aggregateClass(): string;
final public function given(object ...$events): self
{
$this->aggregate = null;
$this->givenEvents = $events;
return $this;
}
/** @param object|Closure $callable */
final public function when(object $callable, mixed ...$parameters): self
{
$this->when = $callable;
$this->parameters = $parameters;
if ($this->givenEvents) {
$this->aggregate = $this->aggregateClass()::createFromEvents($this->givenEvents);
}
try {
$callableOrCommand = $this->when;
$return = null;
if ($callableOrCommand instanceof Closure) {
/** @var AggregateRoot|null $return */
$return = $callableOrCommand($this->aggregate);
} else {
foreach (HandlerFinder::findInClass($this->aggregateClass()) as $handler) {
if (!$callableOrCommand instanceof $handler->commandClass) {
continue;
}
$reflection = new ReflectionClass($this->aggregateClass());
$reflectionMethod = $reflection->getMethod($handler->method);
/** @var AggregateRoot|null $return */
$return = $reflectionMethod->invokeArgs($handler->static ? null : $this->aggregate, [$callableOrCommand, ...$this->parameters]);
}
}
if ($this->aggregate !== null && $return instanceof AggregateRoot) {
throw new AggregateAlreadySet();
}
if ($this->aggregate === null) {
$this->aggregate = $return;
}
} catch (Throwable $throwable) {
$this->thrownException = $throwable;
}
return $this;
}
/**
* @param object|Closure(object): void ...$events Expected events and/or callbacks for aggregate state assertions.
* Closures receive the aggregate instance and are executed after event assertions.
* Event order is preserved regardless of callback placement.
*/
final public function then(object ...$events): self
{
$this->thenExpectations = $events;
return $this;
}
/** @param class-string<Throwable> $exception */
final public function expectsException(string $exception): self
{
$this->expectedException = $exception;
return $this;
}
final public function expectsExceptionMessage(string $exceptionMessage): self
{
$this->expectedExceptionMessage = $exceptionMessage;
return $this;
}
#[After]
final public function assert(): self
{
if ($this->when === null) {
throw new NoWhenProvided();
}
if ($this->thrownException !== null) {
$this->handleException($this->thrownException);
return $this;
}
$this->expectedExceptionWasNotThrown();
if (!$this->aggregate instanceof AggregateRoot) {
throw new NoAggregateCreated();
}
$expectedEvents = array_values(array_filter($this->thenExpectations, static fn (object $item) => !$item instanceof Closure));
$expectationCallbacks = array_filter($this->thenExpectations, static fn (object $item) => $item instanceof Closure);
$events = $this->aggregate->releaseEvents();
self::assertEquals($expectedEvents, $events, 'The events doesn\'t match the expected events.');
foreach ($expectationCallbacks as $callback) {
try {
$callback($this->aggregate);
} catch (Throwable $t) {
$reflection = new ReflectionFunction($callback);
self::fail(
sprintf(
'then() callback defined in %s on line %d failed: %s',
$reflection->getFileName(),
$reflection->getStartLine(),
$t->getMessage(),
),
);
}
}
return $this;
}
#[Before]
final public function reset(): void
{
$this->givenEvents = [];
$this->when = null;
$this->parameters = [];
$this->thenExpectations = [];
$this->expectedException = null;
$this->expectedExceptionMessage = null;
$this->aggregate = null;
$this->thrownException = null;
}
private function handleException(Throwable $throwable): void
{
$checked = false;
if ($this->expectedException) {
self::assertThat($throwable, new ExceptionConstraint($this->expectedException));
$checked = true;
}
if ($this->expectedExceptionMessage) {
self::assertThat($throwable->getMessage(), new ExceptionMessageIsOrContains($this->expectedExceptionMessage));
$checked = true;
}
if (!$checked) {
throw $throwable;
}
}
private function expectedExceptionWasNotThrown(): void
{
if ($this->expectedException !== null) {
self::assertThat(null, new ExceptionConstraint($this->expectedException));
}
if ($this->expectedExceptionMessage === null) {
return;
}
self::assertThat(
null,
new ExceptionMessageIsOrContains($this->expectedExceptionMessage),
sprintf(
'Failed asserting that exception with message "%s" is thrown',
$this->expectedExceptionMessage,
),
);
}
}