respect-validation/tests/feature/Rules/KeyTest.php
Henrique Moody 8332d28acc
Do not overwrite existing names
This commit addresses the skipped tests by modifying certain core
concepts in the library. The way names work has always been somewhat
confusing, even to me. I’ve established that existing names will never
be overwritten, and if a path is already defined, we will change the
name to also include the path.

I’m not very happy with how I’m solving this problem, because the
`FirstResultStringFormatter` is changing some behaviour in the results
that seems to be something that should always happen before. But, for
the moment, I will keep it as is.
2025-12-26 14:48:23 +01:00

112 lines
5.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
test('Missing key', catchAll(
fn() => v::key('foo', v::intType())->assert([]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be present')
->and($fullMessage)->toBe('- `.foo` must be present')
->and($messages)->toBe(['foo' => '`.foo` must be present']),
));
test('Default', catchAll(
fn() => v::key('foo', v::intType())->assert(['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be an integer')
->and($fullMessage)->toBe('- `.foo` must be an integer')
->and($messages)->toBe(['foo' => '`.foo` must be an integer']),
));
test('Inverted', catchAll(
fn() => v::not(v::key('foo', v::intType()))->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must not be an integer')
->and($fullMessage)->toBe('- `.foo` must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` must not be an integer']),
));
test('Double-inverted with missing key', catchAll(
fn() => v::not(v::not(v::key('foo', v::intType())))->assert([]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` must be present')
->and($fullMessage)->toBe('- `.foo` must be present')
->and($messages)->toBe(['foo' => '`.foo` must be present']),
));
test('With wrapped name, missing key', catchAll(
fn() => v::key('foo', v::intType()->setName('Wrapped'))->assert([]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must be present')
->and($fullMessage)->toBe('- Wrapped must be present')
->and($messages)->toBe(['foo' => 'Wrapped must be present']),
));
test('With wrapped name, default', catchAll(
fn() => v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper')->assert(['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('Wrapped must be an integer')
->and($fullMessage)->toBe('- Wrapped must be an integer')
->and($messages)->toBe(['foo' => 'Wrapped must be an integer']),
));
test('With wrapped name, inverted', catchAll(
fn() => v::not(v::key('foo', v::intType()->setName('Wrapped'))->setName('Wrapper'))->setName('Not')->assert(['foo' => 12]),
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(['foo' => 'Wrapped must not be an integer']),
));
test('With wrapper name, default', catchAll(
fn() => v::key('foo', v::intType())->setName('Wrapper')->assert(['foo' => 'string']),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Wrapper) must be an integer')
->and($fullMessage)->toBe('- `.foo` (<- Wrapper) must be an integer')
->and($messages)->toBe(['foo' => '`.foo` (<- Wrapper) must be an integer']),
));
test('With wrapper name, missing key', catchAll(
fn() => v::key('foo', v::intType())->setName('Wrapper')->assert([]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Wrapper) must be present')
->and($fullMessage)->toBe('- `.foo` (<- Wrapper) must be present')
->and($messages)->toBe(['foo' => '`.foo` (<- Wrapper) must be present']),
));
test('With wrapper name, inverted', catchAll(
fn() => v::not(v::key('foo', v::intType())->setName('Wrapper'))->setName('Not')->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Wrapper) must not be an integer')
->and($fullMessage)->toBe('- `.foo` (<- Wrapper) must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` (<- Wrapper) must not be an integer']),
));
test('With "Not" name, inverted', catchAll(
fn() => v::not(v::key('foo', v::intType()))->setName('Not')->assert(['foo' => 12]),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('`.foo` (<- Not) must not be an integer')
->and($fullMessage)->toBe('- `.foo` (<- Not) must not be an integer')
->and($messages)->toBe(['foo' => '`.foo` (<- Not) must not be an integer']),
));
test('With template, default', catchAll(
fn() => v::key('foo', v::intType())->assert(['foo' => 'string'], 'That key is off-key'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('That key is off-key')
->and($fullMessage)->toBe('- That key is off-key')
->and($messages)->toBe(['foo' => 'That key is off-key']),
));
test('With template, inverted', catchAll(
fn() => v::not(v::key('foo', v::intType()))->assert(['foo' => 12], 'No off-key key'),
fn(string $message, string $fullMessage, array $messages) => expect()
->and($message)->toBe('No off-key key')
->and($fullMessage)->toBe('- No off-key key')
->and($messages)->toBe(['foo' => 'No off-key key']),
));