mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 23:05:45 +01:00
Currently, the templates that a user provides when running `assert()` can significantly impact how the message is displayed. Because of this, the formatters become complex as they all need to handle similar conditions to format results. This commit changes this behaviour, letting only the `InterpolationRenderer` handle the templates. This makes the code simpler and allows people to use the `InterpolationRenderer` directly, without needing to figure out how to handle templates. Thinking about it further, I believe handling templates is a concern for the `Renderer` anyway, and this will open the way to other improvements using the renderer. I also removed the exception that is thrown when the template is not a string, because I think that after validation has failed, we should not throw any other exceptions, as that could cause unexpected errors for users.
81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Message\Formatter;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Respect\Validation\Message\StandardFormatter\ResultCreator;
|
|
use Respect\Validation\Result;
|
|
use Respect\Validation\Test\Builders\ResultBuilder;
|
|
use Respect\Validation\Test\Message\TestingMessageRenderer;
|
|
use Respect\Validation\Test\TestCase;
|
|
|
|
#[CoversClass(NestedArrayFormatter::class)]
|
|
final class NestedArrayFormatterTest extends TestCase
|
|
{
|
|
use ResultCreator;
|
|
|
|
/**
|
|
* @param array<string, mixed> $expected
|
|
* @param array<string, mixed> $templates
|
|
*/
|
|
#[Test]
|
|
#[DataProvider('provideForArray')]
|
|
public function itShouldFormatArrayMessage(Result $result, array $expected, array $templates = []): void
|
|
{
|
|
$renderer = new TestingMessageRenderer();
|
|
$formatter = new NestedArrayFormatter();
|
|
|
|
self::assertSame($expected, $formatter->format($result, $renderer, $templates));
|
|
}
|
|
|
|
/** @return array<string, array{0: Result, 1: array<string, mixed>, 2?: array<string, mixed>}> */
|
|
public static function provideForArray(): array
|
|
{
|
|
return [
|
|
'without children' => [
|
|
(new ResultBuilder())->id('only')->template('__parent_original__')->build(),
|
|
['only' => '__parent_original__'],
|
|
],
|
|
'with single-level children' => [
|
|
self::singleLevelChildrenMessage(),
|
|
[
|
|
'__root__' => '__parent_original__',
|
|
'1st' => '__1st_original__',
|
|
'2nd' => '__2nd_original__',
|
|
'3rd' => '__3rd_original__',
|
|
],
|
|
],
|
|
'with single-nested child' => [
|
|
self::multiLevelChildrenWithSingleNestedChildMessage(),
|
|
[
|
|
'__root__' => '__parent_original__',
|
|
'1st' => '__1st_original__',
|
|
'2nd' => '__2nd_1st_original__',
|
|
'3rd' => '__3rd_original__',
|
|
],
|
|
],
|
|
'with multi-nested children' => [
|
|
self::multiLevelChildrenWithMultiNestedChildrenMessage(),
|
|
[
|
|
'__root__' => '__parent_original__',
|
|
'1st' => '__1st_original__',
|
|
'2nd' => [
|
|
'__root__' => '__2nd_original__',
|
|
'2nd_1st' => '__2nd_1st_original__',
|
|
'2nd_2nd' => '__2nd_2nd_original__',
|
|
],
|
|
'3rd' => '__3rd_original__',
|
|
],
|
|
],
|
|
];
|
|
}
|
|
}
|