respect-validation/tests/unit/Message/StandardFormatter/ArrayProvider.php
Henrique Moody fefe905e0b
Include "__root__" when getting message as an array
When converting an object into an array, we exclude the message root
message from it. Since we're using a convention to template those
messages as an array, we could also use the same convention to return
those messages.

While working on it, I noticed that the name "__self__" wasn't
reflecting what that really meant, so I renamed it "__root__" because it
better reflects the meaning of those messages/templates.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-25 22:38:19 +01:00

229 lines
8.6 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Message\StandardFormatter;
use Respect\Validation\Result;
use Respect\Validation\Test\Builders\ResultBuilder;
trait ArrayProvider
{
use ResultCreator;
/** @return array<string, array{0: Result, 1: array<string, mixed>, 2?: array<string, mixed>}> */
public static function provideForArray(): array
{
return [
'without children, without templates' => [
(new ResultBuilder())->id('only')->template('__parent_original__')->build(),
['only' => '__parent_original__'],
],
'without children, with templates' => [
(new ResultBuilder())->id('only')->build(),
['only' => 'Custom template'],
['only' => 'Custom template'],
],
'with single-level children, without templates' => [
self::singleLevelChildrenMessage(),
[
'__root__' => '__parent_original__',
'1st' => '__1st_original__',
'2nd' => '__2nd_original__',
'3rd' => '__3rd_original__',
],
],
'with single-level children, with templates' => [
self::singleLevelChildrenMessage(),
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
],
'with single-level children, with partial templates' => [
self::singleLevelChildrenMessage(),
[
'__root__' => '__parent_original__',
'1st' => '1st custom',
'2nd' => '__2nd_original__',
'3rd' => '3rd custom',
],
[
'1st' => '1st custom',
'3rd' => '3rd custom',
],
],
'with single-level children, with overwritten template' => [
self::singleLevelChildrenMessage(),
['parent' => 'Parent custom'],
['parent' => 'Parent custom'],
],
'with single-nested child, without templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'__root__' => '__parent_original__',
'1st' => '__1st_original__',
'2nd' => '__2nd_1st_original__',
'3rd' => '__3rd_original__',
],
],
'with single-nested child, with templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '2nd > 1st custom',
'3rd' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'2nd_1st' => '2nd > 1st custom',
],
'3rd' => '3rd custom',
],
],
'with single-nested child, with partial templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '__2nd_1st_original__',
'3rd' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'2nd_2nd' => '2nd > 2nd not shown',
],
'3rd' => '3rd custom',
],
],
'with single-nested child, with overwritten templates' => [
self::multiLevelChildrenWithSingleNestedChildMessage(),
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => '2nd custom',
'3rd' => '3rd custom',
],
],
'with multi-nested children, without templates' => [
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__',
],
],
'with multi-nested children, with templates' => [
self::multiLevelChildrenWithMultiNestedChildrenMessage(),
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'__root__' => '2nd custom',
'2nd_1st' => '2nd > 1st custom',
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'__root__' => '2nd custom',
'2nd_1st' => '2nd > 1st custom',
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
],
'with multi-nested children, with partial templates' => [
self::multiLevelChildrenWithMultiNestedChildrenMessage(),
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'__root__' => '__2nd_original__',
'2nd_1st' => '__2nd_1st_original__',
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'1st' => '1st custom',
'2nd' => [
'2nd_2nd' => '2nd > 2nd custom',
],
'3rd' => '3rd custom',
],
],
'with children with the same id, without templates' => [
self::singleLevelChildrenWithSameId(),
[
'__root__' => '__parent_original__',
'child.1' => '__1st_original__',
'child.2' => '__2nd_original__',
'child.3' => '__3rd_original__',
],
],
'with children with the same id, with templates' => [
self::singleLevelChildrenWithSameId(),
[
'__root__' => 'Parent custom',
'child.1' => '1st custom',
'child.2' => '2nd custom',
'child.3' => '3rd custom',
],
[
'__root__' => 'Parent custom',
'child.1' => '1st custom',
'child.2' => '2nd custom',
'child.3' => '3rd custom',
],
],
'with children with the same id, with partial templates' => [
self::singleLevelChildrenWithSameId(),
[
'__root__' => '__parent_original__',
'child.1' => '1st custom',
'child.2' => '2nd custom',
'child.3' => '__3rd_original__',
],
[
'child.1' => '1st custom',
'child.2' => '2nd custom',
],
],
];
}
}