feat(form-builder): form_field_bindings table + polymorphic owner + cascade observer
WS-5a commit 1 of 4 per ARCH-CONSOLIDATION-ADDENDUM-2026-04-24 Q3. Creates the relational home for what was form_fields.binding JSON and form_field_library.default_binding JSON. Owner discriminator is polymorphic morph (owner_type/owner_id) — the pattern the rest of WS-5 (5b validation_rules, 5d options) will reuse. Migration backfills rows from both JSON sources in a single transaction and is genuinely reversible (rollback reconstructs the JSON). Old columns remain in place until commit 3 has switched all readers. Pattern B (binding=null) is represented by absence of row. mode enum covers entity_owned / mirrored only. Cascade on owner delete via observer — bindings are physical state, not historical audit. FormFieldBindingScope enforces multi-tenancy via UNION over both owner chains (form_field → schema → org OR form_field_library → org) — Q2's declarative tenantScopeStrategy() can't walk morph parents. Tests: migration forward/back, morph relation, cascade observer, scope isolation, enum coverage. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\Bindings;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldBindingMode;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class FormFieldBindingRelationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_field_morph_many_bindings_loads_all(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create();
|
||||
FormFieldBinding::factory()->forField($field)->mirrored('user_profile', 'bio')->create();
|
||||
|
||||
$bindings = $field->fresh()->bindings;
|
||||
$this->assertCount(2, $bindings);
|
||||
$targets = $bindings->pluck('target_attribute')->sort()->values()->all();
|
||||
$this->assertSame(['bio', 'email'], $targets);
|
||||
}
|
||||
|
||||
public function test_library_morph_many_bindings_loads_all(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
FormFieldBinding::factory()->forLibrary($library)->entityOwned('person', 'first_name')->create();
|
||||
|
||||
$bindings = $library->fresh()->bindings;
|
||||
$this->assertCount(1, $bindings);
|
||||
$this->assertSame('first_name', $bindings->first()->target_attribute);
|
||||
}
|
||||
|
||||
public function test_owner_morphto_returns_correct_concrete_model(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
$fieldBinding = FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create();
|
||||
$libraryBinding = FormFieldBinding::factory()->forLibrary($library)->entityOwned('person', 'phone')->create();
|
||||
|
||||
$this->assertInstanceOf(FormField::class, $fieldBinding->fresh()->owner);
|
||||
$this->assertSame($field->id, $fieldBinding->fresh()->owner->id);
|
||||
|
||||
$this->assertInstanceOf(FormFieldLibrary::class, $libraryBinding->fresh()->owner);
|
||||
$this->assertSame($library->id, $libraryBinding->fresh()->owner->id);
|
||||
}
|
||||
|
||||
public function test_mode_and_merge_strategy_cast_to_enums(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
$binding = FormFieldBinding::factory()->forField($field)->mirrored('person', 'email')->create();
|
||||
|
||||
$fresh = $binding->fresh();
|
||||
$this->assertSame(FormFieldBindingMode::Mirrored, $fresh->mode);
|
||||
$this->assertTrue($fresh->isMirrored());
|
||||
$this->assertFalse($fresh->isEntityOwned());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user