Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DatabaseSizeCheck to not send notifications on passed checks #156

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}