Files
crewli/api/tests/Unit/Models/FormBuilder/FormSubmissionActionFailureTest.php
bert.hausmans b47e096a55 feat(form-builder): retry history table + integration (WS-6)
Per-attempt retry history (timestamp, user, outcome, exception detail
if failed) replaces the counter-only retry_count tracking.

Changes:

- New `form_submission_action_failure_retry_attempts` table (cascade on
  parent delete, nullOnDelete on user). Explicit short FK names
  (`fsafra_failure_fk`, `fsafra_user_fk`) — auto-generated names exceed
  MySQL's 64-char identifier limit.
- New FormSubmissionActionFailureRetryAttempt model + factory +
  succeeded() state.
- Parent FormSubmissionActionFailure gets retryAttempts() HasMany
  relation (latest('attempted_at')).
- New FormFailureRetryService centralises the retry-flow logic. Both
  the API controller and the artisan command delegate to it. Service
  writes a retry_attempt record per attempt; parent's retry_count
  stays as denormalised cache for index-view performance.
- Successful retry: attempt(succeeded) + parent.retry_count++ +
  parent.resolved_at + parent.resolved_by_user_id + parent.resolved_note
  ("Geslaagde retry door {actor.name}" or "Geslaagde retry
  (geautomatiseerd)" for command-line invocation without an actor).
- Failed retry: attempt(failed) with NEW exception details +
  parent.retry_count++. Parent's exception_class/_message stay
  audit-immutable — they represent the FIRST failure.
- canBeRetried() now correctly checks both resolved_at AND
  dismissed_at (sessie 2's open question Q2 closure).
- New FailureNotRetriableException (controller → 422) and
  ParentSubmissionGoneException (controller → 410) for cleaner
  flow control.

12 new tests:
- FormSubmissionActionFailureRetryAttemptTest (5 unit tests)
- RetryFlowProducesRetryAttemptsTest (7 integration tests covering
  succeeded path, failed path, resolved/dismissed blocking,
  multiple-retries chronological ordering, canBeRetried truth tables)

Pre-existing tests touched:
- FormSubmissionActionFailureTest::test_can_be_retried_only_for_open_state
  — updated to reflect Q2 closure (resolved now blocks too).
- Ws6FoundationMigrationTest::test_down_methods_clean_up_columns_and_table
  — child table must drop before parent (FK constraint).
- 5 backfill test step-counts bumped +1 (new migration sits at top).

SCHEMA.md → v2.9. Schema dump regenerated.

Refs: RFC-WS-6.md §3 Q5 addendum, sessie 2 Q2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 00:14:19 +02:00

98 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Models\FormBuilder;
use App\Enums\FormBuilder\DismissalReasonType;
use App\Models\FormBuilder\FormSubmission;
use App\Models\FormBuilder\FormSubmissionActionFailure;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
final class FormSubmissionActionFailureTest extends TestCase
{
use RefreshDatabase;
public function test_factory_creates_row_with_sensible_defaults(): void
{
$failure = FormSubmissionActionFailure::factory()->create();
$this->assertNotEmpty($failure->id);
$this->assertSame(0, $failure->retry_count);
$this->assertNull($failure->resolved_at);
$this->assertNull($failure->dismissed_at);
$this->assertNotEmpty($failure->context);
}
public function test_submission_relation_returns_parent(): void
{
$submission = FormSubmission::factory()->create();
$failure = FormSubmissionActionFailure::factory()
->for($submission, 'submission')
->create();
$this->assertSame($submission->id, $failure->submission->id);
}
public function test_binding_relation_is_nullable(): void
{
$failure = FormSubmissionActionFailure::factory()->create();
$this->assertNull($failure->binding);
}
public function test_open_scope_excludes_resolved_and_dismissed(): void
{
FormSubmissionActionFailure::factory()->create();
FormSubmissionActionFailure::factory()->resolved()->create();
FormSubmissionActionFailure::factory()->dismissed()->create();
$this->assertSame(1, FormSubmissionActionFailure::query()->open()->count());
$this->assertSame(1, FormSubmissionActionFailure::query()->resolved()->count());
$this->assertSame(1, FormSubmissionActionFailure::query()->dismissed()->count());
}
public function test_dismissed_reason_type_round_trips_as_enum(): void
{
$failure = FormSubmissionActionFailure::factory()->create([
'dismissed_reason_type' => DismissalReasonType::DATA_QUALITY_ISSUE,
'dismissed_at' => now(),
]);
$reloaded = FormSubmissionActionFailure::query()->find($failure->id);
$this->assertSame(DismissalReasonType::DATA_QUALITY_ISSUE, $reloaded->dismissed_reason_type);
}
public function test_context_round_trips_as_array(): void
{
$failure = FormSubmissionActionFailure::factory()->create([
'context' => ['target_entity' => 'company', 'target_attribute' => 'kvk_number'],
]);
$reloaded = FormSubmissionActionFailure::query()->find($failure->id);
$this->assertSame('company', $reloaded->context['target_entity']);
}
public function test_can_be_retried_only_for_open_state(): void
{
// Sessie 3c (Q2 closure): both resolved AND dismissed block retry.
// Open is the only retriable state.
$open = FormSubmissionActionFailure::factory()->create();
$resolved = FormSubmissionActionFailure::factory()->resolved()->create();
$dismissed = FormSubmissionActionFailure::factory()->dismissed()->create();
$this->assertTrue($open->canBeRetried());
$this->assertFalse($resolved->canBeRetried());
$this->assertFalse($dismissed->canBeRetried());
}
public function test_table_has_no_organisation_id_column_per_rfc_v3(): void
{
$this->assertFalse(
Schema::hasColumn('form_submission_action_failures', 'organisation_id'),
'Tenant scope must flow via FK chain to form_submissions.organisation_id (RFC V3)',
);
}
}