Skip to content

Commit

Permalink
add tests for the CheckFailed notification
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Oct 11, 2023
1 parent 07f9977 commit e37a565
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Notifications/CheckFailedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CheckFailedNotification extends Notification implements ShouldQueue

public function __construct(protected Check $check, protected Result $result)
{
//
}

public function getDriver(string $alias): string
Expand Down
79 changes: 79 additions & 0 deletions tests/Feature/NotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php


use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Notification;
use Vormkracht10\LaravelOK\Checks\Base\Check;
use Vormkracht10\LaravelOK\Checks\Base\Result;
use Vormkracht10\LaravelOK\Events\CheckFailed;
use Vormkracht10\LaravelOK\Notifications\CheckFailedNotification;
use function Pest\Laravel\travel;

$failed = new class extends Check
{
public function run(): Result
{
return Result::new()->failed('failed');
}
};

it('sends a notification if a check has failed', function () use ($failed) {

config()->set('ok.notifications.via.mail.to', 'test@example.com');

$check = $failed->setNotificationInterval(1);

$event = new CheckFailed($check, $check->run());

Notification::fake();

Cache::driver('file')->forget('laravel-ok::notifications::'.$check::class);

event($event);

Notification::assertSentTimes(
CheckFailedNotification::class, 1
);
});

it('doesn\'t send a notification if the notification interval has not yet passed', function () use ($failed) {
config()->set('ok.notifications.via.mail.to', 'test@example.com');

$check = $failed->setNotificationInterval(30);

$event = new CheckFailed($check, $check->run());

Notification::fake();

Cache::driver('file')->forget('laravel-ok::notifications::'.$check::class);

event($event);

event($event);

Notification::assertSentTimes(
CheckFailedNotification::class, 1
);
});

it('does send a notification when the notification interval has passed', function () use ($failed) {
config()->set('ok.notifications.via.mail.to', 'test@example.com');

$check = $failed->setNotificationInterval(30);

$event = new CheckFailed($check, $check->run());

Notification::fake();

Cache::driver('file')->forget('laravel-ok::notifications::'.$check::class);

event($event);

travel(30)->minutes();

event($event);

Notification::assertSentTimes(
CheckFailedNotification::class, 2
);
});

0 comments on commit e37a565

Please sign in to comment.