Skip to content

Commit

Permalink
- Add tests with the new Sleep helper (Laravel 10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoan Ballesteros committed Jul 27, 2023
1 parent 2e574f2 commit 06494fa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
26 changes: 25 additions & 1 deletion tests/Commands/BackupCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Sleep;
use Illuminate\Support\Facades\Event;
use Carbon\CarbonInterval as Duration;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;
use Spatie\Backup\Events\BackupHasFailed;
use Spatie\Backup\Events\BackupZipWasCreated;
Expand Down Expand Up @@ -378,3 +380,25 @@
$this->assertStringContainsString('Attempt n°2...', $output);
$this->assertStringContainsString('Attempt n°3...', $output);
});

it('should wait before trying again when retry_delay is configured (with Sleep helper)', function () {
Sleep::fake();

config()->set('backup.backup.tries', 3);
config()->set('backup.backup.retry_delay', 3);

// Use an invalid dbname to trigger failure
$exitCode = Artisan::call('backup:run --only-db --db-name=wrongName');
$output = Artisan::output();

expect($exitCode)->toEqual(1);

$this->assertStringContainsString('Attempt n°2...', $output);
$this->assertStringContainsString('Attempt n°3...', $output);

Sleep::assertSleptTimes(2);
Sleep::assertSequence([
Sleep::for(3)->seconds(),
Sleep::for(3)->seconds(),
]);
})->skip(!isSleepHelperAvailable(), 'requires the Sleep helper (Laravel >= 10)');
27 changes: 26 additions & 1 deletion tests/Commands/CleanupCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Illuminate\Support\Sleep;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;
use Spatie\Backup\Events\CleanupWasSuccessful;

Expand Down Expand Up @@ -178,3 +179,27 @@
$this->assertStringContainsString('Attempt n°2...', $output);
$this->assertStringContainsString('Attempt n°3...', $output);
});

it('should wait before trying again when retry_delay is configured (with Sleep helper)', function () {
Sleep::fake();

// Use an invalid destination disk to trigger an exception
config()->set('backup.backup.destination.disks', ['wrong']);

config()->set('backup.cleanup.tries', 3);
config()->set('backup.cleanup.retry_delay', 3);

$exitCode = Artisan::call('backup:clean');
$output = Artisan::output();

expect($exitCode)->toEqual(1);

$this->assertStringContainsString('Attempt n°2...', $output);
$this->assertStringContainsString('Attempt n°3...', $output);

Sleep::assertSleptTimes(2);
Sleep::assertSequence([
Sleep::for(3)->seconds(),
Sleep::for(3)->seconds(),
]);
})->skip(!isSleepHelperAvailable(), 'requires the Sleep helper (Laravel >= 10)');

0 comments on commit 06494fa

Please sign in to comment.