-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathNestedArrayFormatterTest.php
More file actions
141 lines (133 loc) · 5.64 KB
/
NestedArrayFormatterTest.php
File metadata and controls
141 lines (133 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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\Path;
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<int|string, mixed>, 2?: array<int|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__',
],
],
'with string key collision' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path('foo'))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path('foo'))->build(),
)->build(),
[
'foo' => ['msg1', 'msg2'],
],
],
'with numeric key collision (list)' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path(0))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path(0))->build(),
)->build(),
[
'__root__' => 'root_msg',
0 => 'msg1',
1 => 'msg2',
],
],
'with mixed keys replacement' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path('foo'))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path(0))->build(),
)->build(),
[
'__root__' => 'root_msg',
'foo' => 'msg1',
'c2' => 'msg2',
],
],
'with mixed keys and ID collision' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path('foo'))->build(),
(new ResultBuilder())->id('sameId')->template('msg2')->withPath(new Path(0))->build(),
(new ResultBuilder())->id('sameId')->template('msg3')->withPath(new Path(1))->build(),
)->build(),
[
'__root__' => 'root_msg',
'foo' => 'msg1',
'sameId' => ['msg2', 'msg3'],
],
],
'with pure numeric keys' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path(10))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path(20))->build(),
)->build(),
[
'__root__' => 'root_msg',
0 => 'msg1',
1 => 'msg2',
],
],
];
}
}