-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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]>
- Loading branch information
1 parent
e73478a
commit 2577300
Showing
4 changed files
with
114 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Mail; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Notifications\Events\NotificationSent; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notifiable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Schema; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class SentMessageMailTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('sent_message_users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
}); | ||
} | ||
|
||
public function testDispatchesNotificationSent() | ||
{ | ||
$notificationWasSent = false; | ||
|
||
$user = SentMessageUser::create(); | ||
|
||
Event::listen( | ||
NotificationSent::class, | ||
function(NotificationSent $notification) use (&$notificationWasSent, $user) { | ||
$notificationWasSent = true; | ||
/** | ||
* Confirm that NotificationSent can be serialized/unserialized as | ||
* will happen if the listener implements ShouldQueue. | ||
*/ | ||
/** @var NotificationSent $afterSerialization */ | ||
$afterSerialization = unserialize(serialize($notification)); | ||
|
||
$this->assertTrue($user->is($afterSerialization->notifiable)); | ||
|
||
$this->assertEqualsCanonicalizing($notification->notification, $afterSerialization->notification); | ||
}); | ||
|
||
$user->notify(new SentMessageMailNotification()); | ||
|
||
$this->assertTrue($notificationWasSent); | ||
} | ||
} | ||
|
||
class SentMessageUser extends Model | ||
{ | ||
use Notifiable; | ||
|
||
public $timestamps = false; | ||
} | ||
|
||
|
||
class SentMessageMailNotification extends Notification | ||
{ | ||
public function via(): array | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail(object $notifiable): MailMessage | ||
{ | ||
return (new MailMessage) | ||
->line('Example notification with attachment.') | ||
->attach(__DIR__ . '/Fixtures/blank_document.pdf', [ | ||
'as' => 'blank_document.pdf', | ||
'mime' => 'application/pdf', | ||
]); | ||
} | ||
} |