6
6
use Exception ;
7
7
use Illuminate \Contracts \Notifications \Dispatcher as NotificationDispatcher ;
8
8
use Illuminate \Contracts \Notifications \Factory as NotificationFactory ;
9
+ use Illuminate \Contracts \Queue \ShouldQueue ;
9
10
use Illuminate \Contracts \Translation \HasLocalePreference ;
10
11
use Illuminate \Notifications \AnonymousNotifiable ;
11
12
use Illuminate \Support \Collection ;
@@ -32,6 +33,13 @@ class NotificationFake implements Fake, NotificationDispatcher, NotificationFact
32
33
*/
33
34
public $ locale ;
34
35
36
+ /**
37
+ * Indicates if notifications should be serialized and restored when pushed to the queue.
38
+ *
39
+ * @var bool
40
+ */
41
+ protected $ serializeAndRestore = false ;
42
+
35
43
/**
36
44
* Assert if a notification was sent on-demand based on a truth-test callback.
37
45
*
@@ -313,7 +321,9 @@ public function sendNow($notifiables, $notification, array $channels = null)
313
321
}
314
322
315
323
$ this ->notifications [get_class ($ notifiable )][$ notifiable ->getKey ()][get_class ($ notification )][] = [
316
- 'notification ' => $ notification ,
324
+ 'notification ' => $ this ->serializeAndRestore && $ notification instanceof ShouldQueue
325
+ ? $ this ->serializeAndRestoreNotification ($ notification )
326
+ : $ notification ,
317
327
'channels ' => $ notifiableChannels ,
318
328
'notifiable ' => $ notifiable ,
319
329
'locale ' => $ notification ->locale ?? $ this ->locale ?? value (function () use ($ notifiable ) {
@@ -349,6 +359,30 @@ public function locale($locale)
349
359
return $ this ;
350
360
}
351
361
362
+ /**
363
+ * Specify if notification should be serialized and restored when being "pushed" to the queue.
364
+ *
365
+ * @param bool $serializeAndRestore
366
+ * @return $this
367
+ */
368
+ public function serializeAndRestore (bool $ serializeAndRestore = true )
369
+ {
370
+ $ this ->serializeAndRestore = $ serializeAndRestore ;
371
+
372
+ return $ this ;
373
+ }
374
+
375
+ /**
376
+ * Serialize and unserialize the notification to simulate the queueing process.
377
+ *
378
+ * @param mixed $notification
379
+ * @return mixed
380
+ */
381
+ protected function serializeAndRestoreNotification ($ notification )
382
+ {
383
+ return unserialize (serialize ($ notification ));
384
+ }
385
+
352
386
/**
353
387
* Get the notifications that have been sent.
354
388
*
0 commit comments