3
3
namespace Illuminate \Tests \Queue ;
4
4
5
5
use Illuminate \Container \Container ;
6
+ use Illuminate \Contracts \Events \Dispatcher ;
6
7
use Illuminate \Foundation \Testing \Concerns \InteractsWithRedis ;
8
+ use Illuminate \Queue \Events \JobQueued ;
7
9
use Illuminate \Queue \Jobs \RedisJob ;
8
10
use Illuminate \Queue \RedisQueue ;
9
11
use Illuminate \Support \Carbon ;
@@ -21,6 +23,11 @@ class RedisQueueIntegrationTest extends TestCase
21
23
*/
22
24
private $ queue ;
23
25
26
+ /**
27
+ * @var \Mockery\MockInterface|\Mockery\LegacyMockInterface
28
+ */
29
+ private $ container ;
30
+
24
31
protected function setUp (): void
25
32
{
26
33
Carbon::setTestNow (Carbon::now ());
@@ -57,6 +64,8 @@ public function testExpiredJobsArePopped($driver)
57
64
$ this ->queue ->later (-300 , $ jobs [2 ]);
58
65
$ this ->queue ->later (-100 , $ jobs [3 ]);
59
66
67
+ $ this ->container ->shouldHaveReceived ('bound ' )->with ('events ' )->times (4 );
68
+
60
69
$ this ->assertEquals ($ jobs [2 ], unserialize (json_decode ($ this ->queue ->pop ()->getRawBody ())->data ->command ));
61
70
$ this ->assertEquals ($ jobs [1 ], unserialize (json_decode ($ this ->queue ->pop ()->getRawBody ())->data ->command ));
62
71
$ this ->assertEquals ($ jobs [3 ], unserialize (json_decode ($ this ->queue ->pop ()->getRawBody ())->data ->command ));
@@ -183,13 +192,14 @@ public function testPopProperlyPopsDelayedJobOffOfRedis($driver)
183
192
*/
184
193
public function testPopPopsDelayedJobOffOfRedisWhenExpireNull ($ driver )
185
194
{
186
- $ this ->queue = new RedisQueue ($ this ->redis [$ driver ], 'default ' , null , null );
187
- $ this ->queue ->setContainer (m::mock (Container::class));
195
+ $ this ->setQueue ($ driver , 'default ' , null , null );
188
196
189
197
// Push an item into queue
190
198
$ job = new RedisQueueIntegrationTestJob (10 );
191
199
$ this ->queue ->later (-10 , $ job );
192
200
201
+ $ this ->container ->shouldHaveReceived ('bound ' )->with ('events ' )->once ();
202
+
193
203
// Pop and check it is popped correctly
194
204
$ before = $ this ->currentTime ();
195
205
$ this ->assertEquals ($ job , unserialize (json_decode ($ this ->queue ->pop ()->getRawBody ())->data ->command ));
@@ -264,19 +274,21 @@ public function testBlockingPopProperlyPopsExpiredJobs($driver)
264
274
*/
265
275
public function testNotExpireJobsWhenExpireNull ($ driver )
266
276
{
267
- $ this ->queue = new RedisQueue ($ this ->redis [$ driver ], 'default ' , null , null );
268
- $ this ->queue ->setContainer (m::mock (Container::class));
277
+ $ this ->setQueue ($ driver , 'default ' , null , null );
269
278
270
279
// Make an expired reserved job
271
280
$ failed = new RedisQueueIntegrationTestJob (-20 );
272
281
$ this ->queue ->push ($ failed );
282
+ $ this ->container ->shouldHaveReceived ('bound ' )->with ('events ' )->once ();
283
+
273
284
$ beforeFailPop = $ this ->currentTime ();
274
285
$ this ->queue ->pop ();
275
286
$ afterFailPop = $ this ->currentTime ();
276
287
277
288
// Push an item into queue
278
289
$ job = new RedisQueueIntegrationTestJob (10 );
279
290
$ this ->queue ->push ($ job );
291
+ $ this ->container ->shouldHaveReceived ('bound ' )->with ('events ' )->times (2 );
280
292
281
293
// Pop and check it is popped correctly
282
294
$ before = $ this ->currentTime ();
@@ -309,12 +321,12 @@ public function testNotExpireJobsWhenExpireNull($driver)
309
321
*/
310
322
public function testExpireJobsWhenExpireSet ($ driver )
311
323
{
312
- $ this ->queue = new RedisQueue ($ this ->redis [$ driver ], 'default ' , null , 30 );
313
- $ this ->queue ->setContainer (m::mock (Container::class));
324
+ $ this ->setQueue ($ driver , 'default ' , null , 30 );
314
325
315
326
// Push an item into queue
316
327
$ job = new RedisQueueIntegrationTestJob (10 );
317
328
$ this ->queue ->push ($ job );
329
+ $ this ->container ->shouldHaveReceived ('bound ' )->with ('events ' )->once ();
318
330
319
331
// Pop and check it is popped correctly
320
332
$ before = $ this ->currentTime ();
@@ -455,17 +467,67 @@ public function testSize($driver)
455
467
$ this ->assertEquals (2 , $ this ->queue ->size ());
456
468
}
457
469
470
+ /**
471
+ * @dataProvider redisDriverProvider
472
+ *
473
+ * @param string $driver
474
+ */
475
+ public function testPushJobQueuedEvent ($ driver )
476
+ {
477
+ $ events = m::mock (Dispatcher::class);
478
+ $ events ->shouldReceive ('dispatch ' )->withArgs (function (JobQueued $ jobQueued ) {
479
+ $ this ->assertInstanceOf (RedisQueueIntegrationTestJob::class, $ jobQueued ->job );
480
+ $ this ->assertIsString (RedisQueueIntegrationTestJob::class, $ jobQueued ->jobId );
481
+
482
+ return true ;
483
+ })->andReturnNull ()->once ();
484
+
485
+ $ container = m::mock (Container::class);
486
+ $ container ->shouldReceive ('bound ' )->with ('events ' )->andReturn (true )->once ();
487
+ $ container ->shouldReceive ('offsetGet ' )->with ('events ' )->andReturn ($ events )->once ();
488
+
489
+ $ queue = new RedisQueue ($ this ->redis [$ driver ]);
490
+ $ queue ->setContainer ($ container );
491
+
492
+ $ queue ->push (new RedisQueueIntegrationTestJob (5 ));
493
+ }
494
+
495
+ /**
496
+ * @dataProvider redisDriverProvider
497
+ *
498
+ * @param string $driver
499
+ */
500
+ public function testBulkJobQueuedEvent ($ driver )
501
+ {
502
+ $ events = m::mock (Dispatcher::class);
503
+ $ events ->shouldReceive ('dispatch ' )->with (m::type (JobQueued::class))->andReturnNull ()->times (3 );
504
+
505
+ $ container = m::mock (Container::class);
506
+ $ container ->shouldReceive ('bound ' )->with ('events ' )->andReturn (true )->times (3 );
507
+ $ container ->shouldReceive ('offsetGet ' )->with ('events ' )->andReturn ($ events )->times (3 );
508
+
509
+ $ queue = new RedisQueue ($ this ->redis [$ driver ]);
510
+ $ queue ->setContainer ($ container );
511
+
512
+ $ queue ->bulk ([
513
+ new RedisQueueIntegrationTestJob (5 ),
514
+ new RedisQueueIntegrationTestJob (10 ),
515
+ new RedisQueueIntegrationTestJob (15 ),
516
+ ]);
517
+ }
518
+
458
519
/**
459
520
* @param string $driver
460
521
* @param string $default
461
- * @param string $connection
522
+ * @param string|null $connection
462
523
* @param int $retryAfter
463
524
* @param int|null $blockFor
464
525
*/
465
526
private function setQueue ($ driver , $ default = 'default ' , $ connection = null , $ retryAfter = 60 , $ blockFor = null )
466
527
{
467
528
$ this ->queue = new RedisQueue ($ this ->redis [$ driver ], $ default , $ connection , $ retryAfter , $ blockFor );
468
- $ this ->queue ->setContainer (m::mock (Container::class));
529
+ $ this ->container = m::spy (Container::class);
530
+ $ this ->queue ->setContainer ($ this ->container );
469
531
}
470
532
}
471
533
0 commit comments