From 98868b332cc8c31448bae7c0541c19ebc3d56f26 Mon Sep 17 00:00:00 2001 From: a-komarev Date: Wed, 28 Feb 2018 20:20:35 +0300 Subject: [PATCH 1/3] Add ability to extend DatabaseNotification model attributes on create --- .../Channels/DatabaseChannel.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index 281dd528c22d..1b2975474799 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -16,12 +16,9 @@ class DatabaseChannel */ public function send($notifiable, Notification $notification) { - return $notifiable->routeNotificationFor('database', $notification)->create([ - 'id' => $notification->id, - 'type' => get_class($notification), - 'data' => $this->getData($notifiable, $notification), - 'read_at' => null, - ]); + return $notifiable->routeNotificationFor('database', $notification)->create( + $this->buildPayload($notifiable, $notification) + ); } /** @@ -46,4 +43,21 @@ protected function getData($notifiable, Notification $notification) throw new RuntimeException('Notification is missing toDatabase / toArray method.'); } + + /** + * Build up 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, + ]; + } } From d2ac25d99a8d145509c5a2168a39a2872366a17c Mon Sep 17 00:00:00 2001 From: a-komarev Date: Wed, 28 Feb 2018 20:44:25 +0300 Subject: [PATCH 2/3] Add tests for Extended Database Notification Channel --- .../NotificationDatabaseChannelTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Notifications/NotificationDatabaseChannelTest.php b/tests/Notifications/NotificationDatabaseChannelTest.php index bdbbb5508510..96db366b3151 100644 --- a/tests/Notifications/NotificationDatabaseChannelTest.php +++ b/tests/Notifications/NotificationDatabaseChannelTest.php @@ -31,6 +31,24 @@ public function testDatabaseChannelCreatesDatabaseRecordWithProperData() $channel = new DatabaseChannel; $channel->send($notifiable, $notification); } + + public function testCorrectPayloadIsSentToDatabase() + { + $notification = new NotificationDatabaseChannelTestNotification; + $notification->id = 1; + $notifiable = Mockery::mock(); + + $notifiable->shouldReceive('routeNotificationFor->create')->with([ + 'id' => 1, + 'type' => get_class($notification), + 'data' => ['invoice_id' => 1], + 'read_at' => null, + 'something' => 'else', + ]); + + $channel = new ExtendedDatabaseChannel; + $channel->send($notifiable, $notification); + } } class NotificationDatabaseChannelTestNotification extends Notification @@ -40,3 +58,13 @@ public function toDatabase($notifiable) return new DatabaseMessage(['invoice_id' => 1]); } } + +class ExtendedDatabaseChannel extends DatabaseChannel +{ + protected function buildPayload($notifiable, Notification $notification) + { + return array_merge(parent::buildPayload($notifiable, $notification), [ + 'something' => 'else', + ]); + } +} From 38fc4754dd93bd71f93e38b613fec96951f0829c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Feb 2018 13:59:41 -0600 Subject: [PATCH 3/3] Update DatabaseChannel.php --- src/Illuminate/Notifications/Channels/DatabaseChannel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index 1b2975474799..1019e5a4e83a 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -45,7 +45,7 @@ protected function getData($notifiable, Notification $notification) } /** - * Build up an array payload for the DatabaseNotification Model. + * Build an array payload for the DatabaseNotification Model. * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification