4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Definition;
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;
19 class ArrayNodeTest extends TestCase
22 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
24 public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
26 $node = new ArrayNode('root');
27 $node->normalize(false);
31 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
32 * @expectedExceptionMessage Unrecognized option "foo" under "root"
34 public function testExceptionThrownOnUnrecognizedChild()
36 $node = new ArrayNode('root');
37 $node->normalize(array('foo' => 'bar'));
40 public function ignoreAndRemoveMatrixProvider()
42 $unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
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),
53 * @dataProvider ignoreAndRemoveMatrixProvider
55 public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
57 if ($expected instanceof \Exception) {
58 if (method_exists($this, 'expectException')) {
59 $this->expectException(get_class($expected));
60 $this->expectExceptionMessage($expected->getMessage());
62 $this->setExpectedException(get_class($expected), $expected->getMessage());
65 $node = new ArrayNode('root');
66 $node->setIgnoreExtraKeys($ignore, $remove);
67 $result = $node->normalize(array('foo' => 'bar'));
68 $this->assertSame($expected, $result, $message);
72 * @dataProvider getPreNormalizationTests
74 public function testPreNormalize($denormalized, $normalized)
76 $node = new ArrayNode('foo');
78 $r = new \ReflectionMethod($node, 'preNormalize');
79 $r->setAccessible(true);
81 $this->assertSame($normalized, $r->invoke($node, $denormalized));
84 public function getPreNormalizationTests()
88 array('foo-bar' => 'foo'),
89 array('foo_bar' => 'foo'),
92 array('foo-bar_moo' => 'foo'),
93 array('foo-bar_moo' => 'foo'),
96 array('anything-with-dash-and-no-underscore' => 'first', 'no_dash' => 'second'),
97 array('anything_with_dash_and_no_underscore' => 'first', 'no_dash' => 'second'),
100 array('foo-bar' => null, 'foo_bar' => 'foo'),
101 array('foo-bar' => null, 'foo_bar' => 'foo'),
107 * @dataProvider getZeroNamedNodeExamplesData
109 public function testNodeNameCanBeZero($denormalized, $normalized)
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);
123 $this->assertSame($normalized, $r->invoke($rootNode, $denormalized));
126 public function getZeroNamedNodeExamplesData()
132 'name' => 'something',
135 0 => 'this won\'t work too',
136 'new_key' => 'some other value',
138 'string_key' => 'just value',
142 'name' => 'something',
145 0 => 'this won\'t work too',
146 'new_key' => 'some other value',
148 'string_key' => 'just value',
155 * @dataProvider getPreNormalizedNormalizedOrderedData
157 public function testChildrenOrderIsMaintainedOnNormalizeValue($prenormalized, $normalized)
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);
167 $r = new \ReflectionMethod($node, 'normalizeValue');
168 $r->setAccessible(true);
170 $this->assertSame($normalized, $r->invoke($node, $prenormalized));
173 public function getPreNormalizedNormalizedOrderedData()
177 array('2' => 'two', '1' => 'one', '3' => 'three'),
178 array('2' => 'two', '1' => 'one', '3' => 'three'),
184 * @expectedException \InvalidArgumentException
185 * @expectedExceptionMessage Child nodes must be named.
187 public function testAddChildEmptyName()
189 $node = new ArrayNode('root');
191 $childNode = new ArrayNode('');
192 $node->addChild($childNode);
196 * @expectedException \InvalidArgumentException
197 * @expectedExceptionMessage A child node named "foo" already exists.
199 public function testAddChildNameAlreadyExists()
201 $node = new ArrayNode('root');
203 $childNode = new ArrayNode('foo');
204 $node->addChild($childNode);
206 $childNodeWithSameName = new ArrayNode('foo');
207 $node->addChild($childNodeWithSameName);
211 * @expectedException \RuntimeException
212 * @expectedExceptionMessage The node at path "foo" has no default value.
214 public function testGetDefaultValueWithoutDefaultValue()
216 $node = new ArrayNode('foo');
217 $node->getDefaultValue();