Skip to content

Commit fc0e85e

Browse files
authored
Merge pull request #291 from yoeriboven/carbon-immutable
Allow immutable dates for BackupsCheck
2 parents 5c052de + 8ee77e3 commit fc0e85e

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/Checks/Checks/BackupsCheck.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\Health\Checks\Checks;
44

55
use Carbon\Carbon;
6+
use Carbon\CarbonInterface;
67
use Illuminate\Contracts\Filesystem\Filesystem;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\Facades\File;
@@ -17,9 +18,9 @@ class BackupsCheck extends Check
1718

1819
protected ?Filesystem $disk = null;
1920

20-
protected ?Carbon $youngestShouldHaveBeenMadeBefore = null;
21+
protected ?CarbonInterface $youngestShouldHaveBeenMadeBefore = null;
2122

22-
protected ?Carbon $oldestShouldHaveBeenMadeAfter = null;
23+
protected ?CarbonInterface $oldestShouldHaveBeenMadeAfter = null;
2324

2425
protected ?string $parseModifiedUsing = null;
2526

@@ -52,14 +53,14 @@ public function parseModifiedFormat(string $parseModifiedFormat = 'Y-m-d_H-i-s')
5253
return $this;
5354
}
5455

55-
public function youngestBackShouldHaveBeenMadeBefore(Carbon $date): self
56+
public function youngestBackShouldHaveBeenMadeBefore(CarbonInterface $date): self
5657
{
5758
$this->youngestShouldHaveBeenMadeBefore = $date;
5859

5960
return $this;
6061
}
6162

62-
public function oldestBackShouldHaveBeenMadeAfter(Carbon $date): self
63+
public function oldestBackShouldHaveBeenMadeAfter(CarbonInterface $date): self
6364
{
6465
$this->oldestShouldHaveBeenMadeAfter = $date;
6566

tests/Checks/BackupsCheckTest.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
expect($result)->status->toBe(Status::failed());
154154
});
155155

156-
it('will pass if the backup is at least than the given size when loaded from filesystem disk', function (int $sizeInMb) {
156+
it('will pass if the backup is at least the given size when loaded from filesystem disk', function (int $sizeInMb) {
157157

158158
Storage::fake('backups');
159159

@@ -199,6 +199,30 @@
199199
expect($result)->status->toBe(Status::failed());
200200
});
201201

202+
it('can check if the youngest backup is recent enough when loaded from filesystem disk with an immutable date', function () {
203+
204+
Storage::fake('backups');
205+
Storage::disk('backups')->put('backups/hey.zip', 'content');
206+
207+
testTime()->addMinutes(4);
208+
209+
$result = $this->backupsCheck
210+
->onDisk('backups')
211+
->locatedAt('backups')
212+
->youngestBackShouldHaveBeenMadeBefore(now()->subMinutes(5)->startOfMinute()->toImmutable())
213+
->run();
214+
215+
expect($result)->status->toBe(Status::ok());
216+
217+
testTime()->addMinutes(2);
218+
219+
$result = $this->backupsCheck
220+
->locatedAt($this->temporaryDirectory->path('*.zip'))
221+
->youngestBackShouldHaveBeenMadeBefore(now()->subMinutes(5)->toImmutable())
222+
->run();
223+
expect($result)->status->toBe(Status::failed());
224+
});
225+
202226
it('can check if the oldest backup is old enough when loaded from filesystem disk', function () {
203227

204228
Storage::fake('backups');
@@ -223,6 +247,30 @@
223247
expect($result)->status->toBe(Status::failed());
224248
});
225249

250+
it('can check if the oldest backup is old enough when loaded from filesystem disk with an immutable date', function () {
251+
252+
Storage::fake('backups');
253+
Storage::disk('backups')->put('backups/hey.zip', 'content');
254+
255+
testTime()->addMinutes(4);
256+
257+
$result = $this->backupsCheck
258+
->onDisk('backups')
259+
->locatedAt('backups')
260+
->oldestBackShouldHaveBeenMadeAfter(now()->subMinutes(5)->toImmutable())
261+
->run();
262+
263+
expect($result)->status->toBe(Status::failed());
264+
265+
testTime()->addMinutes(2);
266+
267+
$result = $this->backupsCheck
268+
->locatedAt($this->temporaryDirectory->path('*.zip'))
269+
->oldestBackShouldHaveBeenMadeAfter(now()->subMinutes(5)->toImmutable())
270+
->run();
271+
expect($result)->status->toBe(Status::failed());
272+
});
273+
226274
it('can parse modified time from file name', function ($format) {
227275
Storage::fake('backups');
228276

0 commit comments

Comments
 (0)