mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
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.
91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-FileContributor: Emmerson Siqueira <emmersonsiqueira@gmail.com>
|
|
* SPDX-FileContributor: Gabriel Caruso <carusogabriel34@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
* SPDX-FileContributor: Nick Lombard <github@jigsoft.co.za>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Validators;
|
|
|
|
use DateTimeImmutable;
|
|
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(Min::class)]
|
|
final class MinTest extends TestCase
|
|
{
|
|
#[Test]
|
|
#[DataProvider('providerForNonIterableTypes')]
|
|
public function itShouldInvalidateNonIterableValues(mixed $input): void
|
|
{
|
|
$validator = new Min(Stub::daze());
|
|
|
|
self::assertInvalidInput($validator, $input);
|
|
}
|
|
|
|
/** @param iterable<mixed> $input */
|
|
#[Test]
|
|
#[DataProvider('providerForEmptyIterableValues')]
|
|
public function itShouldValidateEmptyIterableValuesAndNoteTheIndeterminate(iterable $input): void
|
|
{
|
|
$validator = new Min(Stub::daze());
|
|
|
|
$this->assertTrue($validator->evaluate($input)->isIndeterminate);
|
|
}
|
|
|
|
/** @param iterable<mixed> $input */
|
|
#[Test]
|
|
#[DataProvider('providerForNonEmptyIterableValues')]
|
|
public function itShouldValidateNonEmptyIterableValuesWhenWrappedRulePasses(iterable $input): void
|
|
{
|
|
$validator = new Min(Stub::pass(1));
|
|
|
|
self::assertValidInput($validator, $input);
|
|
}
|
|
|
|
/** @param iterable<mixed> $input */
|
|
#[Test]
|
|
#[DataProvider('providerForMinValues')]
|
|
public function itShouldValidateWithTheMinimumValue(iterable $input, mixed $min): void
|
|
{
|
|
$wrapped = Stub::pass(1);
|
|
|
|
$validator = new Min($wrapped);
|
|
$validator->evaluate($input);
|
|
|
|
self::assertSame($min, $wrapped->inputs[0]);
|
|
}
|
|
|
|
/** @return array<string, array{iterable<mixed>, mixed}> */
|
|
public static function providerForMinValues(): array
|
|
{
|
|
$yesterday = new DateTimeImmutable('yesterday');
|
|
$today = new DateTimeImmutable('today');
|
|
$tomorrow = new DateTimeImmutable('tomorrow');
|
|
|
|
return [
|
|
'3 DateTime objects' => [[$yesterday, $today, $tomorrow], $yesterday],
|
|
'2 DateTime objects' => [[$today, $tomorrow], $today],
|
|
'1 DateTime objects' => [[$tomorrow], $tomorrow],
|
|
'3 integers' => [[1, 2, 3], 1],
|
|
'2 integers' => [[2, 3], 2],
|
|
'1 integer' => [[3], 3],
|
|
'1 integer with value `0`' => [[0], 0],
|
|
'3 characters' => [['a', 'b', 'c'], 'a'],
|
|
'2 characters' => [['b', 'c'], 'b'],
|
|
'1 character' => [['c'], 'c'],
|
|
];
|
|
}
|
|
}
|