1 | <?php |
||
51 | abstract class Fixture implements MessageCallback |
||
|
|||
52 | { |
||
53 | const EVENT_NUMBER_MISMATCH = 'Raised and expected number of events mismatches'; |
||
54 | |||
55 | /** |
||
56 | * @var DomainEvent[] |
||
57 | */ |
||
58 | private $then = []; |
||
59 | |||
60 | /** |
||
61 | * @var DomainEvent[] |
||
62 | */ |
||
63 | private $raisedEvents = []; |
||
64 | |||
65 | /** |
||
66 | * @var Command |
||
67 | */ |
||
68 | private $command; |
||
69 | |||
70 | /** |
||
71 | * @var CommandBus |
||
72 | */ |
||
73 | private $commandBus; |
||
74 | |||
75 | /** |
||
76 | * @var EventBus |
||
77 | */ |
||
78 | private $eventBus; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $aggregateClass; |
||
84 | |||
85 | /** |
||
86 | * @var mixed |
||
87 | */ |
||
88 | private $expectedReturnValue; |
||
89 | |||
90 | /** |
||
91 | * @var mixed |
||
92 | */ |
||
93 | private $commandResult; |
||
94 | |||
95 | /** |
||
96 | * @var bool |
||
97 | */ |
||
98 | private $checkReturnValue = false; |
||
99 | |||
100 | /** |
||
101 | * @var null|string |
||
102 | */ |
||
103 | private $expectedExceptionClass = null; |
||
104 | |||
105 | /** |
||
106 | * @var null|string |
||
107 | */ |
||
108 | private $expectedExceptionMessage = null; |
||
109 | |||
110 | /** |
||
111 | * @var null|Exception |
||
112 | */ |
||
113 | private $raisedException; |
||
114 | |||
115 | /** |
||
116 | * @var null|Repository |
||
117 | */ |
||
118 | private $repository; |
||
119 | |||
120 | /** |
||
121 | * @var GenericAggregateId|null |
||
122 | */ |
||
123 | private $aggregateId; |
||
124 | |||
125 | public function __construct($aggregateClass, Repository $repository = null) |
||
136 | |||
137 | /** |
||
138 | * These commands will be initialize the AR. The raised domain events will be ignored. |
||
139 | * It works only with non-ES aggregates. |
||
140 | * |
||
141 | * @param Command $commands one ore more commands |
||
142 | * @throws BadMethodCallException |
||
143 | * @return CommandSourcedFixture |
||
144 | */ |
||
145 | public function givenCommands(Command $commands) |
||
149 | |||
150 | /** |
||
151 | * These events will be initialize the AR. It works only with ES aggregates. |
||
152 | * |
||
153 | * @param DomainEvent $events |
||
154 | * @throws BadMethodCallException |
||
155 | * @return EventSourcedFixture |
||
156 | */ |
||
157 | public function givenEvents(DomainEvent $events) |
||
161 | |||
162 | /** |
||
163 | * Returns the registered or dynamically generated repository. |
||
164 | * |
||
165 | * @return null|Repository |
||
166 | */ |
||
167 | public function getRepository() |
||
171 | |||
172 | /** |
||
173 | * These events are expected to be raised. Starts the evaluation. |
||
174 | * |
||
175 | * @param DomainEvent $events one or more events |
||
176 | */ |
||
177 | public function expectEvents(DomainEvent $events) |
||
182 | |||
183 | /** |
||
184 | * No expected events. Starts the evaluation. |
||
185 | */ |
||
186 | public function expectNoEvents() |
||
190 | |||
191 | /** |
||
192 | * The AR's behavior will be tested when this command is applied. |
||
193 | * |
||
194 | * @param Command $command |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function when(Command $command) |
||
205 | |||
206 | /** |
||
207 | * The given value must be returned by the command handler. |
||
208 | * |
||
209 | * @param mixed $value |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function expectReturnValue($value) |
||
218 | |||
219 | /** |
||
220 | * The given type of exception must be be thrown when the command is applied. Starts the evaluation. |
||
221 | * |
||
222 | * @param string $class the type of the exception |
||
223 | * @param null|string $message the exception message |
||
224 | */ |
||
225 | public function expectException($class, $message = null) |
||
231 | |||
232 | /** |
||
233 | * The command handler which can handle the tested command. |
||
234 | * If the command implements {@link DirectCommand} and is processed directly |
||
235 | * by the AR, does not need to be used. |
||
236 | * |
||
237 | * @param $handler |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function registerAnnotatedCommandHandler($handler) |
||
245 | |||
246 | /** |
||
247 | * The same as {@link registerAnnotatedCommandHandler()} but with closure. |
||
248 | * |
||
249 | * @param Closure $closure |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function registerCommandClosure(Closure $closure) |
||
257 | |||
258 | /** |
||
259 | * The aggregate ID generated by the AR, or defined somehow else. |
||
260 | * Can be obtained in order to do further tests if required. |
||
261 | * |
||
262 | * @return AggregateId |
||
263 | */ |
||
264 | public function getAggregateId() |
||
268 | |||
269 | /** |
||
270 | * @param mixed $result |
||
271 | * @return void |
||
272 | */ |
||
273 | public function onSuccess($result) |
||
277 | |||
278 | /** |
||
279 | * @param Exception $exception |
||
280 | * @return void |
||
281 | */ |
||
282 | public function onFailure(Exception $exception) |
||
286 | |||
287 | /** |
||
288 | * @Subscribe |
||
289 | * @param DomainEvent $event |
||
290 | */ |
||
291 | public function catchEvent(DomainEvent $event) |
||
296 | |||
297 | /** |
||
298 | * @param AggregateId $aggregateId |
||
299 | */ |
||
300 | protected function setAggregateId(AggregateId $aggregateId) |
||
304 | |||
305 | /** |
||
306 | * @return CommandBus |
||
307 | */ |
||
308 | protected function getCommandBus() |
||
312 | |||
313 | /** |
||
314 | * @return EventBus |
||
315 | */ |
||
316 | protected function getEventBus() |
||
320 | |||
321 | /** |
||
322 | * @return ObjectClass |
||
323 | */ |
||
324 | protected function getAggregateClass() |
||
328 | |||
329 | private function evaluate() |
||
337 | |||
338 | private function checkReturnValue() |
||
344 | |||
345 | private function checkThrownException() |
||
358 | |||
359 | private function checkRaisedEvents() |
||
376 | } |
||
377 | |||
398 |