RFC §3 Q9 contract — applicator is purpose-agnostic; per-purpose
differences live in PurposeSubjectResolver. Sessie 2's smoke matrix
covered only event_registration; this commit fills the remaining six.
Coverage per file (18 tests total, all passing):
- SignatureContractPurposePipelineTest (3 tests)
happy path + conflict resolution + no_auth missing-context
target: User via auth (User::email/first_name/last_name)
- UserProfilePurposePipelineTest (3 tests)
happy path + conflict resolution + no_auth missing-context
target: User via auth
- SupplierIntakePurposePipelineTest (3 tests)
happy path + conflict resolution + no_production_request missing-context
target: Company subject pre-set by production_request flow
- PostEventEvaluationPurposePipelineTest (4 tests)
happy path + conflict resolution + no_person_for_user + no_auth
target: Person via auth user → Person.event_id link
- IncidentReportPurposePipelineTest (4 tests)
happy path (auth + Person link)
+ conflict resolution
+ anonymous-allowed (null subject → COMPLETED, empty applications)
+ auth-without-Person (null subject branch)
Unique purpose: only one allowed to legitimately resolve to no subject.
- ArtistAdvancePurposePipelineTest (1 test)
no_portal_token missing-context only.
Happy path + subject_not_found branches require the Artist model
(BACKLOG: ARCH-09); morphTo can't materialise a non-existent class.
Documented inline; full coverage follows once ARCH-09 lands.
Each test wires the schema_snapshot directly with the applicator-shape
binding entries (matches sessie 2's FormBindingApplicatorIntegrationTest
pattern). All bindings use registered binding-target attributes from
config/form_builder/binding_targets.php to satisfy BindingTypeRegistry's
strict resolve() at apply time.
Refs: RFC-WS-6.md §3 Q9, ARCH-BINDINGS.md § 6.5
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
166 lines
6.1 KiB
PHP
166 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder\Bindings\Pipeline;
|
|
|
|
use App\Enums\FormBuilder\ApplyStatus;
|
|
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
|
|
use App\Enums\FormBuilder\FormPurpose;
|
|
use App\Exceptions\FormBuilder\PurposeSubjectResolutionException;
|
|
use App\FormBuilder\Bindings\BindingPassResult;
|
|
use App\FormBuilder\Bindings\FormBindingApplicator;
|
|
use App\Models\Company;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormFieldBinding;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormSubmission;
|
|
use App\Models\FormBuilder\FormValue;
|
|
use App\Models\Organisation;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* End-to-end pipeline smoke for the supplier_intake purpose.
|
|
*
|
|
* RFC §3 Q9 case matrix:
|
|
* - Happy path
|
|
* - Conflict resolution (trust precedence)
|
|
* - Missing required context (no production_request → no Company subject)
|
|
*
|
|
* Subject resolution: SupplierIntakeSubjectResolver returns the Company
|
|
* pre-set on submission.subject_type/subject_id by the production_request
|
|
* upstream flow. Throws if no Company subject.
|
|
*
|
|
* Ref: ARCH-BINDINGS.md § 6.5
|
|
*/
|
|
final class SupplierIntakePurposePipelineTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_happy_path_applies_bindings_and_marks_completed(): void
|
|
{
|
|
// Using `name` as the binding target — registered in
|
|
// config/form_builder/binding_targets.php and a real Company column.
|
|
$company = Company::factory()->create(['name' => 'Old Co']);
|
|
|
|
$submission = $this->makeSubmission($company, [
|
|
'name' => ['value' => 'New Co BV', 'trust' => 70],
|
|
]);
|
|
|
|
$result = DB::transaction(fn (): BindingPassResult => resolve(FormBindingApplicator::class)->apply($submission));
|
|
|
|
$this->assertSame(ApplyStatus::COMPLETED, $result->applyStatus());
|
|
$this->assertSame('company', $result->provisionedSubjectType);
|
|
|
|
$company->refresh();
|
|
$this->assertSame('New Co BV', $company->name);
|
|
}
|
|
|
|
public function test_conflict_resolution_picks_highest_trust(): void
|
|
{
|
|
$company = Company::factory()->create(['name' => 'Initial']);
|
|
|
|
$submission = $this->makeSubmission($company, [
|
|
'name__low' => ['value' => 'LowTrust Co', 'trust' => 30, 'attribute' => 'name'],
|
|
'name__high' => ['value' => 'HighTrust Co', 'trust' => 90, 'attribute' => 'name'],
|
|
]);
|
|
|
|
$result = DB::transaction(fn (): BindingPassResult => resolve(FormBindingApplicator::class)->apply($submission));
|
|
|
|
$this->assertSame(ApplyStatus::COMPLETED, $result->applyStatus());
|
|
$company->refresh();
|
|
$this->assertSame('HighTrust Co', $company->name);
|
|
}
|
|
|
|
public function test_missing_company_subject_throws_resolution_exception(): void
|
|
{
|
|
$org = Organisation::factory()->create();
|
|
$schema = FormSchema::factory()->create([
|
|
'organisation_id' => $org->id,
|
|
'purpose' => FormPurpose::SUPPLIER_INTAKE->value,
|
|
]);
|
|
|
|
// production_request flow would have set subject_type='company' +
|
|
// subject_id; without it, the resolver throws.
|
|
$submission = FormSubmission::factory()->create([
|
|
'form_schema_id' => $schema->id,
|
|
'subject_type' => null,
|
|
'subject_id' => null,
|
|
]);
|
|
$submission->schema_snapshot = ['fields' => []];
|
|
$submission->save();
|
|
|
|
try {
|
|
DB::transaction(fn () => resolve(FormBindingApplicator::class)->apply($submission->fresh()));
|
|
$this->fail('Expected PurposeSubjectResolutionException');
|
|
} catch (PurposeSubjectResolutionException $e) {
|
|
$this->assertSame('supplier_intake', $e->purposeSlug);
|
|
$this->assertSame('no_production_request', $e->reasonCode);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array{value:string, trust:int, attribute?:string}> $bindingSpecs
|
|
*/
|
|
private function makeSubmission(Company $company, array $bindingSpecs): FormSubmission
|
|
{
|
|
$schema = FormSchema::factory()->create([
|
|
'organisation_id' => $company->organisation_id,
|
|
'purpose' => FormPurpose::SUPPLIER_INTAKE->value,
|
|
]);
|
|
|
|
$submission = FormSubmission::factory()->create([
|
|
'form_schema_id' => $schema->id,
|
|
'subject_type' => 'company',
|
|
'subject_id' => $company->id,
|
|
]);
|
|
|
|
$snapshotFields = [];
|
|
foreach ($bindingSpecs as $slug => $spec) {
|
|
$attribute = $spec['attribute'] ?? $slug;
|
|
$field = FormField::factory()->create([
|
|
'form_schema_id' => $schema->id,
|
|
'slug' => $slug,
|
|
]);
|
|
$binding = FormFieldBinding::factory()->forField($field)
|
|
->entityOwned('company', $attribute)
|
|
->create([
|
|
'merge_strategy' => FormFieldBindingMergeStrategy::Overwrite->value,
|
|
'trust_level' => $spec['trust'],
|
|
]);
|
|
$snapshotFields[] = [
|
|
'id' => (string) $field->id,
|
|
'slug' => (string) $field->slug,
|
|
'sort_order' => (int) $field->sort_order,
|
|
'bindings' => [[
|
|
'id' => (string) $binding->id,
|
|
'mode' => 'entity_owned',
|
|
'entity' => 'company',
|
|
'column' => $attribute,
|
|
'merge_strategy' => 'overwrite',
|
|
'trust_level' => $spec['trust'],
|
|
'is_identity_key' => false,
|
|
]],
|
|
];
|
|
$this->writeValue($submission->id, $field->id, $spec['value']);
|
|
}
|
|
|
|
$submission->schema_snapshot = ['fields' => $snapshotFields];
|
|
$submission->save();
|
|
|
|
return $submission->fresh();
|
|
}
|
|
|
|
private function writeValue(string $submissionId, string $fieldId, mixed $value): void
|
|
{
|
|
$row = new FormValue;
|
|
$row->form_submission_id = $submissionId;
|
|
$row->form_field_id = $fieldId;
|
|
$row->setAttribute('value', $value);
|
|
$row->value_anonymised = false;
|
|
$row->save();
|
|
}
|
|
}
|