1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Messenger\Test\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
7
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
8
|
|
|
use Symfony\Component\Messenger\Envelope; |
9
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
10
|
|
|
use Symfony\Component\Messenger\Stamp\DelayStamp; |
11
|
|
|
use Zenstruck\Messenger\Test\InteractsWithMessenger; |
12
|
|
|
use Zenstruck\Messenger\Test\TestEnvelope; |
13
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageA; |
14
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageAHandler; |
15
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageB; |
16
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageBHandler; |
17
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageC; |
18
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageD; |
19
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageE; |
20
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageF; |
21
|
|
|
use Zenstruck\Messenger\Test\Tests\Fixture\NoBundleKernel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Kevin Bond <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class InteractsWithMessengerTest extends WebTestCase |
27
|
|
|
{ |
28
|
|
|
use InteractsWithMessenger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function can_interact_with_queue(): void |
34
|
|
|
{ |
35
|
|
|
self::bootKernel(); |
36
|
|
|
|
37
|
|
|
$this->messenger()->queue()->assertEmpty(); |
38
|
|
|
$this->assertEmpty(self::$container->get(MessageAHandler::class)->messages); |
39
|
|
|
$this->assertEmpty(self::$container->get(MessageBHandler::class)->messages); |
40
|
|
|
|
41
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
42
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
43
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
44
|
|
|
|
45
|
|
|
$this->messenger()->queue()->assertCount(3); |
46
|
|
|
$this->messenger()->queue()->assertContains(MessageA::class); |
47
|
|
|
$this->messenger()->queue()->assertContains(MessageA::class, 2); |
48
|
|
|
$this->messenger()->queue()->assertContains(MessageB::class); |
49
|
|
|
$this->messenger()->queue()->assertContains(MessageB::class, 1); |
50
|
|
|
$this->messenger()->queue()->assertNotContains(MessageC::class); |
51
|
|
|
$this->assertEmpty(self::$container->get(MessageAHandler::class)->messages); |
52
|
|
|
$this->assertEmpty(self::$container->get(MessageBHandler::class)->messages); |
53
|
|
|
|
54
|
|
|
$this->messenger()->process(2); |
55
|
|
|
|
56
|
|
|
$this->messenger()->queue()->assertCount(1); |
57
|
|
|
$this->messenger()->queue()->assertContains(MessageA::class, 1); |
58
|
|
|
$this->messenger()->queue()->assertNotContains(MessageB::class); |
59
|
|
|
$this->assertCount(1, self::$container->get(MessageAHandler::class)->messages); |
60
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
61
|
|
|
|
62
|
|
|
$this->messenger()->process(); |
63
|
|
|
|
64
|
|
|
$this->messenger()->queue()->assertEmpty(); |
65
|
|
|
$this->messenger()->queue()->assertNotContains(MessageA::class); |
66
|
|
|
$this->messenger()->queue()->assertNotContains(MessageB::class); |
67
|
|
|
$this->assertCount(2, self::$container->get(MessageAHandler::class)->messages); |
68
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function can_disable_intercept(): void |
75
|
|
|
{ |
76
|
|
|
self::bootKernel(); |
77
|
|
|
|
78
|
|
|
$this->messenger()->unblock(); |
79
|
|
|
|
80
|
|
|
$this->messenger()->queue()->assertEmpty(); |
81
|
|
|
$this->messenger()->acknowledged()->assertEmpty(); |
82
|
|
|
$this->messenger()->dispatched()->assertEmpty(); |
83
|
|
|
|
84
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
85
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
86
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
87
|
|
|
|
88
|
|
|
$this->messenger()->queue()->assertEmpty(); |
89
|
|
|
$this->messenger()->queue()->assertNotContains(MessageA::class); |
90
|
|
|
$this->messenger()->dispatched()->assertCount(3); |
91
|
|
|
$this->messenger()->dispatched()->assertContains(MessageA::class, 2); |
92
|
|
|
$this->messenger()->dispatched()->assertContains(MessageB::class, 1); |
93
|
|
|
$this->messenger()->acknowledged()->assertCount(3); |
94
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageA::class, 2); |
95
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageB::class, 1); |
96
|
|
|
$this->assertCount(2, self::$container->get(MessageAHandler::class)->messages); |
97
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
*/ |
103
|
|
|
public function disabling_intercept_with_items_on_queue_processes_all(): void |
104
|
|
|
{ |
105
|
|
|
self::bootKernel(); |
106
|
|
|
|
107
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
108
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
109
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
110
|
|
|
|
111
|
|
|
$this->messenger()->queue()->assertCount(3); |
112
|
|
|
|
113
|
|
|
$this->messenger()->process(); |
114
|
|
|
|
115
|
|
|
$this->messenger()->queue()->assertEmpty(); |
116
|
|
|
$this->assertCount(2, self::$container->get(MessageAHandler::class)->messages); |
117
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @test |
122
|
|
|
*/ |
123
|
|
|
public function unblocking_processes_existing_messages_on_queue(): void |
124
|
|
|
{ |
125
|
|
|
self::bootKernel(); |
126
|
|
|
|
127
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
128
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
129
|
|
|
|
130
|
|
|
$this->messenger()->queue()->assertCount(2); |
131
|
|
|
$this->messenger()->acknowledged()->assertEmpty(); |
132
|
|
|
|
133
|
|
|
$this->messenger()->unblock(); |
134
|
|
|
|
135
|
|
|
$this->messenger()->queue()->assertEmpty(); |
136
|
|
|
$this->messenger()->acknowledged()->assertCount(2); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
*/ |
142
|
|
|
public function can_access_envelope_collection_items_via_first(): void |
143
|
|
|
{ |
144
|
|
|
self::bootKernel(); |
145
|
|
|
|
146
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m1 = new MessageA()); |
147
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m2 = new MessageB()); |
148
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m3 = new MessageA(true)); |
149
|
|
|
|
150
|
|
|
$this->messenger()->queue()->assertCount(3); |
151
|
|
|
|
152
|
|
|
$this->assertSame($m1, $this->messenger()->queue()->first()->getMessage()); |
|
|
|
|
153
|
|
|
$this->assertSame($m2, $this->messenger()->queue()->first(MessageB::class)->getMessage()); |
154
|
|
|
$this->assertSame($m3, $this->messenger()->queue()->first(fn(Envelope $e) => $e->getMessage()->fail)->getMessage()); |
155
|
|
|
$this->assertSame($m3, $this->messenger()->queue()->first(fn($e) => $e->getMessage()->fail)->getMessage()); |
156
|
|
|
$this->assertSame($m3, $this->messenger()->queue()->first(fn(MessageA $m) => $m->fail)->getMessage()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @test |
161
|
|
|
*/ |
162
|
|
|
public function envelope_collection_first_throws_exception_if_no_match(): void |
163
|
|
|
{ |
164
|
|
|
self::bootKernel(); |
165
|
|
|
|
166
|
|
|
$this->expectException(\RuntimeException::class); |
167
|
|
|
|
168
|
|
|
$this->messenger()->queue()->first(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
*/ |
174
|
|
|
public function can_make_stamp_assertions_on_test_envelope(): void |
175
|
|
|
{ |
176
|
|
|
self::bootKernel(); |
177
|
|
|
|
178
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA(), [new DelayStamp(1000)]); |
179
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
180
|
|
|
|
181
|
|
|
$this->messenger()->queue()->first()->assertHasStamp(DelayStamp::class); |
182
|
|
|
$this->messenger()->queue()->first(MessageB::class)->assertNotHasStamp(DelayStamp::class); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @test |
187
|
|
|
*/ |
188
|
|
|
public function cannot_access_queue_if_none_registered(): void |
189
|
|
|
{ |
190
|
|
|
self::bootKernel(['environment' => 'test']); |
191
|
|
|
|
192
|
|
|
$this->expectException(\LogicException::class); |
193
|
|
|
$this->expectExceptionMessage('No transports registered'); |
194
|
|
|
|
195
|
|
|
$this->messenger(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @test |
200
|
|
|
*/ |
201
|
|
|
public function accessing_transport_boots_kernel_if_not_yet_booted(): void |
202
|
|
|
{ |
203
|
|
|
$this->messenger()->queue()->assertEmpty(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @test |
208
|
|
|
*/ |
209
|
|
|
public function can_interact_with_multiple_queues(): void |
210
|
|
|
{ |
211
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
212
|
|
|
|
213
|
|
|
$this->messenger('async1')->queue()->assertEmpty(); |
214
|
|
|
$this->messenger('async2')->queue()->assertEmpty(); |
215
|
|
|
$this->assertEmpty(self::$container->get(MessageAHandler::class)->messages); |
216
|
|
|
$this->assertEmpty(self::$container->get(MessageBHandler::class)->messages); |
217
|
|
|
|
218
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
219
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
220
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
221
|
|
|
|
222
|
|
|
$this->messenger('async1')->queue()->assertCount(2); |
223
|
|
|
$this->messenger('async2')->queue()->assertEmpty(); |
224
|
|
|
$this->messenger('async1')->queue()->assertContains(MessageA::class); |
225
|
|
|
$this->messenger('async1')->queue()->assertContains(MessageA::class, 2); |
226
|
|
|
$this->messenger('async2')->queue()->assertNotContains(MessageB::class); |
227
|
|
|
$this->assertEmpty(self::$container->get(MessageAHandler::class)->messages); |
228
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
229
|
|
|
|
230
|
|
|
$this->messenger('async1')->process(1); |
231
|
|
|
|
232
|
|
|
$this->messenger('async1')->queue()->assertCount(1); |
233
|
|
|
$this->messenger('async1')->queue()->assertContains(MessageA::class, 1); |
234
|
|
|
$this->messenger('async2')->queue()->assertNotContains(MessageB::class); |
235
|
|
|
$this->assertCount(1, self::$container->get(MessageAHandler::class)->messages); |
236
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
237
|
|
|
|
238
|
|
|
$this->messenger('async1')->process(); |
239
|
|
|
|
240
|
|
|
$this->messenger('async1')->queue()->assertEmpty(); |
241
|
|
|
$this->messenger('async2')->queue()->assertEmpty(); |
242
|
|
|
$this->messenger('async2')->acknowledged()->assertCount(1); |
243
|
|
|
$this->messenger('async2')->acknowledged()->assertContains(MessageB::class, 1); |
244
|
|
|
$this->assertCount(2, self::$container->get(MessageAHandler::class)->messages); |
245
|
|
|
$this->assertCount(1, self::$container->get(MessageBHandler::class)->messages); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @test |
250
|
|
|
*/ |
251
|
|
|
public function can_enable_intercept(): void |
252
|
|
|
{ |
253
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
254
|
|
|
|
255
|
|
|
$this->messenger('async2')->intercept(); |
256
|
|
|
|
257
|
|
|
$this->messenger('async2')->queue()->assertEmpty(); |
258
|
|
|
$this->assertEmpty(self::$container->get(MessageBHandler::class)->messages); |
259
|
|
|
|
260
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
261
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB()); |
262
|
|
|
|
263
|
|
|
$this->messenger('async2')->queue()->assertCount(2); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @test |
268
|
|
|
*/ |
269
|
|
|
public function cannot_access_queue_that_does_not_exist(): void |
270
|
|
|
{ |
271
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
272
|
|
|
|
273
|
|
|
$this->expectException(\InvalidArgumentException::class); |
274
|
|
|
$this->expectExceptionMessage('"invalid" not registered'); |
275
|
|
|
|
276
|
|
|
$this->messenger('invalid'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @test |
281
|
|
|
*/ |
282
|
|
|
public function cannot_access_queue_that_is_not_test_transport(): void |
283
|
|
|
{ |
284
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
285
|
|
|
|
286
|
|
|
$this->expectException(\LogicException::class); |
287
|
|
|
$this->expectExceptionMessage('Transport "async3" needs to be set to "test://" in your test config to use this feature.'); |
288
|
|
|
|
289
|
|
|
$this->messenger('async3'); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @test |
294
|
|
|
*/ |
295
|
|
|
public function queue_name_is_required_if_using_multiple_transports(): void |
296
|
|
|
{ |
297
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
298
|
|
|
|
299
|
|
|
$this->expectException(\LogicException::class); |
300
|
|
|
$this->expectExceptionMessage('Multiple transports are registered (async1, async2, async3), you must specify a name.'); |
301
|
|
|
|
302
|
|
|
$this->messenger(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @test |
307
|
|
|
*/ |
308
|
|
|
public function can_access_message_objects_on_queue(): void |
309
|
|
|
{ |
310
|
|
|
self::bootKernel(); |
311
|
|
|
|
312
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m1 = new MessageA()); |
313
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m2 = new MessageB()); |
314
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m3 = new MessageA()); |
315
|
|
|
|
316
|
|
|
$this->assertSame([$m1, $m2, $m3], $this->messenger()->queue()->messages()); |
317
|
|
|
$this->assertSame([$m1, $m3], $this->messenger()->queue()->messages(MessageA::class)); |
318
|
|
|
$this->assertSame([$m2], $this->messenger()->queue()->messages(MessageB::class)); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @test |
323
|
|
|
*/ |
324
|
|
|
public function can_access_envelopes_on_envelope_collection(): void |
325
|
|
|
{ |
326
|
|
|
self::bootKernel(); |
327
|
|
|
|
328
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m1 = new MessageA()); |
329
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m2 = new MessageB()); |
330
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m3 = new MessageA()); |
331
|
|
|
|
332
|
|
|
$messages = \array_map(fn(TestEnvelope $envelope) => $envelope->getMessage(), $this->messenger()->queue()->all()); |
333
|
|
|
$messagesFromIterator = \array_map(fn(TestEnvelope $envelope) => $envelope->getMessage(), \iterator_to_array($this->messenger()->queue())); |
334
|
|
|
|
335
|
|
|
$this->assertSame([$m1, $m2, $m3], $messages); |
336
|
|
|
$this->assertSame([$m1, $m2, $m3], $messagesFromIterator); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @test |
341
|
|
|
*/ |
342
|
|
|
public function can_access_sent_acknowledged_and_rejected(): void |
343
|
|
|
{ |
344
|
|
|
self::bootKernel(); |
345
|
|
|
|
346
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m1 = new MessageA(true)); |
347
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch($m2 = new MessageB()); |
348
|
|
|
|
349
|
|
|
$this->assertCount(2, $this->messenger()->queue()); |
350
|
|
|
$this->assertCount(2, $this->messenger()->dispatched()); |
351
|
|
|
$this->assertCount(0, $this->messenger()->acknowledged()); |
352
|
|
|
$this->assertCount(0, $this->messenger()->rejected()); |
353
|
|
|
|
354
|
|
|
$this->messenger()->process(); |
355
|
|
|
|
356
|
|
|
$this->assertCount(0, $this->messenger()->queue()); |
357
|
|
|
$this->assertCount(2, $this->messenger()->dispatched()); |
358
|
|
|
$this->assertCount(1, $this->messenger()->acknowledged()); |
359
|
|
|
$this->assertCount(1, $this->messenger()->rejected()); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @test |
364
|
|
|
*/ |
365
|
|
|
public function cannot_access_queue_if_bundle_not_enabled(): void |
366
|
|
|
{ |
367
|
|
|
self::$class = NoBundleKernel::class; |
368
|
|
|
self::bootKernel(['environment' => 'no_bundle']); |
369
|
|
|
self::$class = null; |
370
|
|
|
|
371
|
|
|
$this->expectException(\LogicException::class); |
372
|
|
|
$this->expectExceptionMessage('Cannot access transport - is ZenstruckMessengerTestBundle enabled in your test environment?'); |
373
|
|
|
|
374
|
|
|
$this->messenger(); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @test |
379
|
|
|
*/ |
380
|
|
|
public function can_configure_throwing_exceptions(): void |
381
|
|
|
{ |
382
|
|
|
self::bootKernel(); |
383
|
|
|
|
384
|
|
|
$this->messenger()->throwExceptions(); |
385
|
|
|
|
386
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA(true)); |
387
|
|
|
|
388
|
|
|
$this->expectException(\RuntimeException::class); |
389
|
|
|
$this->expectExceptionMessage('handling failed...'); |
390
|
|
|
|
391
|
|
|
$this->messenger()->process(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @test |
396
|
|
|
*/ |
397
|
|
|
public function can_configure_throwing_exceptions_with_intercept_disabled(): void |
398
|
|
|
{ |
399
|
|
|
self::bootKernel(); |
400
|
|
|
|
401
|
|
|
$this->messenger()->throwExceptions()->unblock(); |
402
|
|
|
|
403
|
|
|
$this->expectException(\RuntimeException::class); |
404
|
|
|
$this->expectExceptionMessage('handling failed...'); |
405
|
|
|
|
406
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA(true)); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @test |
411
|
|
|
*/ |
412
|
|
|
public function can_disable_exception_catching_in_transport_config(): void |
413
|
|
|
{ |
414
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
415
|
|
|
|
416
|
|
|
$this->expectException(\RuntimeException::class); |
417
|
|
|
$this->expectExceptionMessage('handling failed...'); |
418
|
|
|
|
419
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB(true)); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @test |
424
|
|
|
*/ |
425
|
|
|
public function can_re_enable_exception_catching_if_disabled_in_transport_config(): void |
426
|
|
|
{ |
427
|
|
|
self::bootKernel(['environment' => 'multi_transport']); |
428
|
|
|
|
429
|
|
|
$this->messenger('async2')->catchExceptions(); |
430
|
|
|
|
431
|
|
|
$this->messenger('async2')->rejected()->assertEmpty(); |
432
|
|
|
|
433
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageB(true)); |
434
|
|
|
|
435
|
|
|
$this->messenger('async2')->rejected()->assertCount(1); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @test |
440
|
|
|
*/ |
441
|
|
|
public function transport_data_is_persisted_between_requests_and_kernel_shutdown(): void |
442
|
|
|
{ |
443
|
|
|
self::bootKernel(); |
444
|
|
|
|
445
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
446
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA(true)); |
447
|
|
|
|
448
|
|
|
$this->messenger()->queue()->assertCount(2); |
449
|
|
|
|
450
|
|
|
self::ensureKernelShutdown(); |
451
|
|
|
self::bootKernel(); |
452
|
|
|
|
453
|
|
|
$this->messenger()->queue()->assertCount(2); |
454
|
|
|
|
455
|
|
|
$this->messenger()->process(); |
456
|
|
|
|
457
|
|
|
self::ensureKernelShutdown(); |
458
|
|
|
self::bootKernel(); |
459
|
|
|
|
460
|
|
|
$this->messenger()->queue()->assertEmpty(); |
461
|
|
|
$this->messenger()->dispatched()->assertCount(2); |
462
|
|
|
$this->messenger()->acknowledged()->assertCount(1); |
463
|
|
|
$this->messenger()->rejected()->assertCount(1); |
464
|
|
|
|
465
|
|
|
self::ensureKernelShutdown(); |
466
|
|
|
|
467
|
|
|
$client = self::createClient(); |
468
|
|
|
|
469
|
|
|
$client->request('GET', '/dispatch'); |
470
|
|
|
|
471
|
|
|
$this->messenger()->queue()->assertCount(1); |
472
|
|
|
$this->messenger()->dispatched()->assertCount(3); |
473
|
|
|
$this->messenger()->acknowledged()->assertCount(1); |
474
|
|
|
$this->messenger()->rejected()->assertCount(1); |
475
|
|
|
|
476
|
|
|
$client->request('GET', '/dispatch'); |
477
|
|
|
|
478
|
|
|
$this->messenger()->queue()->assertCount(2); |
479
|
|
|
$this->messenger()->dispatched()->assertCount(4); |
480
|
|
|
$this->messenger()->acknowledged()->assertCount(1); |
481
|
|
|
$this->messenger()->rejected()->assertCount(1); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @test |
486
|
|
|
*/ |
487
|
|
|
public function can_reset_transport_data(): void |
488
|
|
|
{ |
489
|
|
|
self::bootKernel(); |
490
|
|
|
|
491
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
492
|
|
|
|
493
|
|
|
$this->messenger()->queue()->assertNotEmpty(); |
494
|
|
|
|
495
|
|
|
$this->messenger()->reset(); |
496
|
|
|
|
497
|
|
|
$this->messenger()->queue()->assertEmpty(); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @test |
502
|
|
|
*/ |
503
|
|
|
public function disabling_intercept_is_remembered_between_kernel_reboots(): void |
504
|
|
|
{ |
505
|
|
|
self::bootKernel(); |
506
|
|
|
|
507
|
|
|
$this->messenger()->unblock(); |
508
|
|
|
|
509
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
510
|
|
|
|
511
|
|
|
$this->messenger()->queue()->assertEmpty(); |
512
|
|
|
$this->messenger()->dispatched()->assertCount(1); |
513
|
|
|
|
514
|
|
|
self::ensureKernelShutdown(); |
515
|
|
|
self::bootKernel(); |
516
|
|
|
|
517
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
518
|
|
|
|
519
|
|
|
$this->messenger()->queue()->assertEmpty(); |
520
|
|
|
$this->messenger()->dispatched()->assertCount(2); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* @test |
525
|
|
|
*/ |
526
|
|
|
public function throwing_exceptions_is_remembered_between_kernel_reboots(): void |
527
|
|
|
{ |
528
|
|
|
self::bootKernel(); |
529
|
|
|
|
530
|
|
|
$this->messenger()->throwExceptions(); |
531
|
|
|
|
532
|
|
|
self::ensureKernelShutdown(); |
533
|
|
|
self::bootKernel(); |
534
|
|
|
|
535
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA(true)); |
536
|
|
|
|
537
|
|
|
$this->expectException(\RuntimeException::class); |
538
|
|
|
$this->expectErrorMessage('handling failed...'); |
539
|
|
|
|
540
|
|
|
$this->messenger()->process(); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* @test |
545
|
|
|
*/ |
546
|
|
|
public function can_manually_send_message_to_transport_and_process(): void |
547
|
|
|
{ |
548
|
|
|
self::bootKernel(); |
549
|
|
|
|
550
|
|
|
$this->messenger()->queue()->assertEmpty(); |
551
|
|
|
$this->assertEmpty(self::$container->get(MessageAHandler::class)->messages); |
552
|
|
|
|
553
|
|
|
$this->messenger()->send(Envelope::wrap(new MessageA())); |
554
|
|
|
|
555
|
|
|
$this->messenger()->queue()->assertCount(1); |
556
|
|
|
$this->assertEmpty(self::$container->get(MessageAHandler::class)->messages); |
557
|
|
|
|
558
|
|
|
$this->messenger()->process(); |
559
|
|
|
|
560
|
|
|
$this->messenger()->queue()->assertEmpty(); |
561
|
|
|
$this->assertCount(1, self::$container->get(MessageAHandler::class)->messages); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* @test |
566
|
|
|
*/ |
567
|
|
|
public function process_all_is_recursive(): void |
568
|
|
|
{ |
569
|
|
|
self::bootKernel(); |
570
|
|
|
|
571
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageD()); |
572
|
|
|
|
573
|
|
|
$this->messenger()->queue()->assertCount(1); |
574
|
|
|
$this->messenger()->queue()->assertContains(MessageD::class, 1); |
575
|
|
|
|
576
|
|
|
$this->messenger()->process(); |
577
|
|
|
|
578
|
|
|
$this->messenger()->queue()->assertEmpty(); |
579
|
|
|
$this->messenger()->dispatched()->assertCount(3); |
580
|
|
|
$this->messenger()->acknowledged()->assertCount(3); |
581
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageD::class, 1); |
582
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageE::class, 1); |
583
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageF::class, 1); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* @test |
588
|
|
|
*/ |
589
|
|
|
public function process_x_messages_is_recursive(): void |
590
|
|
|
{ |
591
|
|
|
self::bootKernel(); |
592
|
|
|
|
593
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageD()); |
594
|
|
|
|
595
|
|
|
$this->messenger()->queue()->assertCount(1); |
596
|
|
|
$this->messenger()->queue()->assertContains(MessageD::class, 1); |
597
|
|
|
|
598
|
|
|
$this->messenger()->process(1); |
599
|
|
|
|
600
|
|
|
$this->messenger()->queue()->assertCount(1); |
601
|
|
|
$this->messenger()->queue()->assertContains(MessageE::class, 1); |
602
|
|
|
$this->messenger()->acknowledged()->assertCount(1); |
603
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageD::class, 1); |
604
|
|
|
|
605
|
|
|
$this->messenger()->process(2); |
606
|
|
|
|
607
|
|
|
$this->messenger()->queue()->assertEmpty(); |
608
|
|
|
$this->messenger()->acknowledged()->assertCount(3); |
609
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageE::class, 1); |
610
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageF::class, 1); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* @test |
615
|
|
|
*/ |
616
|
|
|
public function fails_if_trying_to_process_more_messages_than_can_be_processed(): void |
617
|
|
|
{ |
618
|
|
|
self::bootKernel(); |
619
|
|
|
|
620
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
621
|
|
|
|
622
|
|
|
try { |
623
|
|
|
$this->messenger()->process(2); |
624
|
|
|
} catch (AssertionFailedError $e) { |
625
|
|
|
$this->assertStringContainsString('Expected to process 2 messages but only 1 was processed.', $e->getMessage()); |
626
|
|
|
$this->messenger()->queue()->assertEmpty(); |
627
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageA::class, 1); |
628
|
|
|
|
629
|
|
|
return; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
$this->fail('Did not fail.'); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* @test |
637
|
|
|
*/ |
638
|
|
|
public function process_or_fail_processes_messages(): void |
639
|
|
|
{ |
640
|
|
|
self::bootKernel(); |
641
|
|
|
|
642
|
|
|
self::$container->get(MessageBusInterface::class)->dispatch(new MessageA()); |
643
|
|
|
|
644
|
|
|
$this->messenger()->queue()->assertCount(1); |
645
|
|
|
$this->messenger()->queue()->assertContains(MessageA::class, 1); |
646
|
|
|
|
647
|
|
|
$this->messenger()->processOrFail(); |
648
|
|
|
|
649
|
|
|
$this->messenger()->queue()->assertEmpty(); |
650
|
|
|
$this->messenger()->acknowledged()->assertCount(1); |
651
|
|
|
$this->messenger()->acknowledged()->assertContains(MessageA::class, 1); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @test |
656
|
|
|
*/ |
657
|
|
|
public function process_or_fail_fails_if_no_messages_on_queue(): void |
658
|
|
|
{ |
659
|
|
|
self::bootKernel(); |
660
|
|
|
|
661
|
|
|
try { |
662
|
|
|
$this->messenger()->processOrFail(); |
663
|
|
|
} catch (AssertionFailedError $e) { |
664
|
|
|
$this->assertStringContainsString('No messages to process.', $e->getMessage()); |
665
|
|
|
|
666
|
|
|
return; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
$this->fail('Did not fail.'); |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
protected static function bootKernel(array $options = []): KernelInterface |
673
|
|
|
{ |
674
|
|
|
return parent::bootKernel(\array_merge(['environment' => 'single_transport'], $options)); |
675
|
|
|
} |
676
|
|
|
} |
677
|
|
|
|