From 4380ac80189d6902e43d27a9526a112cb0a5eea3 Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Wed, 28 Feb 2018 22:59:53 +0300 Subject: [PATCH] [5.6] Add ability to extend DatabaseNotification model attributes on create (#23337) * Add ability to extend DatabaseNotification model attributes on create * Add tests for Extended Database Notification Channel * Update DatabaseChannel.php --- .../Channels/DatabaseChannel.php | 26 +++++++++++++---- .../NotificationDatabaseChannelTest.php | 28 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index 281dd528c22d..1019e5a4e83a 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 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 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', + ]); + } +}