Yaffs site version 1.1
[yaffs-website] / vendor / symfony / config / Tests / Definition / NormalizationTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Tests\Definition;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Definition\NodeInterface;
16 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
18 class NormalizationTest extends TestCase
19 {
20     /**
21      * @dataProvider getEncoderTests
22      */
23     public function testNormalizeEncoders($denormalized)
24     {
25         $tb = new TreeBuilder();
26         $tree = $tb
27             ->root('root_name', 'array')
28                 ->fixXmlConfig('encoder')
29                 ->children()
30                     ->node('encoders', 'array')
31                         ->useAttributeAsKey('class')
32                         ->prototype('array')
33                             ->beforeNormalization()->ifString()->then(function ($v) { return array('algorithm' => $v); })->end()
34                             ->children()
35                                 ->node('algorithm', 'scalar')->end()
36                             ->end()
37                         ->end()
38                     ->end()
39                 ->end()
40             ->end()
41             ->buildTree()
42         ;
43
44         $normalized = array(
45             'encoders' => array(
46                 'foo' => array('algorithm' => 'plaintext'),
47             ),
48         );
49
50         $this->assertNormalized($tree, $denormalized, $normalized);
51     }
52
53     public function getEncoderTests()
54     {
55         $configs = array();
56
57         // XML
58         $configs[] = array(
59             'encoder' => array(
60                 array('class' => 'foo', 'algorithm' => 'plaintext'),
61             ),
62         );
63
64         // XML when only one element of this type
65         $configs[] = array(
66             'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
67         );
68
69         // YAML/PHP
70         $configs[] = array(
71             'encoders' => array(
72                 array('class' => 'foo', 'algorithm' => 'plaintext'),
73             ),
74         );
75
76         // YAML/PHP
77         $configs[] = array(
78             'encoders' => array(
79                 'foo' => 'plaintext',
80             ),
81         );
82
83         // YAML/PHP
84         $configs[] = array(
85             'encoders' => array(
86                 'foo' => array('algorithm' => 'plaintext'),
87             ),
88         );
89
90         return array_map(function ($v) {
91             return array($v);
92         }, $configs);
93     }
94
95     /**
96      * @dataProvider getAnonymousKeysTests
97      */
98     public function testAnonymousKeysArray($denormalized)
99     {
100         $tb = new TreeBuilder();
101         $tree = $tb
102             ->root('root', 'array')
103                 ->children()
104                     ->node('logout', 'array')
105                         ->fixXmlConfig('handler')
106                         ->children()
107                             ->node('handlers', 'array')
108                                 ->prototype('scalar')->end()
109                             ->end()
110                         ->end()
111                     ->end()
112                 ->end()
113             ->end()
114             ->buildTree()
115         ;
116
117         $normalized = array('logout' => array('handlers' => array('a', 'b', 'c')));
118
119         $this->assertNormalized($tree, $denormalized, $normalized);
120     }
121
122     public function getAnonymousKeysTests()
123     {
124         $configs = array();
125
126         $configs[] = array(
127             'logout' => array(
128                 'handlers' => array('a', 'b', 'c'),
129             ),
130         );
131
132         $configs[] = array(
133             'logout' => array(
134                 'handler' => array('a', 'b', 'c'),
135             ),
136         );
137
138         return array_map(function ($v) { return array($v); }, $configs);
139     }
140
141     /**
142      * @dataProvider getNumericKeysTests
143      */
144     public function testNumericKeysAsAttributes($denormalized)
145     {
146         $normalized = array(
147             'thing' => array(42 => array('foo', 'bar'), 1337 => array('baz', 'qux')),
148         );
149
150         $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
151     }
152
153     public function getNumericKeysTests()
154     {
155         $configs = array();
156
157         $configs[] = array(
158             'thing' => array(
159                 42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
160             ),
161         );
162
163         $configs[] = array(
164             'thing' => array(
165                 array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
166             ),
167         );
168
169         return array_map(function ($v) { return array($v); }, $configs);
170     }
171
172     /**
173      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
174      * @expectedExceptionMessage The attribute "id" must be set for path "root.thing".
175      */
176     public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
177     {
178         $denormalized = array(
179             'thing' => array(
180                 array('foo', 'bar'), array('baz', 'qux'),
181             ),
182         );
183
184         $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, array());
185     }
186
187     public function testAssociativeArrayPreserveKeys()
188     {
189         $tb = new TreeBuilder();
190         $tree = $tb
191             ->root('root', 'array')
192                 ->prototype('array')
193                     ->children()
194                         ->node('foo', 'scalar')->end()
195                     ->end()
196                 ->end()
197             ->end()
198             ->buildTree()
199         ;
200
201         $data = array('first' => array('foo' => 'bar'));
202
203         $this->assertNormalized($tree, $data, $data);
204     }
205
206     public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
207     {
208         self::assertSame($normalized, $tree->normalize($denormalized));
209     }
210
211     private function getNumericKeysTestTree()
212     {
213         $tb = new TreeBuilder();
214         $tree = $tb
215             ->root('root', 'array')
216                 ->children()
217                     ->node('thing', 'array')
218                         ->useAttributeAsKey('id')
219                         ->prototype('array')
220                             ->prototype('scalar')->end()
221                         ->end()
222                     ->end()
223                 ->end()
224             ->end()
225             ->buildTree()
226         ;
227
228         return $tree;
229     }
230 }