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\Builder;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16 use Symfony\Component\Config\Definition\Processor;
17 use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
18 use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
20 class ArrayNodeDefinitionTest extends TestCase
22 public function testAppendingSomeNode()
24 $parent = new ArrayNodeDefinition('root');
25 $child = new ScalarNodeDefinition('child');
29 ->scalarNode('foo')->end()
30 ->scalarNode('bar')->end()
34 $this->assertCount(3, $this->getField($parent, 'children'));
35 $this->assertTrue(in_array($child, $this->getField($parent, 'children')));
39 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
40 * @dataProvider providePrototypeNodeSpecificCalls
42 public function testPrototypeNodeSpecificOption($method, $args)
44 $node = new ArrayNodeDefinition('root');
46 call_user_func_array(array($node, $method), $args);
51 public function providePrototypeNodeSpecificCalls()
54 array('defaultValue', array(array())),
55 array('addDefaultChildrenIfNoneSet', array()),
56 array('requiresAtLeastOneElement', array()),
57 array('useAttributeAsKey', array('foo')),
62 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
64 public function testConcreteNodeSpecificOption()
66 $node = new ArrayNodeDefinition('root');
68 ->addDefaultsIfNotSet()
75 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
77 public function testPrototypeNodesCantHaveADefaultValueWhenUsingDefaultChildren()
79 $node = new ArrayNodeDefinition('root');
81 ->defaultValue(array())
82 ->addDefaultChildrenIfNoneSet('foo')
88 public function testPrototypedArrayNodeDefaultWhenUsingDefaultChildren()
90 $node = new ArrayNodeDefinition('root');
92 ->addDefaultChildrenIfNoneSet()
95 $tree = $node->getNode();
96 $this->assertEquals(array(array()), $tree->getDefaultValue());
100 * @dataProvider providePrototypedArrayNodeDefaults
102 public function testPrototypedArrayNodeDefault($args, $shouldThrowWhenUsingAttrAsKey, $shouldThrowWhenNotUsingAttrAsKey, $defaults)
104 $node = new ArrayNodeDefinition('root');
106 ->addDefaultChildrenIfNoneSet($args)
111 $tree = $node->getNode();
112 $this->assertFalse($shouldThrowWhenNotUsingAttrAsKey);
113 $this->assertEquals($defaults, $tree->getDefaultValue());
114 } catch (InvalidDefinitionException $e) {
115 $this->assertTrue($shouldThrowWhenNotUsingAttrAsKey);
118 $node = new ArrayNodeDefinition('root');
120 ->useAttributeAsKey('attr')
121 ->addDefaultChildrenIfNoneSet($args)
126 $tree = $node->getNode();
127 $this->assertFalse($shouldThrowWhenUsingAttrAsKey);
128 $this->assertEquals($defaults, $tree->getDefaultValue());
129 } catch (InvalidDefinitionException $e) {
130 $this->assertTrue($shouldThrowWhenUsingAttrAsKey);
134 public function providePrototypedArrayNodeDefaults()
137 array(null, true, false, array(array())),
138 array(2, true, false, array(array(), array())),
139 array('2', false, true, array('2' => array())),
140 array('foo', false, true, array('foo' => array())),
141 array(array('foo'), false, true, array('foo' => array())),
142 array(array('foo', 'bar'), false, true, array('foo' => array(), 'bar' => array())),
146 public function testNestedPrototypedArrayNodes()
148 $nodeDefinition = new ArrayNodeDefinition('root');
150 ->addDefaultChildrenIfNoneSet()
154 $node = $nodeDefinition->getNode();
156 $this->assertInstanceOf('Symfony\Component\Config\Definition\PrototypedArrayNode', $node);
157 $this->assertInstanceOf('Symfony\Component\Config\Definition\PrototypedArrayNode', $node->getPrototype());
160 public function testEnabledNodeDefaults()
162 $node = new ArrayNodeDefinition('root');
166 ->scalarNode('foo')->defaultValue('bar')->end()
169 $this->assertEquals(array('enabled' => false, 'foo' => 'bar'), $node->getNode()->getDefaultValue());
173 * @dataProvider getEnableableNodeFixtures
175 public function testTrueEnableEnabledNode($expected, $config, $message)
177 $processor = new Processor();
178 $node = new ArrayNodeDefinition('root');
182 ->scalarNode('foo')->defaultValue('bar')->end()
187 $processor->process($node->getNode(), $config),
192 public function testCanBeDisabled()
194 $node = new ArrayNodeDefinition('root');
195 $node->canBeDisabled();
197 $this->assertTrue($this->getField($node, 'addDefaults'));
198 $this->assertEquals(array('enabled' => false), $this->getField($node, 'falseEquivalent'));
199 $this->assertEquals(array('enabled' => true), $this->getField($node, 'trueEquivalent'));
200 $this->assertEquals(array('enabled' => true), $this->getField($node, 'nullEquivalent'));
202 $nodeChildren = $this->getField($node, 'children');
203 $this->assertArrayHasKey('enabled', $nodeChildren);
205 $enabledNode = $nodeChildren['enabled'];
206 $this->assertTrue($this->getField($enabledNode, 'default'));
207 $this->assertTrue($this->getField($enabledNode, 'defaultValue'));
210 public function testIgnoreExtraKeys()
212 $node = new ArrayNodeDefinition('root');
214 $this->assertFalse($this->getField($node, 'ignoreExtraKeys'));
216 $result = $node->ignoreExtraKeys();
218 $this->assertEquals($node, $result);
219 $this->assertTrue($this->getField($node, 'ignoreExtraKeys'));
222 public function testNormalizeKeys()
224 $node = new ArrayNodeDefinition('root');
226 $this->assertTrue($this->getField($node, 'normalizeKeys'));
228 $result = $node->normalizeKeys(false);
230 $this->assertEquals($node, $result);
231 $this->assertFalse($this->getField($node, 'normalizeKeys'));
234 public function getEnableableNodeFixtures()
237 array(array('enabled' => true, 'foo' => 'bar'), array(true), 'true enables an enableable node'),
238 array(array('enabled' => true, 'foo' => 'bar'), array(null), 'null enables an enableable node'),
239 array(array('enabled' => true, 'foo' => 'bar'), array(array('enabled' => true)), 'An enableable node can be enabled'),
240 array(array('enabled' => true, 'foo' => 'baz'), array(array('foo' => 'baz')), 'any configuration enables an enableable node'),
241 array(array('enabled' => false, 'foo' => 'baz'), array(array('foo' => 'baz', 'enabled' => false)), 'An enableable node can be disabled'),
242 array(array('enabled' => false, 'foo' => 'bar'), array(false), 'false disables an enableable node'),
246 protected function getField($object, $field)
248 $reflection = new \ReflectionProperty($object, $field);
249 $reflection->setAccessible(true);
251 return $reflection->getValue($object);