diff --git a/src/Checks/Checks/RecentBackupCheck.php b/src/Checks/Checks/BackupsCheck.php similarity index 88% rename from src/Checks/Checks/RecentBackupCheck.php rename to src/Checks/Checks/BackupsCheck.php index e49b061e..4db63d1d 100644 --- a/src/Checks/Checks/RecentBackupCheck.php +++ b/src/Checks/Checks/BackupsCheck.php @@ -9,7 +9,7 @@ use Spatie\Health\Checks\Result; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; -class RecentBackupCheck extends Check +class BackupsCheck extends Check { protected ?string $locatedAt = null; @@ -18,6 +18,8 @@ class RecentBackupCheck extends Check protected int $minimumSizeInMegabytes = 0; + protected int $minimumNumberOfBackups = 0; + public function locatedAt(string $globPath): self { $this->locatedAt = $globPath; @@ -46,6 +48,13 @@ public function atLeastSizeInMb(int $minimumSizeInMegabytes): self return $this; } + public function atLeastNumberOfBackups(int $minimumNumberOfBackups): self + { + $this->minimumNumberOfBackups = $minimumNumberOfBackups; + + return $this; + } + public function run(): Result { $files = collect(File::glob($this->locatedAt)); @@ -66,6 +75,10 @@ public function run(): Result return Result::make()->failed('No backups found that are large enough'); } + if ($eligableBackups->count() < $this->minimumNumberOfBackups) { + return Result::make()->failed('Not enough backups found'); + } + if ($this->youngestShouldHaveBeenMadeBefore) { if ($this->youngestBackupIsToolOld($eligableBackups)) { return Result::make() diff --git a/tests/Checks/RecentBackupCheckTest.php b/tests/Checks/BackupsCheckTest.php similarity index 74% rename from tests/Checks/RecentBackupCheckTest.php rename to tests/Checks/BackupsCheckTest.php index b1f1f641..ccffe4d6 100644 --- a/tests/Checks/RecentBackupCheckTest.php +++ b/tests/Checks/BackupsCheckTest.php @@ -1,16 +1,16 @@ recentBackupCheck = RecentBackupCheck::new(); + $this->backupsCheck = BackupsCheck::new(); Health::checks([ - RecentBackupCheck::new(), + BackupsCheck::new(), ]); $this->temporaryDirectory = TemporaryDirectory::make(getTemporaryDirectory())->force()->empty(); @@ -22,7 +22,7 @@ it('it will succeed if a file with the given glob exist', function() { addTestFile($this->temporaryDirectory->path('hey.zip')); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->run(); @@ -32,7 +32,7 @@ it('it will fail if a file with the given glob does not exist', function() { addTestFile($this->temporaryDirectory->path('hey.other')); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->run(); @@ -40,7 +40,7 @@ }); it('will fail if the given directory does not exist', function() { - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt('non-existing-directory') ->run(); @@ -50,7 +50,7 @@ it('will fail if the backup is smaller than the given size', function() { addTestFile($this->temporaryDirectory->path('hey.zip'), sizeInMb: 4); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->atLeastSizeInMb(5) ->run(); @@ -61,7 +61,7 @@ it('will pass if the backup is at least than the given size', function(int $sizeInMb) { addTestFile($this->temporaryDirectory->path('hey.zip'), sizeInMb: $sizeInMb); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->atLeastSizeInMb(5) ->run(); @@ -77,7 +77,7 @@ testTime()->addMinutes(4); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->youngestBackShouldHaveBeenMadeBefore(now()->subMinutes(5)->startOfMinute()) ->run(); @@ -85,7 +85,7 @@ testTime()->addMinute(); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->youngestBackShouldHaveBeenMadeBefore(now()->subMinutes(5)) ->run(); @@ -97,7 +97,7 @@ testTime()->addMinutes(4); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->oldestBackShouldHaveBeenMadeAfter(now()->subMinutes(5)) ->run(); @@ -106,12 +106,30 @@ testTime()->addMinute(); - $result = $this->recentBackupCheck + $result = $this->backupsCheck ->locatedAt($this->temporaryDirectory->path('*.zip')) ->oldestBackShouldHaveBeenMadeAfter(now()->subMinutes(5)) ->run(); expect($result)->status->toBe(Status::failed()); }); +it('can check that there are enough backups', function() { + addTestFile($this->temporaryDirectory->path('first.zip')); + + $result = $this->backupsCheck + ->locatedAt($this->temporaryDirectory->path('*.zip')) + ->atLeastNumberOfBackups(2) + ->run(); + expect($result)->status->toBe(Status::failed()); + + addTestFile($this->temporaryDirectory->path('second.zip')); + + $result = $this->backupsCheck + ->locatedAt($this->temporaryDirectory->path('*.zip')) + ->atLeastNumberOfBackups(2) + ->run(); + expect($result)->status->toBe(Status::ok()); +}); +