generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for the CheckFailed notification
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); |