Files
crewli/api/tests/Unit/FormBuilder/Publishing/ConditionalRequirementTest.php
bert.hausmans 81a8120f98 feat(form-builder): add PublishGuard framework + 9 concrete guards (WS-6)
Per-purpose schema validation composes a PurposeGuardProvider returning
a list of guards. Errors collected (not first-fail) so the builder UI
surfaces every issue per save. ConditionalRequirement composes higher-
order without proliferating one-off classes.

RequiresIdentityKeyBinding checks the is_identity_key flag specifically;
the binding-existence check is handled additively by the existing
assertRequiredBindingsPresent in FormSchemaService.

SchemaHasLinkedEvent checks owner_type='event' + owner_id (FormSchema
uses polymorphic owner; there is no direct event_id column).

i18n messages live in lang/nl/form_builder_publish_guards.php.

Refs: RFC-WS-6.md §3 (Q13), §4 (V1, V3)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 22:55:42 +02:00

86 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\ConditionalRequirement;
use App\FormBuilder\Publishing\PublishGuard;
use App\FormBuilder\Publishing\PublishGuardResult;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class ConditionalRequirementTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_predicate_false_regardless_of_sub_guard(): void
{
$schema = FormSchema::factory()->create();
$guard = new ConditionalRequirement(
predicate: fn (): bool => false,
subGuard: $this->alwaysFails(),
code: 'test_predicate_false',
);
$result = $guard->evaluate($schema);
$this->assertTrue($result->passed);
$this->assertSame('conditional:test_predicate_false', $result->guardCode);
}
public function test_passes_when_predicate_true_and_sub_passes(): void
{
$schema = FormSchema::factory()->create();
$guard = new ConditionalRequirement(
predicate: fn (): bool => true,
subGuard: $this->alwaysPasses(),
code: 'test_sub_passes',
);
$result = $guard->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_predicate_true_and_sub_fails(): void
{
$schema = FormSchema::factory()->create();
$guard = new ConditionalRequirement(
predicate: fn (): bool => true,
subGuard: $this->alwaysFails(),
code: 'test_sub_fails',
);
$result = $guard->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('conditional:test_sub_fails', $result->guardCode);
$this->assertSame('inner_failure', $result->messageKey);
$this->assertSame('inner_guard', $result->context['delegated_to']);
}
private function alwaysPasses(): PublishGuard
{
return new class implements PublishGuard {
public function code(): string { return 'inner_guard'; }
public function evaluate(FormSchema $schema): PublishGuardResult
{
return PublishGuardResult::passed($this->code());
}
};
}
private function alwaysFails(): PublishGuard
{
return new class implements PublishGuard {
public function code(): string { return 'inner_guard'; }
public function evaluate(FormSchema $schema): PublishGuardResult
{
return PublishGuardResult::failed($this->code(), 'inner_failure');
}
};
}
}