Yaffs site version 1.1
[yaffs-website] / vendor / symfony / config / Tests / Definition / MergeTest.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\Builder\TreeBuilder;
16
17 class MergeTest extends TestCase
18 {
19     /**
20      * @expectedException \Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException
21      */
22     public function testForbiddenOverwrite()
23     {
24         $tb = new TreeBuilder();
25         $tree = $tb
26             ->root('root', 'array')
27                 ->children()
28                     ->node('foo', 'scalar')
29                         ->cannotBeOverwritten()
30                     ->end()
31                 ->end()
32             ->end()
33             ->buildTree()
34         ;
35
36         $a = array(
37             'foo' => 'bar',
38         );
39
40         $b = array(
41             'foo' => 'moo',
42         );
43
44         $tree->merge($a, $b);
45     }
46
47     public function testUnsetKey()
48     {
49         $tb = new TreeBuilder();
50         $tree = $tb
51             ->root('root', 'array')
52                 ->children()
53                     ->node('foo', 'scalar')->end()
54                     ->node('bar', 'scalar')->end()
55                     ->node('unsettable', 'array')
56                         ->canBeUnset()
57                         ->children()
58                             ->node('foo', 'scalar')->end()
59                             ->node('bar', 'scalar')->end()
60                         ->end()
61                     ->end()
62                     ->node('unsetted', 'array')
63                         ->canBeUnset()
64                         ->prototype('scalar')->end()
65                     ->end()
66                 ->end()
67             ->end()
68             ->buildTree()
69         ;
70
71         $a = array(
72             'foo' => 'bar',
73             'unsettable' => array(
74                 'foo' => 'a',
75                 'bar' => 'b',
76             ),
77             'unsetted' => false,
78         );
79
80         $b = array(
81             'foo' => 'moo',
82             'bar' => 'b',
83             'unsettable' => false,
84             'unsetted' => array('a', 'b'),
85         );
86
87         $this->assertEquals(array(
88             'foo' => 'moo',
89             'bar' => 'b',
90             'unsettable' => false,
91             'unsetted' => array('a', 'b'),
92         ), $tree->merge($a, $b));
93     }
94
95     /**
96      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
97      */
98     public function testDoesNotAllowNewKeysInSubsequentConfigs()
99     {
100         $tb = new TreeBuilder();
101         $tree = $tb
102             ->root('config', 'array')
103                 ->children()
104                     ->node('test', 'array')
105                         ->disallowNewKeysInSubsequentConfigs()
106                         ->useAttributeAsKey('key')
107                         ->prototype('array')
108                             ->children()
109                                 ->node('value', 'scalar')->end()
110                             ->end()
111                         ->end()
112                     ->end()
113                 ->end()
114             ->end()
115             ->buildTree();
116
117         $a = array(
118             'test' => array(
119                 'a' => array('value' => 'foo'),
120             ),
121         );
122
123         $b = array(
124             'test' => array(
125                 'b' => array('value' => 'foo'),
126             ),
127         );
128
129         $tree->merge($a, $b);
130     }
131
132     public function testPerformsNoDeepMerging()
133     {
134         $tb = new TreeBuilder();
135
136         $tree = $tb
137             ->root('config', 'array')
138                 ->children()
139                     ->node('no_deep_merging', 'array')
140                         ->performNoDeepMerging()
141                         ->children()
142                             ->node('foo', 'scalar')->end()
143                             ->node('bar', 'scalar')->end()
144                         ->end()
145                     ->end()
146                 ->end()
147             ->end()
148             ->buildTree()
149         ;
150
151         $a = array(
152             'no_deep_merging' => array(
153                 'foo' => 'a',
154                 'bar' => 'b',
155             ),
156         );
157
158         $b = array(
159             'no_deep_merging' => array(
160                 'c' => 'd',
161             ),
162         );
163
164         $this->assertEquals(array(
165             'no_deep_merging' => array(
166                 'c' => 'd',
167             ),
168         ), $tree->merge($a, $b));
169     }
170
171     public function testPrototypeWithoutAKeyAttribute()
172     {
173         $tb = new TreeBuilder();
174
175         $tree = $tb
176             ->root('config', 'array')
177                 ->children()
178                     ->arrayNode('append_elements')
179                         ->prototype('scalar')->end()
180                     ->end()
181                 ->end()
182             ->end()
183             ->buildTree()
184         ;
185
186         $a = array(
187             'append_elements' => array('a', 'b'),
188         );
189
190         $b = array(
191             'append_elements' => array('c', 'd'),
192         );
193
194         $this->assertEquals(array('append_elements' => array('a', 'b', 'c', 'd')), $tree->merge($a, $b));
195     }
196 }