Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Add ability to extend DatabaseNotification model attributes on create #23337

Merged
merged 3 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
]);
}
}