From a8f9d658792732e44ec5f7273b5350d771a0bda4 Mon Sep 17 00:00:00 2001 From: Meysam Valiolahi Date: Tue, 30 Nov 2021 01:43:49 +0330 Subject: [PATCH] =?UTF-8?q?[8.x]=20Customize=20type=20column=20in=20databa?= =?UTF-8?q?se=20notifications=20using=20databaseType=20me=E2=80=A6=20(#398?= =?UTF-8?q?11)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Customize type column in database notifications using databaseType method. * Update DatabaseChannel.php * formatting Co-authored-by: Taylor Otwell --- .../Channels/DatabaseChannel.php | 36 ++++++++++--------- .../NotificationDatabaseChannelTest.php | 31 ++++++++++++++++ 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index bd8af623144f..8b3167b01508 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -21,6 +21,25 @@ public function send($notifiable, Notification $notification) ); } + /** + * Build an array payload for the DatabaseNotification Model. + * + * @param mixed $notifiable + * @param \Illuminate\Notifications\Notification $notification + * @return array + */ + protected function buildPayload($notifiable, Notification $notification) + { + return [ + 'id' => $notification->id, + 'type' => method_exists($notification, 'databaseType') + ? $notification->databaseType($notifiable) + : get_class($notification), + 'data' => $this->getData($notifiable, $notification), + 'read_at' => null, + ]; + } + /** * Get the data for the notification. * @@ -43,21 +62,4 @@ protected function getData($notifiable, Notification $notification) throw new RuntimeException('Notification is missing toDatabase / toArray method.'); } - - /** - * Build an array payload for the DatabaseNotification Model. - * - * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification - * @return array - */ - protected function buildPayload($notifiable, Notification $notification) - { - return [ - 'id' => $notification->id, - 'type' => get_class($notification), - 'data' => $this->getData($notifiable, $notification), - 'read_at' => null, - ]; - } } diff --git a/tests/Notifications/NotificationDatabaseChannelTest.php b/tests/Notifications/NotificationDatabaseChannelTest.php index 9abcedd5393a..3a45a65a3fc5 100644 --- a/tests/Notifications/NotificationDatabaseChannelTest.php +++ b/tests/Notifications/NotificationDatabaseChannelTest.php @@ -49,6 +49,24 @@ public function testCorrectPayloadIsSentToDatabase() $channel = new ExtendedDatabaseChannel; $channel->send($notifiable, $notification); } + + public function testCustomizeTypeIsSentToDatabase() + { + $notification = new NotificationDatabaseChannelCustomizeTypeTestNotification; + $notification->id = 1; + $notifiable = m::mock(); + + $notifiable->shouldReceive('routeNotificationFor->create')->with([ + 'id' => 1, + 'type' => 'MONTHLY', + 'data' => ['invoice_id' => 1], + 'read_at' => null, + 'something' => 'else', + ]); + + $channel = new ExtendedDatabaseChannel; + $channel->send($notifiable, $notification); + } } class NotificationDatabaseChannelTestNotification extends Notification @@ -59,6 +77,19 @@ public function toDatabase($notifiable) } } +class NotificationDatabaseChannelCustomizeTypeTestNotification extends Notification +{ + public function toDatabase($notifiable) + { + return new DatabaseMessage(['invoice_id' => 1]); + } + + public function databaseType() + { + return 'MONTHLY'; + } +} + class ExtendedDatabaseChannel extends DatabaseChannel { protected function buildPayload($notifiable, Notification $notification)