respect-validation/tests/feature/Validators/LengthTest.php
Henrique Moody 7c681fec66
Fix SPDX headers in all files
I ran the `bin/console spdx --fix` with different strategies for
different files. For most of the core classes, since they've been
drastically rebuilt, I've run it with the `git-blame` strategy, for for
the `src/Validators`, in which the API changed completely but the logic
remains the same, I use the `git-log` strategy.
2026-02-03 15:23:23 +01:00

66 lines
3 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>
*/
declare(strict_types=1);
test('Default', catchAll(
fn() => v::length(v::equals(3))->assert('tulip'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The length of "tulip" must be equal to 3')
->and($fullMessage)->toBe('- The length of "tulip" must be equal to 3')
->and($messages)->toBe(['lengthEquals' => 'The length of "tulip" must be equal to 3']),
));
test('Inverted wrapped', catchAll(
fn() => v::length(v::not(v::equals(4)))->assert('rose'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The length of "rose" must not be equal to 4')
->and($fullMessage)->toBe('- The length of "rose" must not be equal to 4')
->and($messages)->toBe(['lengthNotEquals' => 'The length of "rose" must not be equal to 4']),
));
test('Inverted wrapper', catchAll(
fn() => v::not(v::length(v::equals(4)))->assert('fern'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The length of "fern" must not be equal to 4')
->and($fullMessage)->toBe('- The length of "fern" must not be equal to 4')
->and($messages)->toBe(['notLengthEquals' => 'The length of "fern" must not be equal to 4']),
));
test('With template', catchAll(
fn() => v::length(v::equals(3))->assert('azalea', 'This is a template'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('This is a template')
->and($fullMessage)->toBe('- This is a template')
->and($messages)->toBe(['lengthEquals' => 'This is a template']),
));
test('With wrapper name', catchAll(
fn() => v::named('Cactus', v::length(v::equals(3)))->assert('peyote'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The length of Cactus must be equal to 3')
->and($fullMessage)->toBe('- The length of Cactus must be equal to 3')
->and($messages)->toBe(['lengthEquals' => 'The length of Cactus must be equal to 3']),
));
test('Chained wrapped rule', catchAll(
fn() => v::length(v::between(5, 7)->odd())->assert([]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('The length of `[]` must be between 5 and 7')
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
- `[]` must pass all the rules
- The length of `[]` must be between 5 and 7
- The length of `[]` must be an odd number
FULL_MESSAGE)
->and($messages)->toBe([
'__root__' => '`[]` must pass all the rules',
'lengthBetween' => 'The length of `[]` must be between 5 and 7',
'lengthOdd' => 'The length of `[]` must be an odd number',
]),
));