for($tenant)->create(['status' => Finding::STATUS_NEW]); Carbon::setTestNow('2026-03-19 10:00:00'); $exception = app(FindingExceptionService::class)->request($finding, $tenant, $user, [ 'owner_user_id' => (int) $user->getKey(), 'request_reason' => 'Operational risk accepted temporarily', 'review_due_at' => now()->addDays(14)->toDateTimeString(), 'expires_at' => now()->addDays(30)->toDateTimeString(), ]); expect($exception->status)->toBe(FindingException::STATUS_PENDING) ->and($exception->requested_by_user_id)->toBe((int) $user->getKey()) ->and($exception->request_reason)->toBe('Operational risk accepted temporarily') ->and($exception->currentDecision?->decision_type)->toBe('requested'); Carbon::setTestNow(); }); it('blocks overlapping pending requests for the same finding', function (): void { [$user, $tenant] = createUserWithTenant(role: 'owner'); $finding = Finding::factory()->for($tenant)->create(['status' => Finding::STATUS_NEW]); $service = app(FindingExceptionService::class); $service->request($finding, $tenant, $user, [ 'owner_user_id' => (int) $user->getKey(), 'request_reason' => 'First request', 'review_due_at' => now()->addDays(7)->toDateTimeString(), ]); expect(fn () => $service->request($finding, $tenant, $user, [ 'owner_user_id' => (int) $user->getKey(), 'request_reason' => 'Second request', 'review_due_at' => now()->addDays(10)->toDateTimeString(), ]))->toThrow(InvalidArgumentException::class, 'An exception request is already pending for this finding.'); }); it('blocks self approval and approves with finding mutation otherwise', function (): void { [$requester, $tenant] = createUserWithTenant(role: 'owner'); $approver = \App\Models\User::factory()->create(); createUserWithTenant(tenant: $tenant, user: $approver, role: 'owner'); $finding = Finding::factory()->for($tenant)->create(['status' => Finding::STATUS_NEW]); $service = app(FindingExceptionService::class); $exception = $service->request($finding, $tenant, $requester, [ 'owner_user_id' => (int) $requester->getKey(), 'request_reason' => 'Needs temporary exception', 'review_due_at' => now()->addDays(7)->toDateTimeString(), ]); expect(fn () => $service->approve($exception, $requester, [ 'effective_from' => now()->addHour()->toDateTimeString(), 'expires_at' => now()->addDays(30)->toDateTimeString(), ]))->toThrow(InvalidArgumentException::class, 'Requesters cannot approve their own exception requests.'); $approved = $service->approve($exception->fresh(), $approver, [ 'effective_from' => now()->addHour()->toDateTimeString(), 'expires_at' => now()->addDays(30)->toDateTimeString(), 'approval_reason' => 'Approved with compensating controls', ]); expect($approved->status)->toBe(FindingException::STATUS_ACTIVE) ->and($approved->currentDecision?->decision_type)->toBe('approved') ->and($finding->fresh()?->status)->toBe(Finding::STATUS_RISK_ACCEPTED); });