Files
crewli/api/tests/Feature/FormBuilder/Bindings/RetryServiceFailureClassifierTest.php
bert.hausmans 1afe11609a test(form-builder): WS-6 v1.3-delta D2 tests
~30 new tests + 6 modified covering D2 deliverables.

NEW test files:
- FormSubmissionSubmittedListenerOrderTest: rewritten — flips
  identity-match assertion from sync to ShouldQueue + adds AST-level
  structural guard that every queued listener has the
  apply_status=COMPLETED gate as an early statement
  (form-builder.queued-listener.skipped_apply_failed log line + ApplyStatus::COMPLETED check).
- TriggerPersonIdentityMatchOnFormSubmitTest: rewritten — drops
  failsafe-pad assertions; adds gate-skip tests (null/PENDING/PARTIAL/FAILED);
  invariant-violation throw test; broadcast-dispatch test.
- ApplyBindingsOnFormSubmitTest: extended — initial
  identity_match_status='pending' write, apply_completed_at on both
  paths, classifier-derived failure_response_code per exception subclass,
  unknown_error fallback, deadline wrapper invocation captured by
  test double, outer-transaction failure record.
- SyncTagPickerSelectionsOnSubmitGateTest (NEW): canonical skip-log
  assertion for null/PENDING/PARTIAL/FAILED apply_status; no-skip-log
  assertion for COMPLETED. Uses Log::spy because FormTagSyncService
  is final and can't be Mockery-mocked.
- FormBindingApplicatorDeadlineTest (NEW): withDeadline returns clone;
  no-deadline path; generous-deadline path; timeout exception thrown
  with correct submissionId + reasonCode (temporary_error inherited
  via FormBindingInfraException). Uses incident_report purpose for
  anonymous-allowed branch to avoid PersonProvisioner constraints.
- RetryServiceFailureClassifierTest (NEW): per-subclass
  failure_response_code mapping in recordFailure; apply_completed_at
  symmetry-fix coverage.
- SubmissionChannelAuthTest (NEW): submitter authorised, other user
  denied, missing submission denied, org admin currently denied
  (locks v1 contract per BACKLOG TECH-CHANNEL-AUTH-ORG-ADMIN).
- FormSubmissionResourceIdentityMatchTest: extended — DataProvider
  iterates over all six non-person purposes asserting
  identity_match=null per RFC §Q2 v1.3 contract.

MODIFIED to fit v1.3 layout:
- IdentityMatchOnSubmitTest: rewritten — directly invokes the listener
  with apply_status=COMPLETED pre-set, mirroring ApplyBindings'
  happy-path output (the test fixtures lack an identity-key binding
  so going through full event dispatch fails at PersonProvisioner).
  Drops the failsafe-pad assertion in test_public_submission_marked_pending;
  replaces with v1.3 contract: subject_type=null leaves
  identity_match_status untouched.
- TagPickerSyncListenerTest: same fix — sets apply_status=COMPLETED
  on the submission and invokes the listener directly.

Full suite: 1621 passing (4281 assertions). Larastan: 0 errors.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 03:20:27 +02:00

161 lines
6.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\Bindings;
use App\Enums\FormBuilder\ApplyStatus;
use App\Enums\FormBuilder\FormPurpose;
use App\Exceptions\FormBuilder\FormBindingApplicatorTimeoutException;
use App\Exceptions\FormBuilder\FormBindingDataIntegrityException;
use App\Exceptions\FormBuilder\FormBindingInfraException;
use App\Exceptions\FormBuilder\FormBindingSchemaConfigException;
use App\FormBuilder\Bindings\BindingPassResult;
use App\FormBuilder\Bindings\FormBindingApplicator;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use App\Models\FormBuilder\FormSubmissionActionFailure;
use App\Models\Organisation;
use App\Services\FormBuilder\FormFailureRetryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use RuntimeException;
use Tests\TestCase;
/**
* Per ARCH-BINDINGS §7.1 v1.2 + RFC-WS-6 §Q3 v1.3 addition 2.
*
* D2 wires FormBindingExceptionClassifier into
* FormFailureRetryService::recordFailure so the retry-failure path
* mirrors ApplyBindingsOnFormSubmit's outer-transaction catch block:
* - failure_response_code on the parent submission per the exception
* subclass classification.
* - apply_completed_at = now() (asymmetry fix per the v1.2 note).
*/
final class RetryServiceFailureClassifierTest extends TestCase
{
use RefreshDatabase;
private function makeFailure(): FormSubmissionActionFailure
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create([
'organisation_id' => $org->id,
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
]);
$submission = FormSubmission::factory()->create([
'form_schema_id' => $schema->id,
'organisation_id' => $org->id,
]);
return FormSubmissionActionFailure::factory()
->for($submission, 'submission')
->create([
'exception_class' => 'OriginalException',
'exception_message' => 'first failure message',
]);
}
/**
* @param callable(FormSubmission, ?string): BindingPassResult $apply
*/
private function bindApplicator(callable $apply): void
{
$stub = new class($apply) extends FormBindingApplicator
{
/** @var callable(FormSubmission, ?string): BindingPassResult */
private $apply;
/** @param callable(FormSubmission, ?string): BindingPassResult $apply */
public function __construct(callable $apply)
{
$this->apply = $apply;
}
public function apply(FormSubmission $submission, ?string $sectionId = null): BindingPassResult
{
return ($this->apply)($submission, $sectionId);
}
};
$this->app->instance(FormBindingApplicator::class, $stub);
}
private function retryWithThrow(callable $throwFn): FormSubmissionActionFailure
{
$failure = $this->makeFailure();
$this->bindApplicator(fn () => $throwFn());
$service = $this->app->make(FormFailureRetryService::class);
$result = $service->retry($failure);
$this->assertSame('failed', $result['outcome']);
return $failure->fresh();
}
public function test_record_failure_writes_failure_response_code_for_schema_config(): void
{
$failure = $this->retryWithThrow(
fn () => throw new FormBindingSchemaConfigException(submissionId: 'x', message: 'cfg'),
);
$submission = FormSubmission::query()->withoutGlobalScopes()->find($failure->form_submission_id);
$this->assertSame('schema_config_error', $submission->failure_response_code);
$this->assertSame(ApplyStatus::FAILED, $submission->apply_status);
}
public function test_record_failure_writes_failure_response_code_for_infra(): void
{
$failure = $this->retryWithThrow(
fn () => throw new FormBindingInfraException(submissionId: 'x', message: 'infra'),
);
$submission = FormSubmission::query()->withoutGlobalScopes()->find($failure->form_submission_id);
$this->assertSame('temporary_error', $submission->failure_response_code);
}
public function test_record_failure_writes_failure_response_code_for_timeout(): void
{
// Timeout extends Infra → temporary_error inherited.
$failure = $this->retryWithThrow(
fn () => throw new FormBindingApplicatorTimeoutException(submissionId: 'x', message: 'deadline'),
);
$submission = FormSubmission::query()->withoutGlobalScopes()->find($failure->form_submission_id);
$this->assertSame('temporary_error', $submission->failure_response_code);
}
public function test_record_failure_writes_failure_response_code_for_data_integrity(): void
{
$failure = $this->retryWithThrow(
fn () => throw new FormBindingDataIntegrityException(submissionId: 'x', message: 'fk'),
);
$submission = FormSubmission::query()->withoutGlobalScopes()->find($failure->form_submission_id);
$this->assertSame('data_integrity_error', $submission->failure_response_code);
}
public function test_record_failure_writes_unknown_error_for_arbitrary_throwable(): void
{
$failure = $this->retryWithThrow(fn () => throw new RuntimeException('arbitrary'));
$submission = FormSubmission::query()->withoutGlobalScopes()->find($failure->form_submission_id);
$this->assertSame('unknown_error', $submission->failure_response_code);
}
public function test_record_failure_writes_apply_completed_at_symmetry_fix(): void
{
// Per ARCH-BINDINGS §7.1 v1.2 retry-service asymmetry note —
// pre-D2, recordFailure did NOT write apply_completed_at; only
// recordSuccess did. D2 closes the asymmetry.
$failure = $this->retryWithThrow(
fn () => throw new FormBindingInfraException(submissionId: 'x', message: 'transient'),
);
$submission = FormSubmission::query()->withoutGlobalScopes()->find($failure->form_submission_id);
$this->assertNotNull(
$submission->apply_completed_at,
'apply_completed_at must be set on the failure path; pre-D2 only the success path wrote it.',
);
}
}