mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 14:55:44 +01:00
This commit introduces a mechanism for validators to return early once the validation outcome is determined, rather than evaluating all child validators. The ShortCircuit validator evaluates validators sequentially and stops at the first failure, similar to how PHP's && operator works. This is useful when later validators depend on earlier ones passing, or when you want only the first error message. The ShortCircuitCapable interface allows composite validators (AllOf, AnyOf, OneOf, NoneOf, Each, All) to implement their own short-circuit logic. Why "ShortCircuit" instead of "FailFast": The name "FailFast" was initially considered but proved misleading. While AllOf stops on failure (fail fast), AnyOf stops on success (succeed fast), and OneOf stops on the second success. The common behavior is not about failing quickly, but about returning as soon as the outcome is determined—which is exactly what short-circuit evaluation means. This terminology is familiar to developers from boolean operators (&& and ||), making the behavior immediately understandable. Co-authored-by: Alexandre Gomes Gaigalas <alganet@gmail.com> Assisted-by: Claude Code (Opus 4.5)
137 lines
4 KiB
PHP
137 lines
4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
* SPDX-FileContributor: William Espindola <oi@williamespindola.com.br>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use ArrayIterator;
|
|
use ArrayObject;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Respect\Validation\Test\RuleTestCase;
|
|
use Respect\Validation\Test\Validators\Stub;
|
|
use stdClass;
|
|
|
|
#[Group('validator')]
|
|
#[CoversClass(Each::class)]
|
|
final class EachTest extends RuleTestCase
|
|
{
|
|
/** @return iterable<array{Each|Not, mixed}> */
|
|
public static function providerForValidInput(): iterable
|
|
{
|
|
return [
|
|
[new Each(Stub::daze()), []],
|
|
[new Not(new Each(Stub::daze())), []],
|
|
[new Each(Stub::pass(5)), [1, 2, 3, 4, 5]],
|
|
[new Each(Stub::pass(5)), new ArrayObject([1, 2, 3, 4, 5])],
|
|
];
|
|
}
|
|
|
|
/** @return iterable<array{Each, mixed}> */
|
|
public static function providerForInvalidInput(): iterable
|
|
{
|
|
return [
|
|
[new Each(Stub::daze()), new stdClass()],
|
|
[new Each(Stub::daze()), 123],
|
|
[new Each(Stub::daze()), ''],
|
|
[new Each(Stub::daze()), null],
|
|
[new Each(Stub::daze()), false],
|
|
[new Each(Stub::fail(5)), ['', 2, 3, 4, 5]],
|
|
[new Each(Stub::fail(5)), ['a', 2, 3, 4, 5]],
|
|
[new Each(Stub::fail(5)), new ArrayObject([1, 2, 3, 4, 5])],
|
|
[new Each(Stub::fail(5)), (object) ['foo' => true]],
|
|
];
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitOnFirstFailure(): void
|
|
{
|
|
$stub = new Stub(true, false, true, true, true);
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit([1, 2, 3, 4, 5]);
|
|
|
|
TestCase::assertFalse($result->hasPassed);
|
|
TestCase::assertCount(2, $stub->inputs);
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitPassWhenAllItemsPass(): void
|
|
{
|
|
$stub = Stub::pass(5);
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit([1, 2, 3, 4, 5]);
|
|
|
|
TestCase::assertTrue($result->hasPassed);
|
|
TestCase::assertCount(5, $stub->inputs);
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitFailForNonIterableInput(): void
|
|
{
|
|
$stub = Stub::daze();
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit('not an array');
|
|
|
|
TestCase::assertFalse($result->hasPassed);
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitReturnPassedForEmptyArray(): void
|
|
{
|
|
$stub = Stub::daze();
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit([]);
|
|
|
|
TestCase::assertTrue($result->hasPassed);
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitWorkWithIterator(): void
|
|
{
|
|
$stub = new Stub(true, false, true, true, true);
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit(new ArrayIterator([1, 2, 3, 4, 5]));
|
|
|
|
TestCase::assertFalse($result->hasPassed);
|
|
TestCase::assertCount(2, $stub->inputs);
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitIncludePathOnFailure(): void
|
|
{
|
|
$stub = new Stub(true, false, true, true, true);
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit([1, 2, 3, 4, 5]);
|
|
|
|
TestCase::assertFalse($result->hasPassed);
|
|
TestCase::assertSame(1, $result->path?->value);
|
|
}
|
|
|
|
#[Test]
|
|
public function shouldShortCircuitWorkWithNamedKeys(): void
|
|
{
|
|
$stub = new Stub(true, false, true);
|
|
$validator = new Each($stub);
|
|
|
|
$result = $validator->evaluateShortCircuit(['a' => 1, 'b' => 2, 'c' => 3]);
|
|
|
|
TestCase::assertFalse($result->hasPassed);
|
|
TestCase::assertSame('b', $result->path?->value);
|
|
}
|
|
}
|