feea16f9c397bbd8c3aa4c2ed18875a2956d3e7a
[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      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
150      */
151     public function testThenInvalid()
152     {
153         $test = $this->getTestBuilder()
154             ->ifString()
155             ->thenInvalid('Invalid value')
156         ->end();
157         $this->finalizeTestBuilder($test);
158     }
159
160     public function testThenUnsetExpression()
161     {
162         $test = $this->getTestBuilder()
163             ->ifString()
164             ->thenUnset()
165         ->end();
166         $this->assertEquals(array(), $this->finalizeTestBuilder($test));
167     }
168
169     /**
170      * @expectedException \RuntimeException
171      * @expectedExceptionMessage You must specify an if part.
172      */
173     public function testEndIfPartNotSpecified()
174     {
175         $this->getTestBuilder()->end();
176     }
177
178     /**
179      * @expectedException \RuntimeException
180      * @expectedExceptionMessage You must specify a then part.
181      */
182     public function testEndThenPartNotSpecified()
183     {
184         $builder = $this->getTestBuilder();
185         $builder->ifPart = 'test';
186         $builder->end();
187     }
188
189     /**
190      * Create a test treebuilder with a variable node, and init the validation.
191      *
192      * @return TreeBuilder
193      */
194     protected function getTestBuilder()
195     {
196         $builder = new TreeBuilder();
197
198         return $builder
199             ->root('test')
200             ->children()
201             ->variableNode('key')
202             ->validate()
203         ;
204     }
205
206     /**
207      * Close the validation process and finalize with the given config.
208      *
209      * @param TreeBuilder $testBuilder The tree builder to finalize
210      * @param array       $config      The config you want to use for the finalization, if nothing provided
211      *                                 a simple array('key'=>'value') will be used
212      *
213      * @return array The finalized config values
214      */
215     protected function finalizeTestBuilder($testBuilder, $config = null)
216     {
217         return $testBuilder
218             ->end()
219             ->end()
220             ->end()
221             ->buildTree()
222             ->finalize(null === $config ? array('key' => 'value') : $config)
223         ;
224     }
225
226     /**
227      * Return a closure that will return the given value.
228      *
229      * @param mixed $val The value that the closure must return
230      *
231      * @return \Closure
232      */
233     protected function returnClosure($val)
234     {
235         return function ($v) use ($val) {
236             return $val;
237         };
238     }
239
240     /**
241      * Assert that the given test builder, will return the given value.
242      *
243      * @param mixed       $value       The value to test
244      * @param TreeBuilder $treeBuilder The tree builder to finalize
245      * @param mixed       $config      The config values that new to be finalized
246      */
247     protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
248     {
249         $this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));
250     }
251 }