Passed
Push — 1.x ( 13d125...ebb927 )
by Kevin
02:03
created

InteractsWithMessengerTest::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
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\DependencyInjection\ContainerInterface;
8
use Symfony\Component\HttpKernel\KernelInterface;
9
use Symfony\Component\Messenger\Envelope;
10
use Symfony\Component\Messenger\MessageBusInterface;
11
use Symfony\Component\Messenger\Stamp\DelayStamp;
12
use Zenstruck\Messenger\Test\InteractsWithMessenger;
13
use Zenstruck\Messenger\Test\TestEnvelope;
14
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageA;
15
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageAHandler;
16
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageB;
17
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageBHandler;
18
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageC;
19
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageD;
20
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageE;
21
use Zenstruck\Messenger\Test\Tests\Fixture\Messenger\MessageF;
22
use Zenstruck\Messenger\Test\Tests\Fixture\NoBundleKernel;
23
24
/**
25
 * @author Kevin Bond <[email protected]>
26
 */
27
final class InteractsWithMessengerTest extends WebTestCase
28
{
29
    use InteractsWithMessenger;
30
31
    /**
32
     * @test
33
     */
34
    public function can_interact_with_queue(): void
35
    {
36
        self::bootKernel();
37
38
        $this->messenger()->queue()->assertEmpty();
39
        $this->assertEmpty(self::getContainer()->get(MessageAHandler::class)->messages);
40
        $this->assertEmpty(self::getContainer()->get(MessageBHandler::class)->messages);
41
42
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
43
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
44
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
45
46
        $this->messenger()->queue()->assertCount(3);
47
        $this->messenger()->queue()->assertContains(MessageA::class);
48
        $this->messenger()->queue()->assertContains(MessageA::class, 2);
49
        $this->messenger()->queue()->assertContains(MessageB::class);
50
        $this->messenger()->queue()->assertContains(MessageB::class, 1);
51
        $this->messenger()->queue()->assertNotContains(MessageC::class);
52
        $this->assertEmpty(self::getContainer()->get(MessageAHandler::class)->messages);
53
        $this->assertEmpty(self::getContainer()->get(MessageBHandler::class)->messages);
54
55
        $this->messenger()->process(2);
56
57
        $this->messenger()->queue()->assertCount(1);
58
        $this->messenger()->queue()->assertContains(MessageA::class, 1);
59
        $this->messenger()->queue()->assertNotContains(MessageB::class);
60
        $this->assertCount(1, self::getContainer()->get(MessageAHandler::class)->messages);
61
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
62
63
        $this->messenger()->process();
64
65
        $this->messenger()->queue()->assertEmpty();
66
        $this->messenger()->queue()->assertNotContains(MessageA::class);
67
        $this->messenger()->queue()->assertNotContains(MessageB::class);
68
        $this->assertCount(2, self::getContainer()->get(MessageAHandler::class)->messages);
69
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function can_disable_intercept(): void
76
    {
77
        self::bootKernel();
78
79
        $this->messenger()->unblock();
80
81
        $this->messenger()->queue()->assertEmpty();
82
        $this->messenger()->acknowledged()->assertEmpty();
83
        $this->messenger()->dispatched()->assertEmpty();
84
85
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
86
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
87
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
88
89
        $this->messenger()->queue()->assertEmpty();
90
        $this->messenger()->queue()->assertNotContains(MessageA::class);
91
        $this->messenger()->dispatched()->assertCount(3);
92
        $this->messenger()->dispatched()->assertContains(MessageA::class, 2);
93
        $this->messenger()->dispatched()->assertContains(MessageB::class, 1);
94
        $this->messenger()->acknowledged()->assertCount(3);
95
        $this->messenger()->acknowledged()->assertContains(MessageA::class, 2);
96
        $this->messenger()->acknowledged()->assertContains(MessageB::class, 1);
97
        $this->assertCount(2, self::getContainer()->get(MessageAHandler::class)->messages);
98
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function disabling_intercept_with_items_on_queue_processes_all(): void
105
    {
106
        self::bootKernel();
107
108
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
109
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
110
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
111
112
        $this->messenger()->queue()->assertCount(3);
113
114
        $this->messenger()->process();
115
116
        $this->messenger()->queue()->assertEmpty();
117
        $this->assertCount(2, self::getContainer()->get(MessageAHandler::class)->messages);
118
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function unblocking_processes_existing_messages_on_queue(): void
125
    {
126
        self::bootKernel();
127
128
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
129
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
130
131
        $this->messenger()->queue()->assertCount(2);
132
        $this->messenger()->acknowledged()->assertEmpty();
133
134
        $this->messenger()->unblock();
135
136
        $this->messenger()->queue()->assertEmpty();
137
        $this->messenger()->acknowledged()->assertCount(2);
138
    }
139
140
    /**
141
     * @test
142
     */
143
    public function can_access_envelope_collection_items_via_first(): void
144
    {
145
        self::bootKernel();
146
147
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m1 = new MessageA());
148
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m2 = new MessageB());
149
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m3 = new MessageA(true));
150
151
        $this->messenger()->queue()->assertCount(3);
152
153
        $this->assertSame($m1, $this->messenger()->queue()->first()->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Zenstruck\Messenger\Test\TestEnvelope. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $this->assertSame($m1, $this->messenger()->queue()->first()->/** @scrutinizer ignore-call */ getMessage());
Loading history...
154
        $this->assertSame($m2, $this->messenger()->queue()->first(MessageB::class)->getMessage());
155
        $this->assertSame($m3, $this->messenger()->queue()->first(fn(Envelope $e) => $e->getMessage()->fail)->getMessage());
156
        $this->assertSame($m3, $this->messenger()->queue()->first(fn($e) => $e->getMessage()->fail)->getMessage());
157
        $this->assertSame($m3, $this->messenger()->queue()->first(fn(MessageA $m) => $m->fail)->getMessage());
158
    }
159
160
    /**
161
     * @test
162
     */
163
    public function envelope_collection_first_throws_exception_if_no_match(): void
164
    {
165
        self::bootKernel();
166
167
        $this->expectException(\RuntimeException::class);
168
169
        $this->messenger()->queue()->first();
170
    }
171
172
    /**
173
     * @test
174
     */
175
    public function can_make_stamp_assertions_on_test_envelope(): void
176
    {
177
        self::bootKernel();
178
179
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA(), [new DelayStamp(1000)]);
180
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
181
182
        $this->messenger()->queue()->first()->assertHasStamp(DelayStamp::class);
183
        $this->messenger()->queue()->first(MessageB::class)->assertNotHasStamp(DelayStamp::class);
184
    }
185
186
    /**
187
     * @test
188
     */
189
    public function cannot_access_queue_if_none_registered(): void
190
    {
191
        self::bootKernel(['environment' => 'test']);
192
193
        $this->expectException(\LogicException::class);
194
        $this->expectExceptionMessage('No transports registered');
195
196
        $this->messenger();
197
    }
198
199
    /**
200
     * @test
201
     */
202
    public function accessing_transport_boots_kernel_if_not_yet_booted(): void
203
    {
204
        $this->messenger()->queue()->assertEmpty();
205
    }
206
207
    /**
208
     * @test
209
     */
210
    public function can_interact_with_multiple_queues(): void
211
    {
212
        self::bootKernel(['environment' => 'multi_transport']);
213
214
        $this->messenger('async1')->queue()->assertEmpty();
215
        $this->messenger('async2')->queue()->assertEmpty();
216
        $this->assertEmpty(self::getContainer()->get(MessageAHandler::class)->messages);
217
        $this->assertEmpty(self::getContainer()->get(MessageBHandler::class)->messages);
218
219
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
220
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
221
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
222
223
        $this->messenger('async1')->queue()->assertCount(2);
224
        $this->messenger('async2')->queue()->assertEmpty();
225
        $this->messenger('async1')->queue()->assertContains(MessageA::class);
226
        $this->messenger('async1')->queue()->assertContains(MessageA::class, 2);
227
        $this->messenger('async2')->queue()->assertNotContains(MessageB::class);
228
        $this->assertEmpty(self::getContainer()->get(MessageAHandler::class)->messages);
229
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
230
231
        $this->messenger('async1')->process(1);
232
233
        $this->messenger('async1')->queue()->assertCount(1);
234
        $this->messenger('async1')->queue()->assertContains(MessageA::class, 1);
235
        $this->messenger('async2')->queue()->assertNotContains(MessageB::class);
236
        $this->assertCount(1, self::getContainer()->get(MessageAHandler::class)->messages);
237
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
238
239
        $this->messenger('async1')->process();
240
241
        $this->messenger('async1')->queue()->assertEmpty();
242
        $this->messenger('async2')->queue()->assertEmpty();
243
        $this->messenger('async2')->acknowledged()->assertCount(1);
244
        $this->messenger('async2')->acknowledged()->assertContains(MessageB::class, 1);
245
        $this->assertCount(2, self::getContainer()->get(MessageAHandler::class)->messages);
246
        $this->assertCount(1, self::getContainer()->get(MessageBHandler::class)->messages);
247
    }
248
249
    /**
250
     * @test
251
     */
252
    public function can_enable_intercept(): void
253
    {
254
        self::bootKernel(['environment' => 'multi_transport']);
255
256
        $this->messenger('async2')->intercept();
257
258
        $this->messenger('async2')->queue()->assertEmpty();
259
        $this->assertEmpty(self::getContainer()->get(MessageBHandler::class)->messages);
260
261
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
262
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB());
263
264
        $this->messenger('async2')->queue()->assertCount(2);
265
    }
266
267
    /**
268
     * @test
269
     */
270
    public function cannot_access_queue_that_does_not_exist(): void
271
    {
272
        self::bootKernel(['environment' => 'multi_transport']);
273
274
        $this->expectException(\InvalidArgumentException::class);
275
        $this->expectExceptionMessage('"invalid" not registered');
276
277
        $this->messenger('invalid');
278
    }
279
280
    /**
281
     * @test
282
     */
283
    public function cannot_access_queue_that_is_not_test_transport(): void
284
    {
285
        self::bootKernel(['environment' => 'multi_transport']);
286
287
        $this->expectException(\LogicException::class);
288
        $this->expectExceptionMessage('Transport "async3" needs to be set to "test://" in your test config to use this feature.');
289
290
        $this->messenger('async3');
291
    }
292
293
    /**
294
     * @test
295
     */
296
    public function queue_name_is_required_if_using_multiple_transports(): void
297
    {
298
        self::bootKernel(['environment' => 'multi_transport']);
299
300
        $this->expectException(\LogicException::class);
301
        $this->expectExceptionMessage('Multiple transports are registered (async1, async2, async3), you must specify a name.');
302
303
        $this->messenger();
304
    }
305
306
    /**
307
     * @test
308
     */
309
    public function can_access_message_objects_on_queue(): void
310
    {
311
        self::bootKernel();
312
313
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m1 = new MessageA());
314
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m2 = new MessageB());
315
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m3 = new MessageA());
316
317
        $this->assertSame([$m1, $m2, $m3], $this->messenger()->queue()->messages());
318
        $this->assertSame([$m1, $m3], $this->messenger()->queue()->messages(MessageA::class));
319
        $this->assertSame([$m2], $this->messenger()->queue()->messages(MessageB::class));
320
    }
321
322
    /**
323
     * @test
324
     */
325
    public function can_access_envelopes_on_envelope_collection(): void
326
    {
327
        self::bootKernel();
328
329
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m1 = new MessageA());
330
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m2 = new MessageB());
331
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m3 = new MessageA());
332
333
        $messages = \array_map(fn(TestEnvelope $envelope) => $envelope->getMessage(), $this->messenger()->queue()->all());
334
        $messagesFromIterator = \array_map(fn(TestEnvelope $envelope) => $envelope->getMessage(), \iterator_to_array($this->messenger()->queue()));
335
336
        $this->assertSame([$m1, $m2, $m3], $messages);
337
        $this->assertSame([$m1, $m2, $m3], $messagesFromIterator);
338
    }
339
340
    /**
341
     * @test
342
     */
343
    public function can_access_sent_acknowledged_and_rejected(): void
344
    {
345
        self::bootKernel();
346
347
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m1 = new MessageA(true));
348
        self::getContainer()->get(MessageBusInterface::class)->dispatch($m2 = new MessageB());
349
350
        $this->assertCount(2, $this->messenger()->queue());
351
        $this->assertCount(2, $this->messenger()->dispatched());
352
        $this->assertCount(0, $this->messenger()->acknowledged());
353
        $this->assertCount(0, $this->messenger()->rejected());
354
355
        $this->messenger()->process();
356
357
        $this->assertCount(0, $this->messenger()->queue());
358
        $this->assertCount(2, $this->messenger()->dispatched());
359
        $this->assertCount(1, $this->messenger()->acknowledged());
360
        $this->assertCount(1, $this->messenger()->rejected());
361
    }
362
363
    /**
364
     * @test
365
     */
366
    public function cannot_access_queue_if_bundle_not_enabled(): void
367
    {
368
        self::$class = NoBundleKernel::class;
369
        self::bootKernel(['environment' => 'no_bundle']);
370
        self::$class = null;
371
372
        $this->expectException(\LogicException::class);
373
        $this->expectExceptionMessage('Cannot access transport - is ZenstruckMessengerTestBundle enabled in your test environment?');
374
375
        $this->messenger();
376
    }
377
378
    /**
379
     * @test
380
     */
381
    public function can_configure_throwing_exceptions(): void
382
    {
383
        self::bootKernel();
384
385
        $this->messenger()->throwExceptions();
386
387
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA(true));
388
389
        $this->expectException(\RuntimeException::class);
390
        $this->expectExceptionMessage('handling failed...');
391
392
        $this->messenger()->process();
393
    }
394
395
    /**
396
     * @test
397
     */
398
    public function can_configure_throwing_exceptions_with_intercept_disabled(): void
399
    {
400
        self::bootKernel();
401
402
        $this->messenger()->throwExceptions()->unblock();
403
404
        $this->expectException(\RuntimeException::class);
405
        $this->expectExceptionMessage('handling failed...');
406
407
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA(true));
408
    }
409
410
    /**
411
     * @test
412
     */
413
    public function can_disable_exception_catching_in_transport_config(): void
414
    {
415
        self::bootKernel(['environment' => 'multi_transport']);
416
417
        $this->expectException(\RuntimeException::class);
418
        $this->expectExceptionMessage('handling failed...');
419
420
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB(true));
421
    }
422
423
    /**
424
     * @test
425
     */
426
    public function can_re_enable_exception_catching_if_disabled_in_transport_config(): void
427
    {
428
        self::bootKernel(['environment' => 'multi_transport']);
429
430
        $this->messenger('async2')->catchExceptions();
431
432
        $this->messenger('async2')->rejected()->assertEmpty();
433
434
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageB(true));
435
436
        $this->messenger('async2')->rejected()->assertCount(1);
437
    }
438
439
    /**
440
     * @test
441
     */
442
    public function transport_data_is_persisted_between_requests_and_kernel_shutdown(): void
443
    {
444
        self::bootKernel();
445
446
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
447
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA(true));
448
449
        $this->messenger()->queue()->assertCount(2);
450
451
        self::ensureKernelShutdown();
452
        self::bootKernel();
453
454
        $this->messenger()->queue()->assertCount(2);
455
456
        $this->messenger()->process();
457
458
        self::ensureKernelShutdown();
459
        self::bootKernel();
460
461
        $this->messenger()->queue()->assertEmpty();
462
        $this->messenger()->dispatched()->assertCount(2);
463
        $this->messenger()->acknowledged()->assertCount(1);
464
        $this->messenger()->rejected()->assertCount(1);
465
466
        self::ensureKernelShutdown();
467
468
        $client = self::createClient();
469
470
        $client->request('GET', '/dispatch');
471
472
        $this->messenger()->queue()->assertCount(1);
473
        $this->messenger()->dispatched()->assertCount(3);
474
        $this->messenger()->acknowledged()->assertCount(1);
475
        $this->messenger()->rejected()->assertCount(1);
476
477
        $client->request('GET', '/dispatch');
478
479
        $this->messenger()->queue()->assertCount(2);
480
        $this->messenger()->dispatched()->assertCount(4);
481
        $this->messenger()->acknowledged()->assertCount(1);
482
        $this->messenger()->rejected()->assertCount(1);
483
    }
484
485
    /**
486
     * @test
487
     */
488
    public function can_reset_transport_data(): void
489
    {
490
        self::bootKernel();
491
492
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
493
494
        $this->messenger()->queue()->assertNotEmpty();
495
496
        $this->messenger()->reset();
497
498
        $this->messenger()->queue()->assertEmpty();
499
    }
500
501
    /**
502
     * @test
503
     */
504
    public function disabling_intercept_is_remembered_between_kernel_reboots(): void
505
    {
506
        self::bootKernel();
507
508
        $this->messenger()->unblock();
509
510
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
511
512
        $this->messenger()->queue()->assertEmpty();
513
        $this->messenger()->dispatched()->assertCount(1);
514
515
        self::ensureKernelShutdown();
516
        self::bootKernel();
517
518
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
519
520
        $this->messenger()->queue()->assertEmpty();
521
        $this->messenger()->dispatched()->assertCount(2);
522
    }
523
524
    /**
525
     * @test
526
     */
527
    public function throwing_exceptions_is_remembered_between_kernel_reboots(): void
528
    {
529
        self::bootKernel();
530
531
        $this->messenger()->throwExceptions();
532
533
        self::ensureKernelShutdown();
534
        self::bootKernel();
535
536
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA(true));
537
538
        $this->expectException(\RuntimeException::class);
539
        $this->expectErrorMessage('handling failed...');
540
541
        $this->messenger()->process();
542
    }
543
544
    /**
545
     * @test
546
     */
547
    public function can_manually_send_message_to_transport_and_process(): void
548
    {
549
        self::bootKernel();
550
551
        $this->messenger()->queue()->assertEmpty();
552
        $this->assertEmpty(self::getContainer()->get(MessageAHandler::class)->messages);
553
554
        $this->messenger()->send(Envelope::wrap(new MessageA()));
555
556
        $this->messenger()->queue()->assertCount(1);
557
        $this->assertEmpty(self::getContainer()->get(MessageAHandler::class)->messages);
558
559
        $this->messenger()->process();
560
561
        $this->messenger()->queue()->assertEmpty();
562
        $this->assertCount(1, self::getContainer()->get(MessageAHandler::class)->messages);
563
    }
564
565
    /**
566
     * @test
567
     */
568
    public function process_all_is_recursive(): void
569
    {
570
        self::bootKernel();
571
572
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageD());
573
574
        $this->messenger()->queue()->assertCount(1);
575
        $this->messenger()->queue()->assertContains(MessageD::class, 1);
576
577
        $this->messenger()->process();
578
579
        $this->messenger()->queue()->assertEmpty();
580
        $this->messenger()->dispatched()->assertCount(3);
581
        $this->messenger()->acknowledged()->assertCount(3);
582
        $this->messenger()->acknowledged()->assertContains(MessageD::class, 1);
583
        $this->messenger()->acknowledged()->assertContains(MessageE::class, 1);
584
        $this->messenger()->acknowledged()->assertContains(MessageF::class, 1);
585
    }
586
587
    /**
588
     * @test
589
     */
590
    public function process_x_messages_is_recursive(): void
591
    {
592
        self::bootKernel();
593
594
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageD());
595
596
        $this->messenger()->queue()->assertCount(1);
597
        $this->messenger()->queue()->assertContains(MessageD::class, 1);
598
599
        $this->messenger()->process(1);
600
601
        $this->messenger()->queue()->assertCount(1);
602
        $this->messenger()->queue()->assertContains(MessageE::class, 1);
603
        $this->messenger()->acknowledged()->assertCount(1);
604
        $this->messenger()->acknowledged()->assertContains(MessageD::class, 1);
605
606
        $this->messenger()->process(2);
607
608
        $this->messenger()->queue()->assertEmpty();
609
        $this->messenger()->acknowledged()->assertCount(3);
610
        $this->messenger()->acknowledged()->assertContains(MessageE::class, 1);
611
        $this->messenger()->acknowledged()->assertContains(MessageF::class, 1);
612
    }
613
614
    /**
615
     * @test
616
     */
617
    public function fails_if_trying_to_process_more_messages_than_can_be_processed(): void
618
    {
619
        self::bootKernel();
620
621
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
622
623
        try {
624
            $this->messenger()->process(2);
625
        } catch (AssertionFailedError $e) {
626
            $this->assertStringContainsString('Expected to process 2 messages but only 1 was processed.', $e->getMessage());
627
            $this->messenger()->queue()->assertEmpty();
628
            $this->messenger()->acknowledged()->assertContains(MessageA::class, 1);
629
630
            return;
631
        }
632
633
        $this->fail('Did not fail.');
634
    }
635
636
    /**
637
     * @test
638
     */
639
    public function process_or_fail_processes_messages(): void
640
    {
641
        self::bootKernel();
642
643
        self::getContainer()->get(MessageBusInterface::class)->dispatch(new MessageA());
644
645
        $this->messenger()->queue()->assertCount(1);
646
        $this->messenger()->queue()->assertContains(MessageA::class, 1);
647
648
        $this->messenger()->processOrFail();
649
650
        $this->messenger()->queue()->assertEmpty();
651
        $this->messenger()->acknowledged()->assertCount(1);
652
        $this->messenger()->acknowledged()->assertContains(MessageA::class, 1);
653
    }
654
655
    /**
656
     * @test
657
     */
658
    public function process_or_fail_fails_if_no_messages_on_queue(): void
659
    {
660
        self::bootKernel();
661
662
        try {
663
            $this->messenger()->processOrFail();
664
        } catch (AssertionFailedError $e) {
665
            $this->assertStringContainsString('No messages to process.', $e->getMessage());
666
667
            return;
668
        }
669
670
        $this->fail('Did not fail.');
671
    }
672
673
    protected static function bootKernel(array $options = []): KernelInterface
674
    {
675
        return parent::bootKernel(\array_merge(['environment' => 'single_transport'], $options));
676
    }
677
678
    protected static function getContainer(): ContainerInterface
679
    {
680
        if (\method_exists(parent::class, 'getContainer')) {
681
            return parent::getContainer();
682
        }
683
684
        return self::$container;
685
    }
686
}
687