69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|
|
|
use App\Jobs\ExecuteRestoreRunJob;
|
|
use App\Models\BackupSet;
|
|
use App\Models\RestoreRun;
|
|
use App\Models\Tenant;
|
|
use App\Services\Intune\AuditLogger;
|
|
use App\Services\Intune\RestoreService;
|
|
use App\Support\RestoreRunStatus;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Mockery\MockInterface;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
test('execute restore run job moves queued to running and calls the executor', function () {
|
|
$tenant = Tenant::create([
|
|
'tenant_id' => 'tenant-1',
|
|
'name' => 'Tenant One',
|
|
'metadata' => [],
|
|
]);
|
|
|
|
$backupSet = BackupSet::create([
|
|
'tenant_id' => $tenant->id,
|
|
'name' => 'Backup',
|
|
'status' => 'completed',
|
|
'item_count' => 0,
|
|
]);
|
|
|
|
$restoreRun = RestoreRun::create([
|
|
'tenant_id' => $tenant->id,
|
|
'backup_set_id' => $backupSet->id,
|
|
'requested_by' => 'actor@example.com',
|
|
'is_dry_run' => false,
|
|
'status' => RestoreRunStatus::Queued->value,
|
|
'requested_items' => null,
|
|
'preview' => [],
|
|
'results' => null,
|
|
'metadata' => [],
|
|
]);
|
|
|
|
$restoreService = $this->mock(RestoreService::class, function (MockInterface $mock) use ($tenant, $backupSet) {
|
|
$mock->shouldReceive('executeForRun')
|
|
->once()
|
|
->withArgs(function (RestoreRun $run, Tenant $runTenant, BackupSet $runBackupSet, ?string $email, ?string $name) use ($tenant, $backupSet): bool {
|
|
return $run->status === RestoreRunStatus::Running->value
|
|
&& $runTenant->is($tenant)
|
|
&& $runBackupSet->is($backupSet)
|
|
&& $email === 'actor@example.com'
|
|
&& $name === 'Actor';
|
|
})
|
|
->andReturnUsing(function (RestoreRun $run): RestoreRun {
|
|
$run->update([
|
|
'status' => RestoreRunStatus::Completed->value,
|
|
'completed_at' => now(),
|
|
]);
|
|
|
|
return $run->refresh();
|
|
});
|
|
});
|
|
|
|
$job = new ExecuteRestoreRunJob($restoreRun->id, 'actor@example.com', 'Actor');
|
|
$job->handle($restoreService, app(AuditLogger::class));
|
|
|
|
$restoreRun->refresh();
|
|
|
|
expect($restoreRun->started_at)->not->toBeNull();
|
|
expect($restoreRun->status)->toBe(RestoreRunStatus::Completed->value);
|
|
});
|