-
Notifications
You must be signed in to change notification settings - Fork 2
/
SymfonyConfigTest.php
174 lines (162 loc) · 5.96 KB
/
SymfonyConfigTest.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Krak\Schema;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use function Krak\Schema\ProcessSchema\SymfonyConfig\configTree;
final class SymfonyConfigTest extends \PHPUnit\Framework\TestCase
{
/** @dataProvider provide_config_trees */
public function test_processing_of_schema_to_config_tree(TreeBuilder $expected, TreeBuilder $actual) {
$this->assertEquals($expected->buildTree(), $actual->buildTree());
}
public function provide_config_trees() {
yield 'empty tree' => [
(new TreeBuilder('root')),
configTree('root', struct([])),
];
yield 'tree with atomic values' => [
(new TreeBuilder('root'))->getRootNode()
->children()
->scalarNode('string')->end()
->integerNode('int')->end()
->booleanNode('bool')->end()
->floatNode('float')->end()
->scalarNode('mixed')->end()
->end()
->end(),
configTree('root', struct([
'string' => string(),
'int' => int(),
'bool' => bool(),
'float' => float(),
'mixed' => mixed(),
])),
];
yield 'tree with nested structs' => [
(new TreeBuilder('root'))->getRootNode()
->children()
->arrayNode('struct')
->children()
->arrayNode('nested')
->children()
->integerNode('int')->end()
->end()
->end()
->end()
->end()
->end()
->end(),
configTree('root', struct([
'struct' => struct([
'nested' => struct([
'int' => int(),
])
])
])),
];
yield 'tree with lists' => [
(new TreeBuilder('root'))->getRootNode()
->children()
->arrayNode('struct_of_list')
->children()
->arrayNode('strings')
->scalarPrototype()->end()
->end()
->arrayNode('ints')
->integerPrototype()->end()
->end()
->end()
->end()
->arrayNode('list_of_struct')
->arrayPrototype()
->children()
->scalarNode('string')->end()
->integerNode('int')->end()
->end()
->end()
->end()
->end()
->end(),
configTree('root', struct([
'struct_of_list' => struct([
'strings' => listOf(string()),
'ints' => listOf(int()),
]),
'list_of_struct' => listOf(struct([
'string' => string(),
'int' => int(),
])),
])),
];
yield 'tree with custom configuration' => [
(new TreeBuilder('root'))->getRootNode()
->ignoreExtraKeys()
->children()
->integerNode('int')->end()
->end()
->end(),
configTree('root', struct([
'int' => int(),
], [ 'configure' => function($def) { $def->ignoreExtraKeys(); } ]))
];
yield 'tree with allowExtraKeys' => [
(new TreeBuilder('root'))->getRootNode()
->ignoreExtraKeys(false)
->children()
->integerNode('int')->end()
->end()
->end(),
configTree('root', struct([
'int' => int(),
], ['allowExtraKeys' => true]))
];
yield 'tree with configured array nodes' => [
(new TreeBuilder('root'))->getRootNode()
->children()
->scalarNode('string')->end()
->integerNode('int')->end()
->end()
->end(),
configTree('root', struct([
'string' => string(),
], [
'configure' => function(ArrayNodeDefinition $def) {
$def->children()
->integerNode('int')->end()
->end();
}
])),
];
yield 'tree with dict of string -> int' => [
(new TreeBuilder('root'))->getRootNode()
->children()
->arrayNode('dict')
->useAttributeAsKey('key')
->scalarPrototype()->end()
->end()
->end()
->end(),
configTree('root', struct([
'dict' => dict(string()),
]))
];
yield 'tree with enums' => [
(new TreeBuilder('root'))->getRootNode()
->children()
->enumNode('direction')
->values(['north', 'south', 'east', 'west'])
->end()
->arrayNode('positions')
->enumPrototype()
->values(['up', 'down', 'left', 'right'])
->end()
->end()
->end()
->end(),
configTree('root', struct([
'direction' => enum(['north', 'south', 'east', 'west']),
'positions' => listOf(enum(['up', 'down', 'left', 'right']))
])),
];
}
}