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()); } }