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

[8.x] Customize type column in database notifications using databaseType me… #39811

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/Illuminate/Notifications/Channels/DatabaseChannel.php
Expand Up @@ -21,6 +21,25 @@ public function send($notifiable, Notification $notification)
);
}

/**
* 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' => method_exists($notification, 'databaseType')
? $notification->databaseType($notifiable)
: get_class($notification),
'data' => $this->getData($notifiable, $notification),
'read_at' => null,
];
}

/**
* Get the data for the notification.
*
Expand All @@ -43,21 +62,4 @@ 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,
];
}
}
31 changes: 31 additions & 0 deletions tests/Notifications/NotificationDatabaseChannelTest.php
Expand Up @@ -49,6 +49,24 @@ public function testCorrectPayloadIsSentToDatabase()
$channel = new ExtendedDatabaseChannel;
$channel->send($notifiable, $notification);
}

public function testCustomizeTypeIsSentToDatabase()
{
$notification = new NotificationDatabaseChannelCustomizeTypeTestNotification;
$notification->id = 1;
$notifiable = m::mock();

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

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

class NotificationDatabaseChannelTestNotification extends Notification
Expand All @@ -59,6 +77,19 @@ public function toDatabase($notifiable)
}
}

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

public function databaseType()
{
return 'MONTHLY';
}
}

class ExtendedDatabaseChannel extends DatabaseChannel
{
protected function buildPayload($notifiable, Notification $notification)
Expand Down