feat(form-builder): MergeStrategy enum methods + binding value objects (WS-6)
- FormFieldBindingMergeStrategy::nullWinnerBehaviour() and isValidForScalarTargets() encode the per-strategy null-winner matrix (RFC Q7) and the collection-only restriction (RFC V1). - ResolvedBinding/BindingApplicationResult/BindingPassResult readonly DTOs for the binding pipeline. Construction-time validation for trust level. Apply-status derived from result aggregate. Note: the existing enum is named FormFieldBindingMergeStrategy (not MergeStrategy as the prompt sketched). Methods added to it directly. Refs: RFC-WS-6.md §3 (Q4, Q7), §4 (V1) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\FormBuilder\Bindings;
|
||||
|
||||
use App\FormBuilder\Bindings\BindingApplicationResult;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
final class BindingApplicationResultTest extends TestCase
|
||||
{
|
||||
public function test_succeeded_constructor_populates_state_correctly(): void
|
||||
{
|
||||
$result = BindingApplicationResult::succeeded(
|
||||
bindingId: 'bnd-1',
|
||||
targetEntity: 'person',
|
||||
targetAttribute: 'email',
|
||||
oldValue: null,
|
||||
newValue: 'jan@example.nl',
|
||||
);
|
||||
|
||||
$this->assertTrue($result->success);
|
||||
$this->assertSame('bnd-1', $result->bindingId);
|
||||
$this->assertSame('person', $result->targetEntity);
|
||||
$this->assertSame('email', $result->targetAttribute);
|
||||
$this->assertNull($result->oldValue);
|
||||
$this->assertSame('jan@example.nl', $result->newValue);
|
||||
$this->assertNull($result->exceptionClass);
|
||||
$this->assertNull($result->exceptionMessage);
|
||||
}
|
||||
|
||||
public function test_failed_constructor_captures_exception_metadata(): void
|
||||
{
|
||||
$exception = new RuntimeException('boom');
|
||||
|
||||
$result = BindingApplicationResult::failed(
|
||||
bindingId: 'bnd-2',
|
||||
targetEntity: 'company',
|
||||
targetAttribute: 'name',
|
||||
e: $exception,
|
||||
);
|
||||
|
||||
$this->assertFalse($result->success);
|
||||
$this->assertSame('bnd-2', $result->bindingId);
|
||||
$this->assertNull($result->oldValue);
|
||||
$this->assertNull($result->newValue);
|
||||
$this->assertSame(RuntimeException::class, $result->exceptionClass);
|
||||
$this->assertSame('boom', $result->exceptionMessage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\FormBuilder\Bindings;
|
||||
|
||||
use App\Enums\FormBuilder\ApplyStatus;
|
||||
use App\FormBuilder\Bindings\BindingApplicationResult;
|
||||
use App\FormBuilder\Bindings\BindingPassResult;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
final class BindingPassResultTest extends TestCase
|
||||
{
|
||||
public function test_empty_applications_yield_completed(): void
|
||||
{
|
||||
$result = $this->make([]);
|
||||
$this->assertSame(ApplyStatus::COMPLETED, $result->applyStatus());
|
||||
$this->assertSame(0, $result->successCount());
|
||||
$this->assertSame(0, $result->failureCount());
|
||||
}
|
||||
|
||||
public function test_all_succeeded_yields_completed(): void
|
||||
{
|
||||
$result = $this->make([$this->success('a'), $this->success('b')]);
|
||||
$this->assertSame(ApplyStatus::COMPLETED, $result->applyStatus());
|
||||
$this->assertSame(2, $result->successCount());
|
||||
$this->assertSame(0, $result->failureCount());
|
||||
$this->assertSame([], $result->failures());
|
||||
}
|
||||
|
||||
public function test_all_failed_yields_failed(): void
|
||||
{
|
||||
$result = $this->make([$this->failure('a'), $this->failure('b')]);
|
||||
$this->assertSame(ApplyStatus::FAILED, $result->applyStatus());
|
||||
$this->assertSame(0, $result->successCount());
|
||||
$this->assertSame(2, $result->failureCount());
|
||||
$this->assertCount(2, $result->failures());
|
||||
}
|
||||
|
||||
public function test_mixed_yields_partial(): void
|
||||
{
|
||||
$result = $this->make([
|
||||
$this->success('a'),
|
||||
$this->failure('b'),
|
||||
$this->success('c'),
|
||||
]);
|
||||
$this->assertSame(ApplyStatus::PARTIAL, $result->applyStatus());
|
||||
$this->assertSame(2, $result->successCount());
|
||||
$this->assertSame(1, $result->failureCount());
|
||||
$this->assertCount(1, $result->failures());
|
||||
$this->assertSame('b', $result->failures()[0]->bindingId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<BindingApplicationResult> $applications
|
||||
*/
|
||||
private function make(array $applications): BindingPassResult
|
||||
{
|
||||
return new BindingPassResult(
|
||||
formSubmissionId: 'fs-1',
|
||||
provisionedSubjectType: 'person',
|
||||
provisionedSubjectId: 'p-1',
|
||||
applications: $applications,
|
||||
);
|
||||
}
|
||||
|
||||
private function success(string $bindingId): BindingApplicationResult
|
||||
{
|
||||
return BindingApplicationResult::succeeded(
|
||||
bindingId: $bindingId,
|
||||
targetEntity: 'person',
|
||||
targetAttribute: 'email',
|
||||
oldValue: null,
|
||||
newValue: 'x@y.z',
|
||||
);
|
||||
}
|
||||
|
||||
private function failure(string $bindingId): BindingApplicationResult
|
||||
{
|
||||
return BindingApplicationResult::failed(
|
||||
bindingId: $bindingId,
|
||||
targetEntity: 'person',
|
||||
targetAttribute: 'email',
|
||||
e: new RuntimeException('boom'),
|
||||
);
|
||||
}
|
||||
}
|
||||
101
api/tests/Unit/FormBuilder/Bindings/ResolvedBindingTest.php
Normal file
101
api/tests/Unit/FormBuilder/Bindings/ResolvedBindingTest.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\FormBuilder\Bindings;
|
||||
|
||||
use App\Enums\FormBuilder\BindingTargetType;
|
||||
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
|
||||
use App\FormBuilder\Bindings\ResolvedBinding;
|
||||
use InvalidArgumentException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ResolvedBindingTest extends TestCase
|
||||
{
|
||||
public function test_construction_with_valid_data(): void
|
||||
{
|
||||
$binding = $this->makeWith(['trustLevel' => 80]);
|
||||
$this->assertSame(80, $binding->trustLevel);
|
||||
$this->assertSame('person', $binding->targetEntity);
|
||||
}
|
||||
|
||||
public function test_trust_level_lower_bound(): void
|
||||
{
|
||||
$binding = $this->makeWith(['trustLevel' => 0]);
|
||||
$this->assertSame(0, $binding->trustLevel);
|
||||
}
|
||||
|
||||
public function test_trust_level_upper_bound(): void
|
||||
{
|
||||
$binding = $this->makeWith(['trustLevel' => 100]);
|
||||
$this->assertSame(100, $binding->trustLevel);
|
||||
}
|
||||
|
||||
public function test_trust_level_below_zero_throws(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->makeWith(['trustLevel' => -1]);
|
||||
}
|
||||
|
||||
public function test_trust_level_above_hundred_throws(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->makeWith(['trustLevel' => 101]);
|
||||
}
|
||||
|
||||
public function test_empty_target_entity_throws(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->makeWith(['targetEntity' => '']);
|
||||
}
|
||||
|
||||
public function test_empty_target_attribute_throws(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->makeWith(['targetAttribute' => '']);
|
||||
}
|
||||
|
||||
public function test_null_value_preserved_with_explicit_flag(): void
|
||||
{
|
||||
$binding = $this->makeWith([
|
||||
'value' => null,
|
||||
'valueIsExplicit' => true,
|
||||
]);
|
||||
$this->assertNull($binding->value);
|
||||
$this->assertTrue($binding->valueIsExplicit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $overrides
|
||||
*/
|
||||
private function makeWith(array $overrides): ResolvedBinding
|
||||
{
|
||||
$defaults = [
|
||||
'sourceFormFieldId' => '01jq1k1k1k1k1k1k1k1k1k1k1k',
|
||||
'bindingId' => '01jq1k1k1k1k1k1k1k1k1k1k1l',
|
||||
'targetEntity' => 'person',
|
||||
'targetAttribute' => 'email',
|
||||
'targetType' => BindingTargetType::SCALAR,
|
||||
'mergeStrategy' => FormFieldBindingMergeStrategy::Overwrite,
|
||||
'trustLevel' => 50,
|
||||
'isIdentityKey' => false,
|
||||
'value' => 'jan@example.nl',
|
||||
'valueIsExplicit' => true,
|
||||
];
|
||||
|
||||
$args = array_merge($defaults, $overrides);
|
||||
|
||||
return new ResolvedBinding(
|
||||
sourceFormFieldId: $args['sourceFormFieldId'],
|
||||
bindingId: $args['bindingId'],
|
||||
targetEntity: $args['targetEntity'],
|
||||
targetAttribute: $args['targetAttribute'],
|
||||
targetType: $args['targetType'],
|
||||
mergeStrategy: $args['mergeStrategy'],
|
||||
trustLevel: $args['trustLevel'],
|
||||
isIdentityKey: $args['isIdentityKey'],
|
||||
value: $args['value'],
|
||||
valueIsExplicit: $args['valueIsExplicit'],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user