Files
crewli/api/tests/Unit/FormBuilder/Publishing/AppendStrategyRequiresCollectionTargetTest.php
bert.hausmans 0e986f42cb refactor(form-builder): align binding registry with model column reality (WS-6)
Three renames (registry → matches actual Eloquent model column):
  - person.phone_number       → person.phone
  - company.email             → company.contact_email
  - company.phone_number      → company.contact_phone

Six removals (registry attribute does not exist as model column,
intentionally deferred):
  - person.dietary_preferences  (custom_fields JSON path; BACKLOG
    FORM-BINDING-JSON-PATH)
  - artist.email                (Artist model absent + column absent)
  - artist.stage_name           (column absent)
  - artist.tech_rider           (column absent)
  - artist.hospitality_rider    (column absent)
  - artist entity removed entirely (no v1 bindable attributes)

Decisions documented inline in binding_targets.php and tracked
via BACKLOG entries (Task 4 of this session).

Tests touched:
- BindingTypeRegistryTest:
    test_resolve_person_dietary_preferences_returns_collection_array →
      renamed test_resolve_collection_attribute_returns_collection_array,
      uses Config::set to inject a synthetic 'test_entity.tags' collection
      target. v1 has no production collection targets (BACKLOG
      FORM-BINDING-JSON-PATH).
    test_validate_append_strategy_accepts_collection_target — same pattern.
    test_entities_returns_known_entities — drop 'artist' from expected list.
    test_attributes_for_person_includes_email_and_dietary_preferences →
      renamed _includes_email_and_phone (the renamed attribute).
- AppendStrategyRequiresCollectionTargetTest:
    test_passes_with_collection_target — same Config::set synthetic-
    target pattern.
- MaxOneIdentityKeyPerTargetEntityTest:
    test_passes_with_one_identity_key_each_on_different_entities —
    'company.email' → 'company.contact_email' to match registry rename.

Refs: WS-6 sessie 3a binding-target drift audit

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 00:14:13 +02:00

89 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
use App\FormBuilder\Bindings\BindingTypeRegistry;
use App\FormBuilder\Publishing\AppendStrategyRequiresCollectionTarget;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldBinding;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class AppendStrategyRequiresCollectionTargetTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_no_append_strategy_present(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Overwrite->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_with_scalar_target_reason(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Append->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('scalar_target', $result->context['reason']);
}
public function test_passes_with_collection_target(): void
{
// Sessie 3a.5: v1 registry has no collection targets in any
// production entity. Inject a synthetic collection target via
// Config::set so the guard's collection-allowed branch stays
// under test. Tracked: BACKLOG FORM-BINDING-JSON-PATH.
config()->set('form_builder.binding_targets.test_entity', [
'tags' => ['type' => 'collection', 'php' => 'array', 'identity_key_eligible' => false],
]);
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('test_entity', 'tags')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Append->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_with_unknown_target_reason(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'unknown_attr')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Append->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('unknown_target', $result->context['reason']);
}
private function guard(): AppendStrategyRequiresCollectionTarget
{
return new AppendStrategyRequiresCollectionTarget(
$this->app->make(BindingTypeRegistry::class),
);
}
}