From 8a352293f67876eace4299fa24849b78b58fe453 Mon Sep 17 00:00:00 2001 From: Stefan Damjanovic <> Date: Tue, 13 Dec 2022 20:08:43 +0100 Subject: [PATCH 1/3] Running checks conditionally --- docs/viewing-results/general.md | 15 ++++++++ src/Checks/Check.php | 20 ++++++++++ tests/HealthTest.php | 66 +++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/docs/viewing-results/general.md b/docs/viewing-results/general.md index e769c235..53557e40 100644 --- a/docs/viewing-results/general.md +++ b/docs/viewing-results/general.md @@ -22,3 +22,18 @@ Health::checks([ UsedDiskSpaceCheck::new()->label('Disk space on main disk'), ]); ``` + +## Running checks conditionally + +If you would like to conditionally run a checks, you can use the if and unless methods. + +```php +use Spatie\Health\Facades\Health; +use Spatie\Health\Checks\Checks\DebugModeCheck; +use Spatie\Health\Checks\Checks\RedisCheck; + +Health::checks([ + DebugModeCheck::new()->if(app()->isProduction()), + RedisCheck::new()->unless(app()->environment('development')), +]); +``` diff --git a/src/Checks/Check.php b/src/Checks/Check.php index 84aa3c75..432a5ee5 100755 --- a/src/Checks/Check.php +++ b/src/Checks/Check.php @@ -18,6 +18,8 @@ abstract class Check protected ?string $label = null; + protected bool $shouldRun = true; + final public function __construct() { } @@ -69,11 +71,29 @@ public function getName(): string public function shouldRun(): bool { + if (! $this->shouldRun) { + return false; + } + $date = Date::now(); return (new CronExpression($this->expression))->isDue($date->toDateTimeString()); } + public function if(bool $condition) + { + $this->shouldRun = $condition; + + return $this; + } + + public function unless(bool $condition) + { + $this->shouldRun = !$condition; + + return $this; + } + abstract public function run(): Result; public function markAsCrashed(): Result diff --git a/tests/HealthTest.php b/tests/HealthTest.php index 0cfa7395..a8abe557 100644 --- a/tests/HealthTest.php +++ b/tests/HealthTest.php @@ -1,10 +1,12 @@ toBeInstanceOf(UsedDiskSpaceCheck::class); }); +it('can run checks conditionally using if method', function () { + Health::checks([ + UsedDiskSpaceCheck::new(), + DebugModeCheck::new()->if(false), + ]); + + $checks = Health::registeredChecks()->filter(function (Check $check) { + return $check->shouldRun(); + }); + + expect($checks) + ->toHaveCount(1) + ->and($checks->first()) + ->toBeInstanceOf(UsedDiskSpaceCheck::class); + + Health::clearChecks(); + + Health::checks([ + UsedDiskSpaceCheck::new(), + DebugModeCheck::new()->if(true), + ]); + + $checks = Health::registeredChecks()->filter(function (Check $check) { + return $check->shouldRun(); + }); + + expect($checks) + ->toHaveCount(2) + ->and($checks[1]) + ->toBeInstanceOf(DebugModeCheck::class); +}); + +it('can run checks conditionally using unless method', function () { + Health::checks([ + UsedDiskSpaceCheck::new(), + DebugModeCheck::new()->unless(true), + ]); + + $checks = Health::registeredChecks()->filter(function (Check $check) { + return $check->shouldRun(); + }); + + expect($checks) + ->toHaveCount(1) + ->and($checks->first()) + ->toBeInstanceOf(UsedDiskSpaceCheck::class); + + Health::clearChecks(); + + Health::checks([ + UsedDiskSpaceCheck::new(), + DebugModeCheck::new()->unless(false), + ]); + + $checks = Health::registeredChecks()->filter(function (Check $check) { + return $check->shouldRun(); + }); + + expect($checks) + ->toHaveCount(2) + ->and($checks[1]) + ->toBeInstanceOf(DebugModeCheck::class); +}); + it('will throw an exception when duplicate checks are registered', function () { Health::checks([ PingCheck::new(), From 963ebfc6aeda89ccb1e3baaff37252935d72f8d0 Mon Sep 17 00:00:00 2001 From: Stefan Damjanovic <> Date: Tue, 13 Dec 2022 20:13:20 +0100 Subject: [PATCH 2/3] Add class usage to test case --- tests/HealthTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/HealthTest.php b/tests/HealthTest.php index bcea8379..6733c918 100644 --- a/tests/HealthTest.php +++ b/tests/HealthTest.php @@ -10,6 +10,8 @@ use Spatie\Health\Exceptions\InvalidCheck; use Spatie\Health\Facades\Health; use Spatie\Health\Testing\FakeCheck; +use Spatie\Health\Checks\Checks\DebugModeCheck; +use Spatie\Health\Checks\Check; it('can register checks', function () { Health::checks([ From b4daec4ad5e4e1627d53dd2abe9eeca845df3ad8 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Wed, 14 Dec 2022 10:53:14 +0100 Subject: [PATCH 3/3] Update general.md --- docs/viewing-results/general.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/viewing-results/general.md b/docs/viewing-results/general.md index 53557e40..a8943cd9 100644 --- a/docs/viewing-results/general.md +++ b/docs/viewing-results/general.md @@ -25,7 +25,7 @@ Health::checks([ ## Running checks conditionally -If you would like to conditionally run a checks, you can use the if and unless methods. +If you would like to conditionally run a check, you can use the `if` and `unless` methods. ```php use Spatie\Health\Facades\Health;