Skip to content

Commit 2577300

Browse files
[10.x] Allow serialization of NotificationSent (#47375)
* add base64 encoding when serializing SentMessage * slim down expression * adds test case * Update MessageSent.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent e73478a commit 2577300

File tree

4 files changed

+114
-14
lines changed

4 files changed

+114
-14
lines changed

src/Illuminate/Mail/Events/MessageSent.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,10 @@ public function __serialize()
4646
{
4747
$hasAttachments = collect($this->message->getAttachments())->isNotEmpty();
4848

49-
return $hasAttachments ? [
50-
'sent' => base64_encode(serialize($this->sent)),
51-
'data' => base64_encode(serialize($this->data)),
52-
'hasAttachments' => true,
53-
] : [
49+
return [
5450
'sent' => $this->sent,
55-
'data' => $this->data,
56-
'hasAttachments' => false,
51+
'data' => $hasAttachments ? base64_encode(serialize($this->data)) : $this->data,
52+
'hasAttachments' => $hasAttachments,
5753
];
5854
}
5955

@@ -65,13 +61,11 @@ public function __serialize()
6561
*/
6662
public function __unserialize(array $data)
6763
{
68-
if (isset($data['hasAttachments']) && $data['hasAttachments'] === true) {
69-
$this->sent = unserialize(base64_decode($data['sent']));
70-
$this->data = unserialize(base64_decode($data['data']));
71-
} else {
72-
$this->sent = $data['sent'];
73-
$this->data = $data['data'];
74-
}
64+
$this->sent = $data['sent'];
65+
66+
$this->data = (($data['hasAttachments'] ?? false) === true)
67+
? unserialize(base64_decode($data['data']))
68+
: $data['data'];
7569
}
7670

7771
/**

src/Illuminate/Mail/SentMessage.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,32 @@ public function __call($method, $parameters)
5151
{
5252
return $this->forwardCallTo($this->sentMessage, $method, $parameters);
5353
}
54+
55+
/**
56+
* Get the serializable representation of the object.
57+
*
58+
* @return array
59+
*/
60+
public function __serialize()
61+
{
62+
$hasAttachments = collect($this->sentMessage->getOriginalMessage()->getAttachments())->isNotEmpty();
63+
64+
return [
65+
'hasAttachments' => $hasAttachments,
66+
'sentMessage' => $hasAttachments ? base64_encode(serialize($this->sentMessage)) : $this->sentMessage,
67+
];
68+
}
69+
70+
/**
71+
* Marshal the object from its serialized data.
72+
*
73+
* @param array $data
74+
* @return void
75+
*/
76+
public function __unserialize(array $data)
77+
{
78+
$hasAttachments = ($data['hasAttachments'] ?? false) === true;
79+
80+
$this->sentMessage = $hasAttachments ? unserialize(base64_decode($data['sentMessage'])) : $data['sentMessage'];
81+
}
5482
}
777 Bytes
Binary file not shown.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Mail;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Notifications\Events\NotificationSent;
8+
use Illuminate\Notifications\Messages\MailMessage;
9+
use Illuminate\Notifications\Notifiable;
10+
use Illuminate\Notifications\Notification;
11+
use Illuminate\Support\Facades\Event;
12+
use Illuminate\Support\Facades\Schema;
13+
use Orchestra\Testbench\TestCase;
14+
15+
class SentMessageMailTest extends TestCase
16+
{
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
Schema::create('sent_message_users', function (Blueprint $table) {
22+
$table->increments('id');
23+
});
24+
}
25+
26+
public function testDispatchesNotificationSent()
27+
{
28+
$notificationWasSent = false;
29+
30+
$user = SentMessageUser::create();
31+
32+
Event::listen(
33+
NotificationSent::class,
34+
function(NotificationSent $notification) use (&$notificationWasSent, $user) {
35+
$notificationWasSent = true;
36+
/**
37+
* Confirm that NotificationSent can be serialized/unserialized as
38+
* will happen if the listener implements ShouldQueue.
39+
*/
40+
/** @var NotificationSent $afterSerialization */
41+
$afterSerialization = unserialize(serialize($notification));
42+
43+
$this->assertTrue($user->is($afterSerialization->notifiable));
44+
45+
$this->assertEqualsCanonicalizing($notification->notification, $afterSerialization->notification);
46+
});
47+
48+
$user->notify(new SentMessageMailNotification());
49+
50+
$this->assertTrue($notificationWasSent);
51+
}
52+
}
53+
54+
class SentMessageUser extends Model
55+
{
56+
use Notifiable;
57+
58+
public $timestamps = false;
59+
}
60+
61+
62+
class SentMessageMailNotification extends Notification
63+
{
64+
public function via(): array
65+
{
66+
return ['mail'];
67+
}
68+
69+
public function toMail(object $notifiable): MailMessage
70+
{
71+
return (new MailMessage)
72+
->line('Example notification with attachment.')
73+
->attach(__DIR__ . '/Fixtures/blank_document.pdf', [
74+
'as' => 'blank_document.pdf',
75+
'mime' => 'application/pdf',
76+
]);
77+
}
78+
}

0 commit comments

Comments
 (0)