Skip to content

Commit

Permalink
Add support for notification class overrides (#1518)
Browse files Browse the repository at this point in the history
* Add support for notification class overrides

* use container

* add test

---------

Co-authored-by: Luke Morcom <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent 88bc1f9 commit 4c30f9c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
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.');
}
}

0 comments on commit 4c30f9c

Please sign in to comment.