Skip to content

Commit

Permalink
[10.x] Add serializeAndRestore() to NotificationFake (#50935)
Browse files Browse the repository at this point in the history
* Add serializeAndRestore() to NotificationFake

* Fix Style

* Fix

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
dbpolito and taylorotwell authored Apr 7, 2024
1 parent 2298bc3 commit 5d3fa1c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Collection;
Expand All @@ -32,6 +33,13 @@ class NotificationFake implements Fake, NotificationDispatcher, NotificationFact
*/
public $locale;

/**
* Indicates if notifications should be serialized and restored when pushed to the queue.
*
* @var bool
*/
protected $serializeAndRestore = false;

/**
* Assert if a notification was sent on-demand based on a truth-test callback.
*
Expand Down Expand Up @@ -313,7 +321,9 @@ public function sendNow($notifiables, $notification, array $channels = null)
}

$this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [
'notification' => $notification,
'notification' => $this->serializeAndRestore && $notification instanceof ShouldQueue
? $this->serializeAndRestoreNotification($notification)
: $notification,
'channels' => $notifiableChannels,
'notifiable' => $notifiable,
'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
Expand Down Expand Up @@ -349,6 +359,30 @@ public function locale($locale)
return $this;
}

/**
* Specify if notification should be serialized and restored when being "pushed" to the queue.
*
* @param bool $serializeAndRestore
* @return $this
*/
public function serializeAndRestore(bool $serializeAndRestore = true)
{
$this->serializeAndRestore = $serializeAndRestore;

return $this;
}

/**
* Serialize and unserialize the notification to simulate the queueing process.
*
* @param mixed $notification
* @return mixed
*/
protected function serializeAndRestoreNotification($notification)
{
return unserialize(serialize($notification));
}

/**
* Get the notifications that have been sent.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/Support/SupportTestingNotificationFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Support;

use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Foundation\Auth\User;
use Illuminate\Notifications\AnonymousNotifiable;
Expand Down Expand Up @@ -221,6 +223,16 @@ public function testAssertSentToWhenNotifiableHasFalsyShouldSend()

$this->fake->assertNotSentTo($user, NotificationWithFalsyShouldSendStub::class);
}

public function testAssertItCanSerializeAndRestoreNotifications()
{
$this->fake->serializeAndRestore();
$this->fake->send($this->user, new NotificationWithSerialization('hello'));

$this->fake->assertSentTo($this->user, NotificationWithSerialization::class, function ($notification) {
return $notification->value === 'hello-serialized-unserialized';
});
}
}

class NotificationStub extends Notification
Expand Down Expand Up @@ -256,3 +268,22 @@ public function preferredLocale()
return 'au';
}
}

class NotificationWithSerialization extends NotificationStub implements ShouldQueue
{
use Queueable;

public function __construct(public $value)
{
}

public function __serialize(): array
{
return ['value' => $this->value.'-serialized'];
}

public function __unserialize(array $data): void
{
$this->value = $data['value'].'-unserialized';
}
}

0 comments on commit 5d3fa1c

Please sign in to comment.