respect-validation/tests/feature/HandlingNamesTest.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

30 lines
1.2 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
$input = ['email' => 'not an email'];
test('Results with a defined name cannot be overwritten by another name', catchMessage(
fn() => v::key('email', v::email()->setName('Email'))->setName('Foo')->assert($input),
fn(string $message) => expect($message)->toBe('Email must be a valid email address'),
));
test('Defining a name to a result with a path will add the path to the name', catchMessage(
fn() => v::key('email', v::email())->setName('Email')->assert($input),
fn(string $message) => expect($message)->toBe('`.email` (<- Email) must be a valid email address'),
));
test('Results with a defined name will not be affected by a path', catchMessage(
fn() => v::key('email', v::email()->setName('Email'))->assert($input),
fn(string $message) => expect($message)->toBe('Email must be a valid email address'),
));
test('Not defining a name to a result with a path will display the path', catchMessage(
fn() => v::key('email', v::email())->assert($input),
fn(string $message) => expect($message)->toBe('`.email` must be a valid email address'),
));