Skip to content

Commit

Permalink
[5.6] Add ability to extend DatabaseNotification model attributes on …
Browse files Browse the repository at this point in the history
…create (#23337)

* Add ability to extend DatabaseNotification model attributes on create

* Add tests for Extended Database Notification Channel

* Update DatabaseChannel.php
  • Loading branch information
Anton Komarev authored and taylorotwell committed Feb 28, 2018
1 parent f85164a commit 4380ac8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Illuminate/Notifications/Channels/DatabaseChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand All @@ -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,
];
}
}
28 changes: 28 additions & 0 deletions tests/Notifications/NotificationDatabaseChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
]);
}
}

0 comments on commit 4380ac8

Please sign in to comment.