respect-validation/tests/feature/Rules/LazyTest.php
2025-12-18 19:03:39 +01:00

88 lines
3.5 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
test('Default', catchAll(
fn() => v::lazy(
fn() => v::intType(),
)->assert(true),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`true` must be an integer')
->and($fullMessage)->toBe('- `true` must be an integer')
->and($messages)->toBe(['intType' => '`true` must be an integer']),
));
test('Inverted', catchAll(
fn() => v::not(v::lazy(
fn() => v::intType(),
))->assert(2),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('2 must not be an integer')
->and($fullMessage)->toBe('- 2 must not be an integer')
->and($messages)->toBe(['notIntType' => '2 must not be an integer']),
));
test('With created name, default', catchAll(
fn() => v::lazy(
fn() => v::intType()->setName('Created'),
)->setName('Wrapper')->assert(true),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Created must be an integer')
->and($fullMessage)->toBe('- Created must be an integer')
->and($messages)->toBe(['intType' => 'Created must be an integer']),
));
test('With wrapper name, default', catchAll(
fn() => v::lazy(
fn() => v::intType(),
)->setName('Wrapper')->assert(true),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapper must be an integer')
->and($fullMessage)->toBe('- Wrapper must be an integer')
->and($messages)->toBe(['intType' => 'Wrapper must be an integer']),
));
test('With created name, inverted', catchAll(
fn() => v::not(v::lazy(
fn() => v::intType()->setName('Created'),
)->setName('Wrapped'))->setName('Not')->assert(2),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Created must not be an integer')
->and($fullMessage)->toBe('- Created must not be an integer')
->and($messages)->toBe(['notIntType' => 'Created must not be an integer']),
));
test('With wrapper name, inverted', catchAll(
fn() => v::not(v::lazy(
fn() => v::intType(),
)->setName('Wrapped'))->setName('Not')->assert(2),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must not be an integer')
->and($fullMessage)->toBe('- Wrapped must not be an integer')
->and($messages)->toBe(['notIntType' => 'Wrapped must not be an integer']),
));
test('With not name, inverted', catchAll(
fn() => v::not(v::lazy(
fn() => v::intType(),
))->setName('Not')->assert(2),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Not must not be an integer')
->and($fullMessage)->toBe('- Not must not be an integer')
->and($messages)->toBe(['notIntType' => 'Not must not be an integer']),
));
test('With template, default', catchAll(
fn() => v::lazy(
fn() => v::intType(),
)->assert(true, 'Lazy lizards lounging like lords in the local lagoon'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Lazy lizards lounging like lords in the local lagoon')
->and($fullMessage)->toBe('- Lazy lizards lounging like lords in the local lagoon')
->and($messages)->toBe(['intType' => 'Lazy lizards lounging like lords in the local lagoon']),
));