Yaffs site version 1.1
[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 testIfArrayExpression()
80     {
81         $test = $this->getTestBuilder()
82             ->ifArray()
83             ->then($this->returnClosure('new_value'))
84         ->end();
85         $this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
86
87         $test = $this->getTestBuilder()
88             ->ifArray()
89             ->then($this->returnClosure('new_value'))
90         ->end();
91         $this->assertFinalizedValueIs('value', $test);
92     }
93
94     public function testIfInArrayExpression()
95     {
96         $test = $this->getTestBuilder()
97             ->ifInArray(array('foo', 'bar', 'value'))
98             ->then($this->returnClosure('new_value'))
99         ->end();
100         $this->assertFinalizedValueIs('new_value', $test);
101
102         $test = $this->getTestBuilder()
103             ->ifInArray(array('foo', 'bar'))
104             ->then($this->returnClosure('new_value'))
105         ->end();
106         $this->assertFinalizedValueIs('value', $test);
107     }
108
109     public function testIfNotInArrayExpression()
110     {
111         $test = $this->getTestBuilder()
112             ->ifNotInArray(array('foo', 'bar'))
113             ->then($this->returnClosure('new_value'))
114         ->end();
115         $this->assertFinalizedValueIs('new_value', $test);
116
117         $test = $this->getTestBuilder()
118             ->ifNotInArray(array('foo', 'bar', 'value_from_config'))
119             ->then($this->returnClosure('new_value'))
120         ->end();
121         $this->assertFinalizedValueIs('new_value', $test);
122     }
123
124     public function testThenEmptyArrayExpression()
125     {
126         $test = $this->getTestBuilder()
127             ->ifString()
128             ->thenEmptyArray()
129         ->end();
130         $this->assertFinalizedValueIs(array(), $test);
131     }
132
133     /**
134      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
135      */
136     public function testThenInvalid()
137     {
138         $test = $this->getTestBuilder()
139             ->ifString()
140             ->thenInvalid('Invalid value')
141         ->end();
142         $this->finalizeTestBuilder($test);
143     }
144
145     public function testThenUnsetExpression()
146     {
147         $test = $this->getTestBuilder()
148             ->ifString()
149             ->thenUnset()
150         ->end();
151         $this->assertEquals(array(), $this->finalizeTestBuilder($test));
152     }
153
154     /**
155      * @expectedException \RuntimeException
156      * @expectedExceptionMessage You must specify an if part.
157      */
158     public function testEndIfPartNotSpecified()
159     {
160         $this->getTestBuilder()->end();
161     }
162
163     /**
164      * @expectedException \RuntimeException
165      * @expectedExceptionMessage You must specify a then part.
166      */
167     public function testEndThenPartNotSpecified()
168     {
169         $builder = $this->getTestBuilder();
170         $builder->ifPart = 'test';
171         $builder->end();
172     }
173
174     /**
175      * Create a test treebuilder with a variable node, and init the validation.
176      *
177      * @return TreeBuilder
178      */
179     protected function getTestBuilder()
180     {
181         $builder = new TreeBuilder();
182
183         return $builder
184             ->root('test')
185             ->children()
186             ->variableNode('key')
187             ->validate()
188         ;
189     }
190
191     /**
192      * Close the validation process and finalize with the given config.
193      *
194      * @param TreeBuilder $testBuilder The tree builder to finalize
195      * @param array       $config      The config you want to use for the finalization, if nothing provided
196      *                                 a simple array('key'=>'value') will be used
197      *
198      * @return array The finalized config values
199      */
200     protected function finalizeTestBuilder($testBuilder, $config = null)
201     {
202         return $testBuilder
203             ->end()
204             ->end()
205             ->end()
206             ->buildTree()
207             ->finalize(null === $config ? array('key' => 'value') : $config)
208         ;
209     }
210
211     /**
212      * Return a closure that will return the given value.
213      *
214      * @param mixed $val The value that the closure must return
215      *
216      * @return \Closure
217      */
218     protected function returnClosure($val)
219     {
220         return function ($v) use ($val) {
221             return $val;
222         };
223     }
224
225     /**
226      * Assert that the given test builder, will return the given value.
227      *
228      * @param mixed       $value       The value to test
229      * @param TreeBuilder $treeBuilder The tree builder to finalize
230      * @param mixed       $config      The config values that new to be finalized
231      */
232     protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
233     {
234         $this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));
235     }
236 }