-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathQueryBusTest.php
393 lines (332 loc) · 13.4 KB
/
QueryBusTest.php
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
* This file is part of prooph/service-bus.
* (c) 2014-2021 Alexander Miertsch <[email protected]>
* (c) 2015-2021 Sascha-Oliver Prolic <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ProophTest\ServiceBus;
use PHPUnit\Framework\TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\ServiceBus\Exception\MessageDispatchException;
use Prooph\ServiceBus\Exception\RuntimeException;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\InvokeStrategy\FinderInvokeStrategy;
use Prooph\ServiceBus\QueryBus;
use ProophTest\ServiceBus\Mock\CustomMessage;
use ProophTest\ServiceBus\Mock\ErrorProducer;
use ProophTest\ServiceBus\Mock\FetchSomething;
use ProophTest\ServiceBus\Mock\Finder;
use React\Promise\Deferred;
use React\Promise\Promise;
class QueryBusTest extends TestCase
{
/**
* @var QueryBus
*/
private $queryBus;
protected function setUp(): void
{
$this->queryBus = new QueryBus();
}
/**
* @test
*/
public function it_dispatches_a_message_using_the_default_process(): void
{
$fetchSomething = new FetchSomething(['filter' => 'todo']);
$receivedMessage = null;
$dispatchEvent = null;
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use (&$receivedMessage, &$dispatchEvent): void {
$actionEvent->setParam(
MessageBus::EVENT_PARAM_MESSAGE_HANDLER,
function (FetchSomething $fetchSomething, Deferred $deferred) use (&$receivedMessage): void {
$deferred->resolve($fetchSomething);
}
);
$dispatchEvent = $actionEvent;
},
MessageBus::PRIORITY_ROUTE
);
$promise = $this->queryBus->dispatch($fetchSomething);
$promise->then(function ($result) use (&$receivedMessage): void {
$receivedMessage = $result;
});
$this->assertSame($fetchSomething, $receivedMessage);
$this->assertTrue($dispatchEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED));
}
/**
* @test
*/
public function it_triggers_all_defined_action_events(): void
{
$initializeIsTriggered = false;
$detectMessageNameIsTriggered = false;
$routeIsTriggered = false;
$locateHandlerIsTriggered = false;
$invokeFinderIsTriggered = false;
$finalizeIsTriggered = false;
//Should always be triggered
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use (&$initializeIsTriggered): void {
$initializeIsTriggered = true;
},
MessageBus::PRIORITY_INITIALIZE
);
//Should be triggered because we dispatch a message that does not
//implement Prooph\Common\Messaging\HasMessageName
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use (&$detectMessageNameIsTriggered): void {
$detectMessageNameIsTriggered = true;
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_NAME, 'custom-message');
},
MessageBus::PRIORITY_DETECT_MESSAGE_NAME
);
//Should be triggered because we did not provide a message-handler yet
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use (&$routeIsTriggered): void {
$routeIsTriggered = true;
if ($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME) === 'custom-message') {
//We provide the message handler as a string (service id) to tell the bus to trigger the locate-handler event
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, 'error-producer');
}
},
MessageBus::PRIORITY_ROUTE
);
//Should be triggered because we provided the message-handler as string (service id)
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use (&$locateHandlerIsTriggered): void {
$locateHandlerIsTriggered = true;
if ($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER) === 'error-producer') {
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, new ErrorProducer());
}
},
MessageBus::PRIORITY_LOCATE_HANDLER
);
//Should be triggered because the message-handler is not callable
$this->queryBus->attach(
QueryBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) use (&$invokeFinderIsTriggered): void {
$invokeFinderIsTriggered = true;
$handler = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER);
if ($handler instanceof ErrorProducer) {
$handler->throwException($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE));
}
},
QueryBus::PRIORITY_INVOKE_HANDLER
);
//Should always be triggered
$this->queryBus->attach(
MessageBus::EVENT_FINALIZE,
function (ActionEvent $actionEvent) use (&$finalizeIsTriggered): void {
if ($actionEvent->getParam(MessageBus::EVENT_PARAM_EXCEPTION) instanceof \Throwable) {
$actionEvent->setParam(MessageBus::EVENT_PARAM_EXCEPTION, null);
}
$finalizeIsTriggered = true;
},
1000 // high priority
);
$customMessage = new CustomMessage('I have no further meaning');
$this->queryBus->dispatch($customMessage);
$this->assertTrue($initializeIsTriggered);
$this->assertTrue($detectMessageNameIsTriggered);
$this->assertTrue($routeIsTriggered);
$this->assertTrue($locateHandlerIsTriggered);
$this->assertTrue($invokeFinderIsTriggered);
$this->assertTrue($finalizeIsTriggered);
}
/**
* @test
*/
public function it_uses_the_fqcn_of_the_message_if_message_name_was_not_provided_and_message_does_not_implement_has_message_name(): void
{
$handler = new Finder();
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $e) use ($handler): void {
if ($e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME) === CustomMessage::class) {
$e->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $handler);
}
},
MessageBus::PRIORITY_ROUTE
);
(new FinderInvokeStrategy())->attachToMessageBus($this->queryBus);
$customMessage = new CustomMessage('foo');
$promise = $this->queryBus->dispatch($customMessage);
$this->assertSame($customMessage, $handler->getLastMessage());
$this->assertInstanceOf(Promise::class, $promise);
$this->assertInstanceOf(Deferred::class, $handler->getLastDeferred());
}
/**
* @test
*/
public function it_rejects_the_deferred_with_a_service_bus_exception_if_exception_is_not_handled_by_a_plugin(): void
{
$exception = null;
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function () {
throw new \Exception('ka boom');
},
MessageBus::PRIORITY_INITIALIZE
);
$promise = $this->queryBus->dispatch('throw it');
$promise->then(
function () {
},
function (\Throwable $ex) use (&$exception): void {
$exception = $ex;
}
);
$this->assertInstanceOf(MessageDispatchException::class, $exception);
}
/**
* @test
*/
public function it_throws_exception_if_event_has_no_handler_after_it_has_been_set_and_event_was_triggered(): void
{
$exception = null;
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $e): void {
$e->setParam(QueryBus::EVENT_PARAM_MESSAGE_HANDLER, null);
},
MessageBus::PRIORITY_INITIALIZE
);
$promise = $this->queryBus->dispatch('throw it');
$promise->then(
function () {
},
function (\Throwable $ex) use (&$exception): void {
$exception = $ex;
}
);
$this->assertInstanceOf(RuntimeException::class, $exception);
$this->assertEquals('Message dispatch failed. See previous exception for details.', $exception->getMessage());
}
/**
* @test
*/
public function it_throws_exception_if_event_has_stopped_propagation(): void
{
$exception = null;
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $e): void {
throw new \RuntimeException('throw it!');
},
MessageBus::PRIORITY_INITIALIZE
);
$promise = $this->queryBus->dispatch('throw it');
$promise->then(
function () {
},
function (\Throwable $ex) use (&$exception): void {
$exception = $ex;
}
);
$this->assertInstanceOf(MessageDispatchException::class, $exception);
$this->assertEquals('Message dispatch failed. See previous exception for details.', $exception->getMessage());
$this->assertInstanceOf(\RuntimeException::class, $exception->getPrevious());
$this->assertEquals('throw it!', $exception->getPrevious()->getMessage());
}
/**
* @test
*/
public function it_can_deactive_an_action_event_listener_aggregate(): void
{
$handler = new Finder();
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $e) use ($handler): void {
if ($e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME) === CustomMessage::class) {
$e->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, $handler);
}
},
MessageBus::PRIORITY_ROUTE
);
$plugin = new FinderInvokeStrategy();
$plugin->attachToMessageBus($this->queryBus);
$plugin->detachFromMessageBus($this->queryBus);
$customMessage = new CustomMessage('foo');
$promise = $this->queryBus->dispatch($customMessage);
$this->assertNull($handler->getLastMessage());
$this->assertInstanceOf(Promise::class, $promise);
$this->assertNull($handler->getLastDeferred());
}
/**
* @test
*/
public function it_throws_exception_if_message_was_not_handled(): void
{
$this->expectException(RuntimeException::class);
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $e): void {
$e->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, new \stdClass());
},
MessageBus::PRIORITY_INITIALIZE
);
$promise = $this->queryBus->dispatch('throw it');
$promise->done();
}
/**
* @test
*/
public function it_could_reset_exception_before_promise_becomes_rejected(): void
{
$exceptionParamWasSet = false;
$this->queryBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent) {
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLER, function (): void {
throw new \Exception('Unset me!');
});
},
MessageBus::PRIORITY_INITIALIZE
);
$this->queryBus->attach(
MessageBus::EVENT_FINALIZE,
function (ActionEvent $actionEvent) use (&$exceptionParamWasSet): void {
if ($actionEvent->getParam(MessageBus::EVENT_PARAM_EXCEPTION) instanceof \Throwable) {
$exceptionParamWasSet = true;
$actionEvent->setParam(MessageBus::EVENT_PARAM_EXCEPTION, null);
}
},
MessageBus::PRIORITY_PROMISE_REJECT + 1
);
$promise = $this->queryBus->dispatch('throw an exception!');
$promise->done();
$this->assertTrue($exceptionParamWasSet);
}
/**
* @test
*/
public function it_always_triggers_finalize_listeners_regardless_whether_the_propagation_of_the_event_has_been_stopped(): void
{
$this->queryBus->attach(QueryBus::EVENT_DISPATCH, function (ActionEvent $event) {
$event->setParam(QueryBus::EVENT_PARAM_MESSAGE_HANDLER, function (): void {
});
}, QueryBus::PRIORITY_LOCATE_HANDLER + 1);
$this->queryBus->attach(QueryBus::EVENT_DISPATCH, function (ActionEvent $event): void {
$event->stopPropagation();
}, QueryBus::PRIORITY_INVOKE_HANDLER - 1);
$this->queryBus->attach(MessageBus::EVENT_FINALIZE, function (): void {
}, 3);
$finalizeHasBeenCalled = false;
$this->queryBus->attach(MessageBus::EVENT_FINALIZE, function () use (&$finalizeHasBeenCalled): void {
$finalizeHasBeenCalled = true;
}, 2);
$this->queryBus->dispatch('a message');
$this->assertTrue($finalizeHasBeenCalled);
}
}