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] [WIP] Add notification database channel custom types #23245

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion src/Illuminate/Notifications/Channels/DatabaseChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ public function send($notifiable, Notification $notification)
{
return $notifiable->routeNotificationFor('database', $notification)->create([
'id' => $notification->id,
'type' => get_class($notification),
'type' => $this->getType($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
]);
}

/**
* Get the type of the notification.
*
* @param \Illuminate\Notifications\Notification $notification
* @return array
*/
protected function getType(Notification $notification)
{
if (method_exists($notification, 'broadcastType')) {
return $notification->broadcastType();
}

return get_class($notification);
}

/**
* Get the data for the notification.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Notifications/NotificationDatabaseChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ public function testDatabaseChannelCreatesDatabaseRecordWithProperData()
$channel = new DatabaseChannel;
$channel->send($notifiable, $notification);
}

public function testDatabaseChannelCreatesDatabaseRecordWithCustomType()
{
$notification = new NotificationDatabaseChannelWithCustomTypeTestNotification;
$notification->id = 1;
$notifiable = Mockery::mock();

$notifiable->shouldReceive('routeNotificationFor->create')->with([
'id' => 1,
'type' => 'custom.type',
'data' => ['invoice_id' => 1],
'read_at' => null,
]);

$channel = new DatabaseChannel;
$channel->send($notifiable, $notification);
}
}

class NotificationDatabaseChannelTestNotification extends Notification
Expand All @@ -40,3 +57,16 @@ public function toDatabase($notifiable)
return new DatabaseMessage(['invoice_id' => 1]);
}
}

class NotificationDatabaseChannelWithCustomTypeTestNotification extends Notification
{
public function toDatabase($notifiable)
{
return new DatabaseMessage(['invoice_id' => 1]);
}

public function broadcastType()
{
return 'custom.type';
}
}