respect-validation/tests/unit/Rules/NullOrTest.php
Henrique Moody d1e0c8b0c1
Update NullOr to generate results with siblings
Since I updated the validation engine[1], it became possible to create
results with siblings. This commit changes the "NullOr", allowing it to
create a result with a sibling when possible. That will improve the
clarity of the error message.

I also updated the documentation, since it was still called "Nullable"

[1]: 238f2d506a
2024-12-04 18:06:12 +01:00

51 lines
1.7 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Respect\Validation\Test\Rules\Stub;
use Respect\Validation\Test\RuleTestCase;
use stdClass;
#[Group('rule')]
#[CoversClass(NullOr::class)]
final class NullOrTest extends RuleTestCase
{
/** @return iterable<string, array{NullOr, mixed}> */
public static function providerForValidInput(): iterable
{
yield 'null with passing rule' => [new NullOr(Stub::pass(1)), null];
yield 'null with failing rule' => [new NullOr(Stub::fail(1)), null];
yield 'not null with passing rule' => [new NullOr(Stub::pass(1)), 42];
}
/** @return iterable<array{NullOr, mixed}> */
public static function providerForInvalidInput(): iterable
{
yield [new NullOr(Stub::fail(1)), ''];
yield [new NullOr(Stub::fail(1)), ''];
yield [new NullOr(Stub::fail(1)), 1];
yield [new NullOr(Stub::fail(1)), []];
yield [new NullOr(Stub::fail(1)), ' '];
yield [new NullOr(Stub::fail(1)), 0];
yield [new NullOr(Stub::fail(1)), '0'];
yield [new NullOr(Stub::fail(1)), 0];
yield [new NullOr(Stub::fail(1)), '0.0'];
yield [new NullOr(Stub::fail(1)), false];
yield [new NullOr(Stub::fail(1)), ['']];
yield [new NullOr(Stub::fail(1)), [' ']];
yield [new NullOr(Stub::fail(1)), [0]];
yield [new NullOr(Stub::fail(1)), ['0']];
yield [new NullOr(Stub::fail(1)), [false]];
yield [new NullOr(Stub::fail(1)), [[''], [0]]];
yield [new NullOr(Stub::fail(1)), new stdClass()];
}
}