-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathFormErrorNormalizerTest.php
156 lines (136 loc) · 4.66 KB
/
FormErrorNormalizerTest.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Normalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Serializer\Normalizer\FormErrorNormalizer;
class FormErrorNormalizerTest extends TestCase
{
private FormErrorNormalizer $normalizer;
private FormInterface $form;
protected function setUp(): void
{
$this->normalizer = new FormErrorNormalizer();
$this->form = $this->createMock(FormInterface::class);
$this->form->method('isSubmitted')->willReturn(true);
$this->form->method('all')->willReturn([]);
$this->form->method('getErrors')
->willReturn(new FormErrorIterator($this->form, [
new FormError('a', 'b', ['c', 'd'], 5, 'f'),
new FormError(1, 2, [3, 4], 5, 6),
])
);
}
public function testSupportsNormalizationWithWrongClass()
{
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
public function testSupportsNormalizationWithNotSubmittedForm()
{
$form = $this->createMock(FormInterface::class);
$this->assertFalse($this->normalizer->supportsNormalization($form));
}
public function testSupportsNormalizationWithValidForm()
{
$this->assertTrue($this->normalizer->supportsNormalization($this->form));
}
public function testNormalize()
{
$expected = [
'code' => null,
'title' => 'Validation Failed',
'type' => 'https://symfony.com/errors/form',
'errors' => [
[
'message' => 'a',
'cause' => 'f',
],
[
'message' => '1',
'cause' => 6,
],
],
];
$this->assertEquals($expected, $this->normalizer->normalize($this->form));
}
public function testNormalizeWithChildren()
{
$exptected = [
'code' => null,
'title' => 'Validation Failed',
'type' => 'https://symfony.com/errors/form',
'errors' => [
[
'message' => 'a',
'cause' => null,
],
],
'children' => [
'form1' => [
'errors' => [
[
'message' => 'b',
'cause' => null,
],
],
],
'form2' => [
'errors' => [
[
'message' => 'c',
'cause' => null,
],
],
'children' => [
'form3' => [
'errors' => [
[
'message' => 'd',
'cause' => null,
],
],
],
],
],
],
];
$form = clone $form1 = clone $form2 = clone $form3 = $this->createMock(FormInterface::class);
$form1->method('getErrors')
->willReturn(new FormErrorIterator($form1, [
new FormError('b'),
])
);
$form1->method('getName')->willReturn('form1');
$form2->method('getErrors')
->willReturn(new FormErrorIterator($form1, [
new FormError('c'),
])
);
$form2->method('getName')->willReturn('form2');
$form3->method('getErrors')
->willReturn(new FormErrorIterator($form1, [
new FormError('d'),
])
);
$form3->method('getName')->willReturn('form3');
$form2->method('all')->willReturn([$form3]);
$form = $this->createMock(FormInterface::class);
$form->method('isSubmitted')->willReturn(true);
$form->method('all')->willReturn([$form1, $form2]);
$form->method('getErrors')
->willReturn(new FormErrorIterator($form, [
new FormError('a'),
])
);
$this->assertEquals($exptected, $this->normalizer->normalize($form));
}
}