Skip to content

Commit 5d3fa1c

Browse files
[10.x] Add serializeAndRestore() to NotificationFake (#50935)
* Add serializeAndRestore() to NotificationFake * Fix Style * Fix * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2298bc3 commit 5d3fa1c

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/Illuminate/Support/Testing/Fakes/NotificationFake.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Exception;
77
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
88
use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Contracts\Translation\HasLocalePreference;
1011
use Illuminate\Notifications\AnonymousNotifiable;
1112
use Illuminate\Support\Collection;
@@ -32,6 +33,13 @@ class NotificationFake implements Fake, NotificationDispatcher, NotificationFact
3233
*/
3334
public $locale;
3435

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+
3543
/**
3644
* Assert if a notification was sent on-demand based on a truth-test callback.
3745
*
@@ -313,7 +321,9 @@ public function sendNow($notifiables, $notification, array $channels = null)
313321
}
314322

315323
$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,
317327
'channels' => $notifiableChannels,
318328
'notifiable' => $notifiable,
319329
'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
@@ -349,6 +359,30 @@ public function locale($locale)
349359
return $this;
350360
}
351361

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+
352386
/**
353387
* Get the notifications that have been sent.
354388
*

tests/Support/SupportTestingNotificationFakeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Illuminate\Tests\Support;
44

55
use Exception;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
68
use Illuminate\Contracts\Translation\HasLocalePreference;
79
use Illuminate\Foundation\Auth\User;
810
use Illuminate\Notifications\AnonymousNotifiable;
@@ -221,6 +223,16 @@ public function testAssertSentToWhenNotifiableHasFalsyShouldSend()
221223

222224
$this->fake->assertNotSentTo($user, NotificationWithFalsyShouldSendStub::class);
223225
}
226+
227+
public function testAssertItCanSerializeAndRestoreNotifications()
228+
{
229+
$this->fake->serializeAndRestore();
230+
$this->fake->send($this->user, new NotificationWithSerialization('hello'));
231+
232+
$this->fake->assertSentTo($this->user, NotificationWithSerialization::class, function ($notification) {
233+
return $notification->value === 'hello-serialized-unserialized';
234+
});
235+
}
224236
}
225237

226238
class NotificationStub extends Notification
@@ -256,3 +268,22 @@ public function preferredLocale()
256268
return 'au';
257269
}
258270
}
271+
272+
class NotificationWithSerialization extends NotificationStub implements ShouldQueue
273+
{
274+
use Queueable;
275+
276+
public function __construct(public $value)
277+
{
278+
}
279+
280+
public function __serialize(): array
281+
{
282+
return ['value' => $this->value.'-serialized'];
283+
}
284+
285+
public function __unserialize(array $data): void
286+
{
287+
$this->value = $data['value'].'-unserialized';
288+
}
289+
}

0 commit comments

Comments
 (0)