Yaffs site version 1.1
[yaffs-website] / vendor / symfony / config / Tests / Definition / Builder / ArrayNodeDefinitionTest.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\Builder;
13
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;
19
20 class ArrayNodeDefinitionTest extends TestCase
21 {
22     public function testAppendingSomeNode()
23     {
24         $parent = new ArrayNodeDefinition('root');
25         $child = new ScalarNodeDefinition('child');
26
27         $parent
28             ->children()
29                 ->scalarNode('foo')->end()
30                 ->scalarNode('bar')->end()
31             ->end()
32             ->append($child);
33
34         $this->assertCount(3, $this->getField($parent, 'children'));
35         $this->assertTrue(in_array($child, $this->getField($parent, 'children')));
36     }
37
38     /**
39      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
40      * @dataProvider providePrototypeNodeSpecificCalls
41      */
42     public function testPrototypeNodeSpecificOption($method, $args)
43     {
44         $node = new ArrayNodeDefinition('root');
45
46         call_user_func_array(array($node, $method), $args);
47
48         $node->getNode();
49     }
50
51     public function providePrototypeNodeSpecificCalls()
52     {
53         return array(
54             array('defaultValue', array(array())),
55             array('addDefaultChildrenIfNoneSet', array()),
56             array('requiresAtLeastOneElement', array()),
57             array('useAttributeAsKey', array('foo')),
58         );
59     }
60
61     /**
62      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
63      */
64     public function testConcreteNodeSpecificOption()
65     {
66         $node = new ArrayNodeDefinition('root');
67         $node
68             ->addDefaultsIfNotSet()
69             ->prototype('array')
70         ;
71         $node->getNode();
72     }
73
74     /**
75      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
76      */
77     public function testPrototypeNodesCantHaveADefaultValueWhenUsingDefaultChildren()
78     {
79         $node = new ArrayNodeDefinition('root');
80         $node
81             ->defaultValue(array())
82             ->addDefaultChildrenIfNoneSet('foo')
83             ->prototype('array')
84         ;
85         $node->getNode();
86     }
87
88     public function testPrototypedArrayNodeDefaultWhenUsingDefaultChildren()
89     {
90         $node = new ArrayNodeDefinition('root');
91         $node
92             ->addDefaultChildrenIfNoneSet()
93             ->prototype('array')
94         ;
95         $tree = $node->getNode();
96         $this->assertEquals(array(array()), $tree->getDefaultValue());
97     }
98
99     /**
100      * @dataProvider providePrototypedArrayNodeDefaults
101      */
102     public function testPrototypedArrayNodeDefault($args, $shouldThrowWhenUsingAttrAsKey, $shouldThrowWhenNotUsingAttrAsKey, $defaults)
103     {
104         $node = new ArrayNodeDefinition('root');
105         $node
106             ->addDefaultChildrenIfNoneSet($args)
107             ->prototype('array')
108         ;
109
110         try {
111             $tree = $node->getNode();
112             $this->assertFalse($shouldThrowWhenNotUsingAttrAsKey);
113             $this->assertEquals($defaults, $tree->getDefaultValue());
114         } catch (InvalidDefinitionException $e) {
115             $this->assertTrue($shouldThrowWhenNotUsingAttrAsKey);
116         }
117
118         $node = new ArrayNodeDefinition('root');
119         $node
120             ->useAttributeAsKey('attr')
121             ->addDefaultChildrenIfNoneSet($args)
122             ->prototype('array')
123         ;
124
125         try {
126             $tree = $node->getNode();
127             $this->assertFalse($shouldThrowWhenUsingAttrAsKey);
128             $this->assertEquals($defaults, $tree->getDefaultValue());
129         } catch (InvalidDefinitionException $e) {
130             $this->assertTrue($shouldThrowWhenUsingAttrAsKey);
131         }
132     }
133
134     public function providePrototypedArrayNodeDefaults()
135     {
136         return array(
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())),
143         );
144     }
145
146     public function testNestedPrototypedArrayNodes()
147     {
148         $nodeDefinition = new ArrayNodeDefinition('root');
149         $nodeDefinition
150             ->addDefaultChildrenIfNoneSet()
151             ->prototype('array')
152                   ->prototype('array')
153         ;
154         $node = $nodeDefinition->getNode();
155
156         $this->assertInstanceOf('Symfony\Component\Config\Definition\PrototypedArrayNode', $node);
157         $this->assertInstanceOf('Symfony\Component\Config\Definition\PrototypedArrayNode', $node->getPrototype());
158     }
159
160     public function testEnabledNodeDefaults()
161     {
162         $node = new ArrayNodeDefinition('root');
163         $node
164             ->canBeEnabled()
165             ->children()
166                 ->scalarNode('foo')->defaultValue('bar')->end()
167         ;
168
169         $this->assertEquals(array('enabled' => false, 'foo' => 'bar'), $node->getNode()->getDefaultValue());
170     }
171
172     /**
173      * @dataProvider getEnableableNodeFixtures
174      */
175     public function testTrueEnableEnabledNode($expected, $config, $message)
176     {
177         $processor = new Processor();
178         $node = new ArrayNodeDefinition('root');
179         $node
180             ->canBeEnabled()
181             ->children()
182                 ->scalarNode('foo')->defaultValue('bar')->end()
183         ;
184
185         $this->assertEquals(
186             $expected,
187             $processor->process($node->getNode(), $config),
188             $message
189         );
190     }
191
192     public function testCanBeDisabled()
193     {
194         $node = new ArrayNodeDefinition('root');
195         $node->canBeDisabled();
196
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'));
201
202         $nodeChildren = $this->getField($node, 'children');
203         $this->assertArrayHasKey('enabled', $nodeChildren);
204
205         $enabledNode = $nodeChildren['enabled'];
206         $this->assertTrue($this->getField($enabledNode, 'default'));
207         $this->assertTrue($this->getField($enabledNode, 'defaultValue'));
208     }
209
210     public function testIgnoreExtraKeys()
211     {
212         $node = new ArrayNodeDefinition('root');
213
214         $this->assertFalse($this->getField($node, 'ignoreExtraKeys'));
215
216         $result = $node->ignoreExtraKeys();
217
218         $this->assertEquals($node, $result);
219         $this->assertTrue($this->getField($node, 'ignoreExtraKeys'));
220     }
221
222     public function testNormalizeKeys()
223     {
224         $node = new ArrayNodeDefinition('root');
225
226         $this->assertTrue($this->getField($node, 'normalizeKeys'));
227
228         $result = $node->normalizeKeys(false);
229
230         $this->assertEquals($node, $result);
231         $this->assertFalse($this->getField($node, 'normalizeKeys'));
232     }
233
234     public function getEnableableNodeFixtures()
235     {
236         return array(
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'),
243         );
244     }
245
246     protected function getField($object, $field)
247     {
248         $reflection = new \ReflectionProperty($object, $field);
249         $reflection->setAccessible(true);
250
251         return $reflection->getValue($object);
252     }
253 }