1 | <?php |
||
27 | class BddAggregateTestHelper |
||
28 | { |
||
29 | private $aggregateId; |
||
30 | |||
31 | /** @var EventDispatcher */ |
||
32 | private $eventDispatcher; |
||
33 | |||
34 | private $priorEvents = []; |
||
35 | |||
36 | /** @var Command */ |
||
37 | private $command; |
||
38 | private $aggregate; |
||
39 | |||
40 | /** @var EventsApplierOnAggregate */ |
||
41 | private $eventsApplierOnAggregate; |
||
42 | |||
43 | /** @var CommandApplier */ |
||
44 | private $commandApplier; |
||
45 | /** |
||
46 | * @var CommandSubscriber |
||
47 | */ |
||
48 | private $commandSubscriber; |
||
49 | |||
50 | 9 | public function __construct( |
|
62 | |||
63 | 8 | private function getCommandSubscriber(): CommandSubscriber |
|
67 | |||
68 | 9 | public function onAggregate($aggregate) |
|
73 | |||
74 | 9 | public function given(...$priorEvents) |
|
78 | |||
79 | /** |
||
80 | * @param Event[] $priorEvents |
||
81 | * @return EventWithMetaData[] |
||
82 | */ |
||
83 | private function decorateEventsWithMetadata(array $priorEvents) |
||
89 | |||
90 | 8 | public function when(Command $command) |
|
94 | |||
95 | 4 | public function then(...$expectedEvents) |
|
96 | { |
||
97 | 4 | $this->checkCommand($this->command); |
|
98 | |||
99 | 4 | $this->eventsApplierOnAggregate->applyEventsOnAggregate($this->aggregate, $this->priorEvents); |
|
100 | |||
101 | 4 | $newEvents = $this->executeCommand($this->command); |
|
102 | |||
103 | 4 | $this->assertTheseEvents($expectedEvents, $newEvents); |
|
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * @param Command $command |
||
108 | * @return array |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | 4 | public function executeCommand(Command $command) |
|
112 | { |
||
113 | 4 | $handler = $this->getCommandSubscriber()->getHandlerForCommand($command); |
|
114 | |||
115 | 4 | $newEventsGenerator = $this->commandApplier->applyCommand($this->aggregate, $command, $handler->getMethodName()); |
|
116 | |||
117 | /** @var EventWithMetaData[] $eventsWithMetaData */ |
||
118 | 4 | $eventsWithMetaData = []; |
|
119 | |||
120 | 4 | $newEvents = []; |
|
121 | |||
122 | 4 | foreach ($newEventsGenerator as $event) { |
|
123 | 4 | $eventWithMetaData = $this->decorateEventWithMetaData($event); |
|
124 | |||
125 | 4 | $this->eventsApplierOnAggregate->applyEventsOnAggregate($this->aggregate, [$eventWithMetaData]); |
|
126 | |||
127 | 4 | $eventsWithMetaData[] = $eventWithMetaData; |
|
128 | 4 | $newEvents[] = $event; |
|
129 | } |
||
130 | |||
131 | 4 | foreach ($eventsWithMetaData as $eventWithMetaData) { |
|
132 | 4 | $this->eventDispatcher->dispatchEvent($eventWithMetaData); |
|
133 | } |
||
134 | |||
135 | 4 | return $newEvents; |
|
136 | } |
||
137 | |||
138 | 9 | private function decorateEventWithMetaData($event): EventWithMetaData |
|
142 | |||
143 | 5 | public function thenShouldFailWith($exceptionClass, $message = null) |
|
144 | { |
||
145 | 5 | $this->checkCommand($this->command); |
|
146 | |||
147 | try { |
||
148 | 4 | $handler = $this->getCommandSubscriber()->getHandlerForCommand($this->command); |
|
149 | |||
150 | 4 | $this->eventsApplierOnAggregate->applyEventsOnAggregate($this->aggregate, $this->priorEvents); |
|
151 | |||
152 | 4 | iterator_to_array( |
|
153 | 4 | $this->commandApplier->applyCommand( |
|
154 | 4 | $this->aggregate, $this->command, $handler->getMethodName())); |
|
155 | |||
156 | 1 | throw new NoExceptionThrown( |
|
157 | 1 | sprintf("Exception '%s' was is expected, but none was thrown", $exceptionClass)); |
|
158 | |||
159 | 4 | } catch (\Throwable $thrownException) { |
|
160 | |||
161 | 4 | if ($thrownException instanceof NoExceptionThrown) { |
|
162 | 1 | throw $thrownException;//rethrown |
|
163 | } |
||
164 | |||
165 | 3 | if (!$this->isClassOrSubClass($exceptionClass, $thrownException)) { |
|
166 | 1 | throw new WrongExceptionClassThrown( |
|
167 | 1 | sprintf( |
|
168 | 1 | "Exception '%s' was expected, but '%s(%s)' was thrown", |
|
169 | 1 | $exceptionClass, |
|
170 | 1 | get_class($thrownException), |
|
171 | 1 | $thrownException->getMessage())); |
|
172 | } |
||
173 | |||
174 | 2 | if ($message && $thrownException->getMessage() != $message) { |
|
175 | 1 | throw new WrongExceptionMessageWasThrown( |
|
176 | 1 | sprintf( |
|
177 | 1 | "Exception with message '%s' was expected, but '%s' was thrown", |
|
178 | 1 | $message, |
|
179 | 1 | $thrownException->getMessage())); |
|
180 | } |
||
181 | } |
||
182 | 1 | } |
|
183 | |||
184 | 4 | public function assertTheseEvents(array $expectedEvents, array $actualEvents) |
|
192 | |||
193 | 4 | private function checkForToFewEvents(array $expectedEvents, array $actualEvents) |
|
209 | |||
210 | 2 | private function checkForToManyEvents(int $additionalCount) |
|
217 | |||
218 | 4 | public function hashEvent($event) |
|
222 | |||
223 | 9 | private function factoryMetaData(): MetaData |
|
229 | |||
230 | 3 | private function isClassOrSubClass(string $parentClass, $childClass): bool |
|
234 | |||
235 | 9 | private function checkCommand($command) |
|
241 | } |