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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Definition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\NodeInterface;
class NormalizationTest extends TestCase
{
/**
* @dataProvider getEncoderTests
*/
public function testNormalizeEncoders($denormalized)
{
$tb = new TreeBuilder('root_name', 'array');
$tree = $tb
->getRootNode()
->fixXmlConfig('encoder')
->children()
->node('encoders', 'array')
->useAttributeAsKey('class')
->prototype('array')
->beforeNormalization()->ifString()->then(function ($v) { return ['algorithm' => $v]; })->end()
->children()
->node('algorithm', 'scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$normalized = [
'encoders' => [
'foo' => ['algorithm' => 'plaintext'],
],
];
$this->assertNormalized($tree, $denormalized, $normalized);
}
public function getEncoderTests()
{
$configs = [];
// XML
$configs[] = [
'encoder' => [
['class' => 'foo', 'algorithm' => 'plaintext'],
],
];
// XML when only one element of this type
$configs[] = [
'encoder' => ['class' => 'foo', 'algorithm' => 'plaintext'],
];
// YAML/PHP
$configs[] = [
'encoders' => [
['class' => 'foo', 'algorithm' => 'plaintext'],
],
];
// YAML/PHP
$configs[] = [
'encoders' => [
'foo' => 'plaintext',
],
];
// YAML/PHP
$configs[] = [
'encoders' => [
'foo' => ['algorithm' => 'plaintext'],
],
];
return array_map(function ($v) {
return [$v];
}, $configs);
}
/**
* @dataProvider getAnonymousKeysTests
*/
public function testAnonymousKeysArray($denormalized)
{
$tb = new TreeBuilder('root', 'array');
$tree = $tb
->getRootNode()
->children()
->node('logout', 'array')
->fixXmlConfig('handler')
->children()
->node('handlers', 'array')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$normalized = ['logout' => ['handlers' => ['a', 'b', 'c']]];
$this->assertNormalized($tree, $denormalized, $normalized);
}
public function getAnonymousKeysTests()
{
$configs = [];
$configs[] = [
'logout' => [
'handlers' => ['a', 'b', 'c'],
],
];
$configs[] = [
'logout' => [
'handler' => ['a', 'b', 'c'],
],
];
return array_map(function ($v) { return [$v]; }, $configs);
}
/**
* @dataProvider getNumericKeysTests
*/
public function testNumericKeysAsAttributes($denormalized)
{
$normalized = [
'thing' => [42 => ['foo', 'bar'], 1337 => ['baz', 'qux']],
];
$this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
}
public function getNumericKeysTests()
{
$configs = [];
$configs[] = [
'thing' => [
42 => ['foo', 'bar'], 1337 => ['baz', 'qux'],
],
];
$configs[] = [
'thing' => [
['foo', 'bar', 'id' => 42], ['baz', 'qux', 'id' => 1337],
],
];
return array_map(function ($v) { return [$v]; }, $configs);
}
public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
{
$this->expectException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException');
$this->expectExceptionMessage('The attribute "id" must be set for path "root.thing".');
$denormalized = [
'thing' => [
['foo', 'bar'], ['baz', 'qux'],
],
];
$this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, []);
}
public function testAssociativeArrayPreserveKeys()
{
$tb = new TreeBuilder('root', 'array');
$tree = $tb
->getRootNode()
->prototype('array')
->children()
->node('foo', 'scalar')->end()
->end()
->end()
->end()
->buildTree()
;
$data = ['first' => ['foo' => 'bar']];
$this->assertNormalized($tree, $data, $data);
}
public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
{
self::assertSame($normalized, $tree->normalize($denormalized));
}
private function getNumericKeysTestTree()
{
$tb = new TreeBuilder('root', 'array');
$tree = $tb
->getRootNode()
->children()
->node('thing', 'array')
->useAttributeAsKey('id')
->prototype('array')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->buildTree()
;
return $tree;
}
}