Yaffs site version 1.1
[yaffs-website] / vendor / symfony / config / Tests / Definition / ArrayNodeTest.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\ArrayNode;
16 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17 use Symfony\Component\Config\Definition\ScalarNode;
18
19 class ArrayNodeTest extends TestCase
20 {
21     /**
22      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
23      */
24     public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
25     {
26         $node = new ArrayNode('root');
27         $node->normalize(false);
28     }
29
30     /**
31      * @expectedException        \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
32      * @expectedExceptionMessage Unrecognized option "foo" under "root"
33      */
34     public function testExceptionThrownOnUnrecognizedChild()
35     {
36         $node = new ArrayNode('root');
37         $node->normalize(array('foo' => 'bar'));
38     }
39
40     public function ignoreAndRemoveMatrixProvider()
41     {
42         $unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
43
44         return array(
45             array(true, true, array(), 'no exception is thrown for an unrecognized child if the ignoreExtraKeys option is set to true'),
46             array(true, false, array('foo' => 'bar'), 'extra keys are not removed when ignoreExtraKeys second option is set to false'),
47             array(false, true, $unrecognizedOptionException),
48             array(false, false, $unrecognizedOptionException),
49         );
50     }
51
52     /**
53      * @dataProvider ignoreAndRemoveMatrixProvider
54      */
55     public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
56     {
57         if ($expected instanceof \Exception) {
58             if (method_exists($this, 'expectException')) {
59                 $this->expectException(get_class($expected));
60                 $this->expectExceptionMessage($expected->getMessage());
61             } else {
62                 $this->setExpectedException(get_class($expected), $expected->getMessage());
63             }
64         }
65         $node = new ArrayNode('root');
66         $node->setIgnoreExtraKeys($ignore, $remove);
67         $result = $node->normalize(array('foo' => 'bar'));
68         $this->assertSame($expected, $result, $message);
69     }
70
71     /**
72      * @dataProvider getPreNormalizationTests
73      */
74     public function testPreNormalize($denormalized, $normalized)
75     {
76         $node = new ArrayNode('foo');
77
78         $r = new \ReflectionMethod($node, 'preNormalize');
79         $r->setAccessible(true);
80
81         $this->assertSame($normalized, $r->invoke($node, $denormalized));
82     }
83
84     public function getPreNormalizationTests()
85     {
86         return array(
87             array(
88                 array('foo-bar' => 'foo'),
89                 array('foo_bar' => 'foo'),
90             ),
91             array(
92                 array('foo-bar_moo' => 'foo'),
93                 array('foo-bar_moo' => 'foo'),
94             ),
95             array(
96                 array('anything-with-dash-and-no-underscore' => 'first', 'no_dash' => 'second'),
97                 array('anything_with_dash_and_no_underscore' => 'first', 'no_dash' => 'second'),
98             ),
99             array(
100                 array('foo-bar' => null, 'foo_bar' => 'foo'),
101                 array('foo-bar' => null, 'foo_bar' => 'foo'),
102             ),
103         );
104     }
105
106     /**
107      * @dataProvider getZeroNamedNodeExamplesData
108      */
109     public function testNodeNameCanBeZero($denormalized, $normalized)
110     {
111         $zeroNode = new ArrayNode(0);
112         $zeroNode->addChild(new ScalarNode('name'));
113         $fiveNode = new ArrayNode(5);
114         $fiveNode->addChild(new ScalarNode(0));
115         $fiveNode->addChild(new ScalarNode('new_key'));
116         $rootNode = new ArrayNode('root');
117         $rootNode->addChild($zeroNode);
118         $rootNode->addChild($fiveNode);
119         $rootNode->addChild(new ScalarNode('string_key'));
120         $r = new \ReflectionMethod($rootNode, 'normalizeValue');
121         $r->setAccessible(true);
122
123         $this->assertSame($normalized, $r->invoke($rootNode, $denormalized));
124     }
125
126     public function getZeroNamedNodeExamplesData()
127     {
128         return array(
129             array(
130                 array(
131                     0 => array(
132                         'name' => 'something',
133                     ),
134                     5 => array(
135                         0 => 'this won\'t work too',
136                         'new_key' => 'some other value',
137                     ),
138                     'string_key' => 'just value',
139                 ),
140                 array(
141                     0 => array(
142                         'name' => 'something',
143                     ),
144                     5 => array(
145                         0 => 'this won\'t work too',
146                         'new_key' => 'some other value',
147                     ),
148                     'string_key' => 'just value',
149                 ),
150             ),
151         );
152     }
153
154     /**
155      * @dataProvider getPreNormalizedNormalizedOrderedData
156      */
157     public function testChildrenOrderIsMaintainedOnNormalizeValue($prenormalized, $normalized)
158     {
159         $scalar1 = new ScalarNode('1');
160         $scalar2 = new ScalarNode('2');
161         $scalar3 = new ScalarNode('3');
162         $node = new ArrayNode('foo');
163         $node->addChild($scalar1);
164         $node->addChild($scalar3);
165         $node->addChild($scalar2);
166
167         $r = new \ReflectionMethod($node, 'normalizeValue');
168         $r->setAccessible(true);
169
170         $this->assertSame($normalized, $r->invoke($node, $prenormalized));
171     }
172
173     public function getPreNormalizedNormalizedOrderedData()
174     {
175         return array(
176             array(
177                 array('2' => 'two', '1' => 'one', '3' => 'three'),
178                 array('2' => 'two', '1' => 'one', '3' => 'three'),
179             ),
180         );
181     }
182
183     /**
184      * @expectedException \InvalidArgumentException
185      * @expectedExceptionMessage Child nodes must be named.
186      */
187     public function testAddChildEmptyName()
188     {
189         $node = new ArrayNode('root');
190
191         $childNode = new ArrayNode('');
192         $node->addChild($childNode);
193     }
194
195     /**
196      * @expectedException \InvalidArgumentException
197      * @expectedExceptionMessage A child node named "foo" already exists.
198      */
199     public function testAddChildNameAlreadyExists()
200     {
201         $node = new ArrayNode('root');
202
203         $childNode = new ArrayNode('foo');
204         $node->addChild($childNode);
205
206         $childNodeWithSameName = new ArrayNode('foo');
207         $node->addChild($childNodeWithSameName);
208     }
209
210     /**
211      * @expectedException \RuntimeException
212      * @expectedExceptionMessage The node at path "foo" has no default value.
213      */
214     public function testGetDefaultValueWithoutDefaultValue()
215     {
216         $node = new ArrayNode('foo');
217         $node->getDefaultValue();
218     }
219 }