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

Add support for notification class overrides #1518

Merged
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
8 changes: 8 additions & 0 deletions src/Contracts/LongWaitDetectedNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Horizon\Contracts;

interface LongWaitDetectedNotification
{
//
}
11 changes: 7 additions & 4 deletions src/Events/LongWaitDetected.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Laravel\Horizon\Events;

use Laravel\Horizon\Notifications\LongWaitDetected as LongWaitDetectedNotification;
use Illuminate\Container\Container;
use Laravel\Horizon\Contracts\LongWaitDetectedNotification;

class LongWaitDetected
{
Expand Down Expand Up @@ -49,8 +50,10 @@ public function __construct($connection, $queue, $seconds)
*/
public function toNotification()
{
return new LongWaitDetectedNotification(
$this->connection, $this->queue, $this->seconds
);
return Container::getInstance()->make(LongWaitDetectedNotification::class, [
'connection' => $this->connection,
'queue' => $this->queue,
'seconds' => $this->seconds,
]);
}
}
3 changes: 2 additions & 1 deletion src/Notifications/LongWaitDetected.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\SlackMessage as ChannelIdSlackMessage;
use Illuminate\Support\Str;
use Laravel\Horizon\Contracts\LongWaitDetectedNotification;
use Laravel\Horizon\Horizon;

class LongWaitDetected extends Notification
class LongWaitDetected extends Notification implements LongWaitDetectedNotification
{
use Queueable;

Expand Down
3 changes: 3 additions & 0 deletions src/ServiceBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ trait ServiceBindings
Contracts\SupervisorRepository::class => Repositories\RedisSupervisorRepository::class,
Contracts\TagRepository::class => Repositories\RedisTagRepository::class,
Contracts\WorkloadRepository::class => Repositories\RedisWorkloadRepository::class,

// Notifications...
Contracts\LongWaitDetectedNotification::class => Notifications\LongWaitDetected::class,
];
}
48 changes: 48 additions & 0 deletions tests/Feature/NotificationOverridesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Laravel\Horizon\Tests\Feature;

use Illuminate\Container\Container;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Notification;
use Laravel\Horizon\Contracts\LongWaitDetectedNotification as LongWaitDetectedNotificationContract;
use Laravel\Horizon\Events\LongWaitDetected;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\Notifications\LongWaitDetected as LongWaitDetectedNotification;
use Laravel\Horizon\Tests\IntegrationTest;

class NotificationOverridesTest extends IntegrationTest
{
public function test_custom_notifications_are_sent_if_specified()
{
Notification::fake();

Horizon::routeMailNotificationsTo('[email protected]');

Container::getInstance()->bind(LongWaitDetectedNotificationContract::class, CustomLongWaitDetectedNotification::class);

event(new LongWaitDetected('redis', 'test-queue-2', 60));

Notification::assertSentOnDemand(CustomLongWaitDetectedNotification::class);
}

public function test_normal_notifications_are_sent_if_not_specified()
{
Notification::fake();

Horizon::routeMailNotificationsTo('[email protected]');

event(new LongWaitDetected('redis', 'test-queue-2', 60));

Notification::assertSentOnDemand(LongWaitDetectedNotification::class);
}
}

class CustomLongWaitDetectedNotification extends LongWaitDetectedNotification implements LongWaitDetectedNotificationContract
{
public function toMail($notifiable)
{
return (new MailMessage)
->line('This is a custom notification for a long wait.');
}
}