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>
This commit is contained in:
2026-05-08 03:20:27 +02:00
parent 94205164ed
commit 1afe11609a
10 changed files with 1096 additions and 130 deletions

View File

@@ -0,0 +1,149 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\Bindings;
use App\Enums\FormBuilder\FormPurpose;
use App\Exceptions\FormBuilder\FormBindingApplicatorTimeoutException;
use App\FormBuilder\Bindings\BindingPassResult;
use App\FormBuilder\Bindings\FormBindingApplicator;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
/**
* Per RFC-WS-6 §Q1 v1.3 addition 4 + ARCH-BINDINGS §5.3.
*
* The deadline wrapper is a soft post-call microtime check. It cannot
* interrupt mid-query; it catches the long tail of slow applies before
* they hang the public flow.
*
* Uses incident_report purpose with a null submitter so the applicator
* takes the anonymous-allowed branch apply() returns an empty
* BindingPassResult without provisioning a Person, and the deadline
* check still fires at the end of apply(). Avoids the persons-table
* NOT NULL constraints that an event_registration fixture would
* otherwise need full first/last_name bindings to satisfy.
*/
final class FormBindingApplicatorDeadlineTest extends TestCase
{
use RefreshDatabase;
public function test_with_deadline_returns_a_clone(): void
{
$applicator = $this->app->make(FormBindingApplicator::class);
$clone = $applicator->withDeadline(5);
$this->assertInstanceOf(FormBindingApplicator::class, $clone);
$this->assertNotSame($applicator, $clone, 'withDeadline must return a clone, not mutate the receiver');
}
public function test_apply_without_deadline_does_not_throw_for_fast_runs(): void
{
$submission = $this->makeAnonymousSubmission();
$applicator = $this->app->make(FormBindingApplicator::class);
DB::transaction(function () use ($applicator, $submission): void {
$result = $applicator->apply($submission);
$this->assertInstanceOf(BindingPassResult::class, $result);
});
}
public function test_apply_with_generous_deadline_does_not_throw(): void
{
$submission = $this->makeAnonymousSubmission();
$applicator = $this->app->make(FormBindingApplicator::class);
DB::transaction(function () use ($applicator, $submission): void {
$result = $applicator->withDeadline(60)->apply($submission);
$this->assertInstanceOf(BindingPassResult::class, $result);
});
}
public function test_apply_throws_timeout_when_elapsed_exceeds_deadline(): void
{
$submission = $this->makeAnonymousSubmission();
$applicator = new SlowApplicator(
$this->app->make(\App\FormBuilder\Purposes\PurposeRegistry::class),
$this->app->make(\App\FormBuilder\Bindings\BindingConflictResolver::class),
$this->app->make(\App\FormBuilder\Bindings\BindingTypeRegistry::class),
$this->app->make(\App\FormBuilder\Bindings\BindingActivityLogger::class),
);
$applicator->sleepMicroseconds = 50_000; // 50ms
$this->expectException(FormBindingApplicatorTimeoutException::class);
$this->expectExceptionMessageMatches('/exceeded deadline of 0s/');
DB::transaction(function () use ($applicator, $submission): void {
// 0 seconds — any wall-clock elapsed should exceed it.
$applicator->withDeadline(0)->apply($submission);
});
}
public function test_timeout_exception_carries_submission_id_and_reason_code(): void
{
$submission = $this->makeAnonymousSubmission();
$applicator = new SlowApplicator(
$this->app->make(\App\FormBuilder\Purposes\PurposeRegistry::class),
$this->app->make(\App\FormBuilder\Bindings\BindingConflictResolver::class),
$this->app->make(\App\FormBuilder\Bindings\BindingTypeRegistry::class),
$this->app->make(\App\FormBuilder\Bindings\BindingActivityLogger::class),
);
$applicator->sleepMicroseconds = 50_000;
$caught = null;
try {
DB::transaction(function () use ($applicator, $submission): void {
$applicator->withDeadline(0)->apply($submission);
});
} catch (FormBindingApplicatorTimeoutException $e) {
$caught = $e;
}
$this->assertNotNull($caught);
$this->assertSame((string) $submission->id, $caught->submissionId);
// Inherited via FormBindingInfraException — Timeout extends Infra.
$this->assertSame('temporary_error', $caught->reasonCode());
}
/**
* Anonymous incident-report submission: the applicator's
* IncidentReportSubjectResolver returns null when submitted_by_user_id
* is null, and the applicator's anonymous-allowed branch returns an
* empty BindingPassResult. The deadline check still fires at the end.
*/
private function makeAnonymousSubmission(): FormSubmission
{
$schema = FormSchema::factory()->create([
'purpose' => FormPurpose::INCIDENT_REPORT->value,
]);
$submission = FormSubmission::factory()->create([
'form_schema_id' => $schema->id,
'subject_type' => null,
'subject_id' => null,
'submitted_by_user_id' => null,
]);
return $submission->fresh();
}
}
/**
* Test double sleeps inside apply() to force the deadline check at
* the end of apply() to fail for any reasonable deadline value (incl. 0).
*/
final class SlowApplicator extends FormBindingApplicator
{
public int $sleepMicroseconds = 0;
public function apply(FormSubmission $submission, ?string $sectionId = null): BindingPassResult
{
usleep($this->sleepMicroseconds);
return parent::apply($submission, $sectionId);
}
}

View File

@@ -0,0 +1,160 @@
<?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.',
);
}
}