Skip to content

Commit

Permalink
Merge pull request #156 from felly/main
Browse files Browse the repository at this point in the history
Fix `DatabaseSizeCheck` to not send notifications on passed checks
  • Loading branch information
freekmurze authored Feb 2, 2023
2 parents dbe8b39 + ba9d08f commit 91a8bd9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Checks/Checks/DatabaseSizeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ public function failWhenSizeAboveGb(float $errorThresholdGb): self

public function run(): Result
{
$result = Result::make();

$databaseSizeInGb = $this->getDatabaseSizeInGb();

$result = Result::make()
->meta([
'database_size' => $databaseSizeInGb,
])
->shortSummary("{$databaseSizeInGb} GB");

return $databaseSizeInGb >= $this->failWhenSizeAboveGb
? $result->failed("Database size is {$databaseSizeInGb} GB, which is above the threshold of {$this->failWhenSizeAboveGb} GB")
: $result->ok("{$databaseSizeInGb} GB");
: $result->ok();
}

protected function getDefaultConnectionName(): string
Expand Down
21 changes: 21 additions & 0 deletions tests/Checks/DatabaseSizeCheckTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

use Illuminate\Support\Facades\Notification;
use Spatie\Health\Checks\Checks\DatabaseSizeCheck;
use Spatie\Health\Commands\RunHealthChecksCommand;
use Spatie\Health\Enums\Status;
use Spatie\Health\Facades\Health;
use Spatie\Health\Tests\TestClasses\FakeDatabaseSizeCheck;
use function Pest\Laravel\artisan;

it('will determine that database size is ok if it does not cross the maximum', function () {
$result = DatabaseSizeCheck::new()
Expand All @@ -20,3 +25,19 @@

expect($result->status)->toBe(Status::failed());
});

it('should not send a notification on a successful check', function () {
registerPassingDatabaseSizeCheck();

artisan(RunHealthChecksCommand::class)->assertSuccessful();

Notification::assertNothingSent();
});

function registerPassingDatabaseSizeCheck()
{
Health::checks([
FakeDatabaseSizeCheck::new()
->fakeDatabaseSizeInGb(0.5),
]);
}
23 changes: 23 additions & 0 deletions tests/TestClasses/FakeDatabaseSizeCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\Health\Tests\TestClasses;

use Closure;
use Spatie\Health\Checks\Checks\DatabaseSizeCheck;

class FakeDatabaseSizeCheck extends DatabaseSizeCheck
{
protected float $fakeDatabaseSizeInGb = 0;

public function fakeDatabaseSizeInGb(float $fakeDatabaseSizeInGb): self
{
$this->fakeDatabaseSizeInGb = $fakeDatabaseSizeInGb;

return $this;
}

public function getDatabaseSizeInGb(): float
{
return $this->fakeDatabaseSizeInGb;
}
}

0 comments on commit 91a8bd9

Please sign in to comment.