respect-validation/tests/unit/Validators/AllTest.php
Alexandre Gomes Gaigalas bcc60ec035 Allow empty values in iterables for All, Each, Max, Min
Now empty values are again allowed in FilteredArray-style
validators.

To solve the issue with negation, a Result attribute was
added to signal indeciseveness (when a result cannot be
reliably inverted). On such cases, we consider that result
to be valid.

For example, `v::not(v::min(v::equals(10)))` says "The
lowest value of the iterable input should not be equal 10".

If the input is empty, we cannot decide whether its minimum
is equal to 10 or not, so the validator essentially becomes
a null-op.

Users that want to ensure these validators have a valid
decidable target must use it in combination with `Length`
or other similar validators to achieve the same result.
2026-01-30 21:27:16 +00:00

65 lines
2.3 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Validators;
use ArrayObject;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\Validators\Stub;
#[Group('validator')]
#[CoversClass(All::class)]
final class AllTest extends TestCase
{
/** @return iterable<string, array{Stub, mixed}> */
public static function providerForValidInput(): iterable
{
yield 'all pass with array' => [Stub::pass(3), [1, 2, 3]];
yield 'all pass with ArrayObject' => [Stub::pass(3), new ArrayObject([1, 2, 3])];
yield 'single element that passes' => [Stub::pass(1), ['value']];
yield 'all pass with array of strings' => [Stub::pass(5), ['a', 'b', 'c', 'd', 'e']];
yield 'empty array' => [Stub::daze(), []];
}
/** @return iterable<string, array{Stub, mixed}> */
public static function providerForInvalidInput(): iterable
{
yield 'some fail with array' => [Stub::fail(3), [1, 2, 3]];
yield 'all fail with array' => [Stub::fail(3), [1, 2, 3]];
yield 'mixed pass/fail with array' => [new Stub(true, false, true), [1, 2, 3]];
yield 'some fail with ArrayObject' => [Stub::fail(3), new ArrayObject([1, 2, 3])];
yield 'non-array input' => [Stub::daze(), 'not an array'];
yield 'string input' => [Stub::daze(), 'string'];
yield 'integer input' => [Stub::daze(), 123];
yield 'null input' => [Stub::daze(), null];
yield 'boolean input' => [Stub::daze(), true];
yield 'object input' => [Stub::daze(), (object) ['foo' => 'bar']];
}
#[Test]
#[DataProvider('providerForValidInput')]
public function shouldValidateValidInput(Stub $stub, mixed $input): void
{
$validator = new All($stub);
self::assertValidInput($validator, $input);
}
#[Test]
#[DataProvider('providerForInvalidInput')]
public function shouldValidateInvalidInput(Stub $stub, mixed $input): void
{
$validator = new All($stub);
self::assertInvalidInput($validator, $input);
}
}