Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / config / Tests / Definition / Builder / ExprBuilderTest.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\TreeBuilder;
16
17 class ExprBuilderTest extends TestCase
18 {
19     public function testAlwaysExpression()
20     {
21         $test = $this->getTestBuilder()
22             ->always($this->returnClosure('new_value'))
23         ->end();
24
25         $this->assertFinalizedValueIs('new_value', $test);
26     }
27
28     public function testIfTrueExpression()
29     {
30         $test = $this->getTestBuilder()
31             ->ifTrue()
32             ->then($this->returnClosure('new_value'))
33         ->end();
34         $this->assertFinalizedValueIs('new_value', $test, array('key' => true));
35
36         $test = $this->getTestBuilder()
37             ->ifTrue(function ($v) { return true; })
38             ->then($this->returnClosure('new_value'))
39         ->end();
40         $this->assertFinalizedValueIs('new_value', $test);
41
42         $test = $this->getTestBuilder()
43             ->ifTrue(function ($v) { return false; })
44             ->then($this->returnClosure('new_value'))
45         ->end();
46         $this->assertFinalizedValueIs('value', $test);
47     }
48
49     public function testIfStringExpression()
50     {
51         $test = $this->getTestBuilder()
52             ->ifString()
53             ->then($this->returnClosure('new_value'))
54         ->end();
55         $this->assertFinalizedValueIs('new_value', $test);
56
57         $test = $this->getTestBuilder()
58             ->ifString()
59             ->then($this->returnClosure('new_value'))
60         ->end();
61         $this->assertFinalizedValueIs(45, $test, array('key' => 45));
62     }
63
64     public function testIfNullExpression()
65     {
66         $test = $this->getTestBuilder()
67             ->ifNull()
68             ->then($this->returnClosure('new_value'))
69         ->end();
70         $this->assertFinalizedValueIs('new_value', $test, array('key' => null));
71
72         $test = $this->getTestBuilder()
73             ->ifNull()
74             ->then($this->returnClosure('new_value'))
75         ->end();
76         $this->assertFinalizedValueIs('value', $test);
77     }
78
79     public function testIfEmptyExpression()
80     {
81         $test = $this->getTestBuilder()
82             ->ifEmpty()
83             ->then($this->returnClosure('new_value'))
84         ->end();
85         $this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
86
87         $test = $this->getTestBuilder()
88             ->ifEmpty()
89             ->then($this->returnClosure('new_value'))
90         ->end();
91         $this->assertFinalizedValueIs('value', $test);
92     }
93
94     public function testIfArrayExpression()
95     {
96         $test = $this->getTestBuilder()
97             ->ifArray()
98             ->then($this->returnClosure('new_value'))
99         ->end();
100         $this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
101
102         $test = $this->getTestBuilder()
103             ->ifArray()
104             ->then($this->returnClosure('new_value'))
105         ->end();
106         $this->assertFinalizedValueIs('value', $test);
107     }
108
109     public function testIfInArrayExpression()
110     {
111         $test = $this->getTestBuilder()
112             ->ifInArray(array('foo', 'bar', 'value'))
113             ->then($this->returnClosure('new_value'))
114         ->end();
115         $this->assertFinalizedValueIs('new_value', $test);
116
117         $test = $this->getTestBuilder()
118             ->ifInArray(array('foo', 'bar'))
119             ->then($this->returnClosure('new_value'))
120         ->end();
121         $this->assertFinalizedValueIs('value', $test);
122     }
123
124     public function testIfNotInArrayExpression()
125     {
126         $test = $this->getTestBuilder()
127             ->ifNotInArray(array('foo', 'bar'))
128             ->then($this->returnClosure('new_value'))
129         ->end();
130         $this->assertFinalizedValueIs('new_value', $test);
131
132         $test = $this->getTestBuilder()
133             ->ifNotInArray(array('foo', 'bar', 'value_from_config'))
134             ->then($this->returnClosure('new_value'))
135         ->end();
136         $this->assertFinalizedValueIs('new_value', $test);
137     }
138
139     public function testThenEmptyArrayExpression()
140     {
141         $test = $this->getTestBuilder()
142             ->ifString()
143             ->thenEmptyArray()
144         ->end();
145         $this->assertFinalizedValueIs(array(), $test);
146     }
147
148     /**
149      * @dataProvider castToArrayValues
150      */
151     public function testcastToArrayExpression($configValue, $expectedValue)
152     {
153         $test = $this->getTestBuilder()
154             ->castToArray()
155         ->end();
156         $this->assertFinalizedValueIs($expectedValue, $test, array('key' => $configValue));
157     }
158
159     public function castToArrayValues()
160     {
161         yield array('value', array('value'));
162         yield array(-3.14, array(-3.14));
163         yield array(null, array(null));
164         yield array(array('value'), array('value'));
165     }
166
167     /**
168      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
169      */
170     public function testThenInvalid()
171     {
172         $test = $this->getTestBuilder()
173             ->ifString()
174             ->thenInvalid('Invalid value')
175         ->end();
176         $this->finalizeTestBuilder($test);
177     }
178
179     public function testThenUnsetExpression()
180     {
181         $test = $this->getTestBuilder()
182             ->ifString()
183             ->thenUnset()
184         ->end();
185         $this->assertEquals(array(), $this->finalizeTestBuilder($test));
186     }
187
188     /**
189      * @expectedException \RuntimeException
190      * @expectedExceptionMessage You must specify an if part.
191      */
192     public function testEndIfPartNotSpecified()
193     {
194         $this->getTestBuilder()->end();
195     }
196
197     /**
198      * @expectedException \RuntimeException
199      * @expectedExceptionMessage You must specify a then part.
200      */
201     public function testEndThenPartNotSpecified()
202     {
203         $builder = $this->getTestBuilder();
204         $builder->ifPart = 'test';
205         $builder->end();
206     }
207
208     /**
209      * Create a test treebuilder with a variable node, and init the validation.
210      *
211      * @return TreeBuilder
212      */
213     protected function getTestBuilder()
214     {
215         $builder = new TreeBuilder();
216
217         return $builder
218             ->root('test')
219             ->children()
220             ->variableNode('key')
221             ->validate()
222         ;
223     }
224
225     /**
226      * Close the validation process and finalize with the given config.
227      *
228      * @param TreeBuilder $testBuilder The tree builder to finalize
229      * @param array       $config      The config you want to use for the finalization, if nothing provided
230      *                                 a simple array('key'=>'value') will be used
231      *
232      * @return array The finalized config values
233      */
234     protected function finalizeTestBuilder($testBuilder, $config = null)
235     {
236         return $testBuilder
237             ->end()
238             ->end()
239             ->end()
240             ->buildTree()
241             ->finalize(null === $config ? array('key' => 'value') : $config)
242         ;
243     }
244
245     /**
246      * Return a closure that will return the given value.
247      *
248      * @param mixed $val The value that the closure must return
249      *
250      * @return \Closure
251      */
252     protected function returnClosure($val)
253     {
254         return function ($v) use ($val) {
255             return $val;
256         };
257     }
258
259     /**
260      * Assert that the given test builder, will return the given value.
261      *
262      * @param mixed       $value       The value to test
263      * @param TreeBuilder $treeBuilder The tree builder to finalize
264      * @param mixed       $config      The config values that new to be finalized
265      */
266     protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
267     {
268         $this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));
269     }
270 }