4
4
5
5
use Aws \Result ;
6
6
use Aws \Sqs \SqsClient ;
7
+ use Illuminate \Bus \Dispatcher ;
7
8
use Illuminate \Container \Container ;
9
+ use Illuminate \Contracts \Bus \Dispatcher as DispatcherContract ;
8
10
use Illuminate \Queue \Jobs \SqsJob ;
9
11
use Illuminate \Queue \SqsQueue ;
10
12
use Illuminate \Support \Carbon ;
13
+ use Illuminate \Support \Str ;
14
+ use Illuminate \Tests \Queue \Fixtures \FakeSqsJob ;
11
15
use Mockery as m ;
12
16
use PHPUnit \Framework \TestCase ;
13
17
@@ -16,13 +20,17 @@ class QueueSqsQueueTest extends TestCase
16
20
protected $ sqs ;
17
21
protected $ account ;
18
22
protected $ queueName ;
23
+ protected $ fifoQueueName ;
19
24
protected $ baseUrl ;
20
25
protected $ prefix ;
21
26
protected $ queueUrl ;
27
+ protected $ fifoQueueUrl ;
22
28
protected $ mockedJob ;
23
29
protected $ mockedData ;
24
30
protected $ mockedPayload ;
25
31
protected $ mockedDelay ;
32
+ protected $ mockedMessageGroupId ;
33
+ protected $ mockedDeduplicationId ;
26
34
protected $ mockedMessageId ;
27
35
protected $ mockedReceiptHandle ;
28
36
protected $ mockedSendMessageResponseModel ;
@@ -42,16 +50,20 @@ protected function setUp(): void
42
50
43
51
$ this ->account = '1234567891011 ' ;
44
52
$ this ->queueName = 'emails ' ;
53
+ $ this ->fifoQueueName = 'emails.fifo ' ;
45
54
$ this ->baseUrl = 'https://sqs.someregion.amazonaws.com ' ;
46
55
47
56
// This is how the modified getQueue builds the queueUrl
48
57
$ this ->prefix = $ this ->baseUrl .'/ ' .$ this ->account .'/ ' ;
49
58
$ this ->queueUrl = $ this ->prefix .$ this ->queueName ;
59
+ $ this ->fifoQueueUrl = $ this ->prefix .$ this ->fifoQueueName ;
50
60
51
61
$ this ->mockedJob = 'foo ' ;
52
62
$ this ->mockedData = ['data ' ];
53
63
$ this ->mockedPayload = json_encode (['job ' => $ this ->mockedJob , 'data ' => $ this ->mockedData ]);
54
64
$ this ->mockedDelay = 10 ;
65
+ $ this ->mockedMessageGroupId = 'group-1 ' ;
66
+ $ this ->mockedDeduplicationId = 'deduplication-id-1 ' ;
55
67
$ this ->mockedMessageId = 'e3cd03ee-59a3-4ad8-b0aa-ee2e3808ac81 ' ;
56
68
$ this ->mockedReceiptHandle = '0NNAq8PwvXuWv5gMtS9DJ8qEdyiUwbAjpp45w2m6M4SJ1Y+PxCh7R930NRB8ylSacEmoSnW18bgd4nK\/O6ctE+VFVul4eD23mA07vVoSnPI4F\/voI1eNCp6Iax0ktGmhlNVzBwaZHEr91BRtqTRM3QKd2ASF8u+IQaSwyl\/DGK+P1+dqUOodvOVtExJwdyDLy1glZVgm85Yw9Jf5yZEEErqRwzYz\/qSigdvW4sm2l7e4phRol\/+IjMtovOyH\/ukueYdlVbQ4OshQLENhUKe7RNN5i6bE\/e5x9bnPhfj2gbM ' ;
57
69
@@ -237,4 +249,182 @@ public function testGetFifoQueueEnsuresTheQueueIsOnlySuffixedOnce()
237
249
$ queueUrl = $ this ->baseUrl .'/ ' .$ this ->account .'/test ' .$ suffix .'.fifo ' ;
238
250
$ this ->assertEquals ($ queueUrl , $ queue ->getQueue ('test-staging.fifo ' ));
239
251
}
252
+
253
+ public function testPushProperlyPushesJobObjectOntoSqs ()
254
+ {
255
+ $ job = new FakeSqsJob ();
256
+
257
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->queueName , $ this ->account ])->getMock ();
258
+ $ queue ->setContainer ($ container = m::spy (Container::class));
259
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->queueName , $ this ->mockedData )->willReturn ($ this ->mockedPayload );
260
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with ($ this ->queueName )->willReturn ($ this ->queueUrl );
261
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->queueUrl , 'MessageBody ' => $ this ->mockedPayload ])->andReturn ($ this ->mockedSendMessageResponseModel );
262
+ $ id = $ queue ->push ($ job , $ this ->mockedData , $ this ->queueName );
263
+ $ this ->assertEquals ($ this ->mockedMessageId , $ id );
264
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
265
+ }
266
+
267
+ public function testPendingDispatchProperlyPushesJobObjectOntoSqs ()
268
+ {
269
+ $ job = new FakeSqsJob ();
270
+
271
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->queueName , $ this ->account ])->getMock ();
272
+ $ queue ->setContainer ($ container = m::spy (Container::class));
273
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->queueName , '' )->willReturn ($ this ->mockedPayload );
274
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with (null )->willReturn ($ this ->queueUrl );
275
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->queueUrl , 'MessageBody ' => $ this ->mockedPayload ])->andReturn ($ this ->mockedSendMessageResponseModel );
276
+
277
+ $ dispatcher = new Dispatcher ($ container , fn () => $ queue );
278
+ app ()->instance (DispatcherContract::class, $ dispatcher );
279
+
280
+ FakeSqsJob::dispatch ();
281
+
282
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
283
+ }
284
+
285
+ public function testPushProperlyPushesJobObjectOntoSqsFairQueue ()
286
+ {
287
+ $ job = (new FakeSqsJob ())->onGroup ($ this ->mockedMessageGroupId );
288
+
289
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->queueName , $ this ->account ])->getMock ();
290
+ $ queue ->setContainer ($ container = m::spy (Container::class));
291
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->queueName , $ this ->mockedData )->willReturn ($ this ->mockedPayload );
292
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with ($ this ->queueName )->willReturn ($ this ->queueUrl );
293
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->queueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->mockedMessageGroupId ])->andReturn ($ this ->mockedSendMessageResponseModel );
294
+ $ id = $ queue ->push ($ job , $ this ->mockedData , $ this ->queueName );
295
+ $ this ->assertEquals ($ this ->mockedMessageId , $ id );
296
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
297
+ }
298
+
299
+ public function testPendingDispatchProperlyPushesJobObjectOntoSqsFairQueue ()
300
+ {
301
+ $ job = (new FakeSqsJob ())->onGroup ($ this ->mockedMessageGroupId );
302
+
303
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->queueName , $ this ->account ])->getMock ();
304
+ $ queue ->setContainer ($ container = m::spy (Container::class));
305
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->queueName , '' )->willReturn ($ this ->mockedPayload );
306
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with (null )->willReturn ($ this ->queueUrl );
307
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->queueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->mockedMessageGroupId ])->andReturn ($ this ->mockedSendMessageResponseModel );
308
+
309
+ $ dispatcher = new Dispatcher ($ container , fn () => $ queue );
310
+ app ()->instance (DispatcherContract::class, $ dispatcher );
311
+
312
+ FakeSqsJob::dispatch ()->onGroup ($ this ->mockedMessageGroupId );
313
+
314
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
315
+ }
316
+
317
+ public function testPushProperlyPushesJobStringOntoSqsFifoQueue ()
318
+ {
319
+ Str::createUuidsUsing (fn () => $ this ->mockedDeduplicationId );
320
+
321
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->fifoQueueName , $ this ->account ])->getMock ();
322
+ $ queue ->setContainer ($ container = m::spy (Container::class));
323
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ this ->mockedJob , $ this ->fifoQueueName , $ this ->mockedData )->willReturn ($ this ->mockedPayload );
324
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with ($ this ->fifoQueueName )->willReturn ($ this ->fifoQueueUrl );
325
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->fifoQueueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->fifoQueueName , 'MessageDeduplicationId ' => $ this ->mockedDeduplicationId ])->andReturn ($ this ->mockedSendMessageResponseModel );
326
+ $ id = $ queue ->push ($ this ->mockedJob , $ this ->mockedData , $ this ->fifoQueueName );
327
+ $ this ->assertEquals ($ this ->mockedMessageId , $ id );
328
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
329
+
330
+ Str::createUuidsNormally ();
331
+ }
332
+
333
+ public function testPushProperlyPushesJobObjectOntoSqsFifoQueue ()
334
+ {
335
+ Str::createUuidsUsing (fn () => $ this ->mockedDeduplicationId );
336
+
337
+ $ job = (new FakeSqsJob ())->onGroup ($ this ->mockedMessageGroupId );
338
+
339
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->fifoQueueName , $ this ->account ])->getMock ();
340
+ $ queue ->setContainer ($ container = m::spy (Container::class));
341
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->fifoQueueName , $ this ->mockedData )->willReturn ($ this ->mockedPayload );
342
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with ($ this ->fifoQueueName )->willReturn ($ this ->fifoQueueUrl );
343
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->fifoQueueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->mockedMessageGroupId , 'MessageDeduplicationId ' => $ this ->mockedDeduplicationId ])->andReturn ($ this ->mockedSendMessageResponseModel );
344
+ $ id = $ queue ->push ($ job , $ this ->mockedData , $ this ->fifoQueueName );
345
+ $ this ->assertEquals ($ this ->mockedMessageId , $ id );
346
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
347
+
348
+ Str::createUuidsNormally ();
349
+ }
350
+
351
+ public function testPendingDispatchProperlyPushesJobObjectOntoSqsFifoQueue ()
352
+ {
353
+ Str::createUuidsUsing (fn () => $ this ->mockedDeduplicationId );
354
+
355
+ $ job = (new FakeSqsJob ())->onGroup ($ this ->mockedMessageGroupId );
356
+
357
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->fifoQueueName , $ this ->account ])->getMock ();
358
+ $ queue ->setContainer ($ container = m::spy (Container::class));
359
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->fifoQueueName , '' )->willReturn ($ this ->mockedPayload );
360
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with (null )->willReturn ($ this ->fifoQueueUrl );
361
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->fifoQueueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->mockedMessageGroupId , 'MessageDeduplicationId ' => $ this ->mockedDeduplicationId ])->andReturn ($ this ->mockedSendMessageResponseModel );
362
+
363
+ $ dispatcher = new Dispatcher ($ container , fn () => $ queue );
364
+ app ()->instance (DispatcherContract::class, $ dispatcher );
365
+
366
+ FakeSqsJob::dispatch ()->onGroup ($ this ->mockedMessageGroupId );
367
+
368
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
369
+
370
+ Str::createUuidsNormally ();
371
+ }
372
+
373
+ public function testDelayedPushProperlyPushesJobStringOntoSqsFifoQueueWithoutDelay ()
374
+ {
375
+ Str::createUuidsUsing (fn () => $ this ->mockedDeduplicationId );
376
+
377
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'secondsUntil ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->fifoQueueName , $ this ->account ])->getMock ();
378
+ $ queue ->setContainer ($ container = m::spy (Container::class));
379
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ this ->mockedJob , $ this ->fifoQueueName , $ this ->mockedData )->willReturn ($ this ->mockedPayload );
380
+ $ queue ->expects ($ this ->never ())->method ('secondsUntil ' )->with ($ this ->mockedDelay )->willReturn ($ this ->mockedDelay );
381
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with ($ this ->fifoQueueName )->willReturn ($ this ->fifoQueueUrl );
382
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->fifoQueueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->fifoQueueName , 'MessageDeduplicationId ' => $ this ->mockedDeduplicationId ])->andReturn ($ this ->mockedSendMessageResponseModel );
383
+ $ id = $ queue ->later ($ this ->mockedDelay , $ this ->mockedJob , $ this ->mockedData , $ this ->fifoQueueName );
384
+ $ this ->assertEquals ($ this ->mockedMessageId , $ id );
385
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
386
+
387
+ Str::createUuidsNormally ();
388
+ }
389
+
390
+ public function testDelayedPushProperlyPushesJobObjectOntoSqsFifoQueueWithoutDelay ()
391
+ {
392
+ Str::createUuidsUsing (fn () => $ this ->mockedDeduplicationId );
393
+
394
+ $ job = (new FakeSqsJob ())->onGroup ($ this ->mockedMessageGroupId );
395
+
396
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'secondsUntil ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->fifoQueueName , $ this ->account ])->getMock ();
397
+ $ queue ->setContainer ($ container = m::spy (Container::class));
398
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->fifoQueueName , $ this ->mockedData )->willReturn ($ this ->mockedPayload );
399
+ $ queue ->expects ($ this ->never ())->method ('secondsUntil ' )->with ($ this ->mockedDelay )->willReturn ($ this ->mockedDelay );
400
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with ($ this ->fifoQueueName )->willReturn ($ this ->fifoQueueUrl );
401
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->fifoQueueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->mockedMessageGroupId , 'MessageDeduplicationId ' => $ this ->mockedDeduplicationId ])->andReturn ($ this ->mockedSendMessageResponseModel );
402
+ $ id = $ queue ->later ($ this ->mockedDelay , $ job , $ this ->mockedData , $ this ->fifoQueueName );
403
+ $ this ->assertEquals ($ this ->mockedMessageId , $ id );
404
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
405
+
406
+ Str::createUuidsNormally ();
407
+ }
408
+
409
+ public function testDelayedPendingDispatchProperlyPushesJobObjectOntoSqsFifoQueueWithoutDelay ()
410
+ {
411
+ Str::createUuidsUsing (fn () => $ this ->mockedDeduplicationId );
412
+
413
+ $ job = (new FakeSqsJob ())->onGroup ($ this ->mockedMessageGroupId )->delay ($ this ->mockedDelay );
414
+
415
+ $ queue = $ this ->getMockBuilder (SqsQueue::class)->onlyMethods (['createPayload ' , 'getQueue ' ])->setConstructorArgs ([$ this ->sqs , $ this ->fifoQueueName , $ this ->account ])->getMock ();
416
+ $ queue ->setContainer ($ container = m::spy (Container::class));
417
+ $ queue ->expects ($ this ->once ())->method ('createPayload ' )->with ($ job , $ this ->fifoQueueName , '' )->willReturn ($ this ->mockedPayload );
418
+ $ queue ->expects ($ this ->once ())->method ('getQueue ' )->with (null )->willReturn ($ this ->fifoQueueUrl );
419
+ $ this ->sqs ->shouldReceive ('sendMessage ' )->once ()->with (['QueueUrl ' => $ this ->fifoQueueUrl , 'MessageBody ' => $ this ->mockedPayload , 'MessageGroupId ' => $ this ->mockedMessageGroupId , 'MessageDeduplicationId ' => $ this ->mockedDeduplicationId ])->andReturn ($ this ->mockedSendMessageResponseModel );
420
+
421
+ $ dispatcher = new Dispatcher ($ container , fn () => $ queue );
422
+ app ()->instance (DispatcherContract::class, $ dispatcher );
423
+
424
+ FakeSqsJob::dispatch ()->onGroup ($ this ->mockedMessageGroupId )->delay ($ this ->mockedDelay );
425
+
426
+ $ container ->shouldHaveReceived ('bound ' )->with ('events ' )->twice ();
427
+
428
+ Str::createUuidsNormally ();
429
+ }
240
430
}
0 commit comments