Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add serializeAndRestore() to NotificationFake #50935

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}
Loading