4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Definition\Builder;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17 class ExprBuilderTest extends TestCase
19 public function testAlwaysExpression()
21 $test = $this->getTestBuilder()
22 ->always($this->returnClosure('new_value'))
25 $this->assertFinalizedValueIs('new_value', $test);
28 public function testIfTrueExpression()
30 $test = $this->getTestBuilder()
32 ->then($this->returnClosure('new_value'))
34 $this->assertFinalizedValueIs('new_value', $test, array('key' => true));
36 $test = $this->getTestBuilder()
37 ->ifTrue(function ($v) { return true; })
38 ->then($this->returnClosure('new_value'))
40 $this->assertFinalizedValueIs('new_value', $test);
42 $test = $this->getTestBuilder()
43 ->ifTrue(function ($v) { return false; })
44 ->then($this->returnClosure('new_value'))
46 $this->assertFinalizedValueIs('value', $test);
49 public function testIfStringExpression()
51 $test = $this->getTestBuilder()
53 ->then($this->returnClosure('new_value'))
55 $this->assertFinalizedValueIs('new_value', $test);
57 $test = $this->getTestBuilder()
59 ->then($this->returnClosure('new_value'))
61 $this->assertFinalizedValueIs(45, $test, array('key' => 45));
64 public function testIfNullExpression()
66 $test = $this->getTestBuilder()
68 ->then($this->returnClosure('new_value'))
70 $this->assertFinalizedValueIs('new_value', $test, array('key' => null));
72 $test = $this->getTestBuilder()
74 ->then($this->returnClosure('new_value'))
76 $this->assertFinalizedValueIs('value', $test);
79 public function testIfEmptyExpression()
81 $test = $this->getTestBuilder()
83 ->then($this->returnClosure('new_value'))
85 $this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
87 $test = $this->getTestBuilder()
89 ->then($this->returnClosure('new_value'))
91 $this->assertFinalizedValueIs('value', $test);
94 public function testIfArrayExpression()
96 $test = $this->getTestBuilder()
98 ->then($this->returnClosure('new_value'))
100 $this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
102 $test = $this->getTestBuilder()
104 ->then($this->returnClosure('new_value'))
106 $this->assertFinalizedValueIs('value', $test);
109 public function testIfInArrayExpression()
111 $test = $this->getTestBuilder()
112 ->ifInArray(array('foo', 'bar', 'value'))
113 ->then($this->returnClosure('new_value'))
115 $this->assertFinalizedValueIs('new_value', $test);
117 $test = $this->getTestBuilder()
118 ->ifInArray(array('foo', 'bar'))
119 ->then($this->returnClosure('new_value'))
121 $this->assertFinalizedValueIs('value', $test);
124 public function testIfNotInArrayExpression()
126 $test = $this->getTestBuilder()
127 ->ifNotInArray(array('foo', 'bar'))
128 ->then($this->returnClosure('new_value'))
130 $this->assertFinalizedValueIs('new_value', $test);
132 $test = $this->getTestBuilder()
133 ->ifNotInArray(array('foo', 'bar', 'value_from_config'))
134 ->then($this->returnClosure('new_value'))
136 $this->assertFinalizedValueIs('new_value', $test);
139 public function testThenEmptyArrayExpression()
141 $test = $this->getTestBuilder()
145 $this->assertFinalizedValueIs(array(), $test);
149 * @dataProvider castToArrayValues
151 public function testcastToArrayExpression($configValue, $expectedValue)
153 $test = $this->getTestBuilder()
156 $this->assertFinalizedValueIs($expectedValue, $test, array('key' => $configValue));
159 public function castToArrayValues()
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'));
168 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
170 public function testThenInvalid()
172 $test = $this->getTestBuilder()
174 ->thenInvalid('Invalid value')
176 $this->finalizeTestBuilder($test);
179 public function testThenUnsetExpression()
181 $test = $this->getTestBuilder()
185 $this->assertEquals(array(), $this->finalizeTestBuilder($test));
189 * @expectedException \RuntimeException
190 * @expectedExceptionMessage You must specify an if part.
192 public function testEndIfPartNotSpecified()
194 $this->getTestBuilder()->end();
198 * @expectedException \RuntimeException
199 * @expectedExceptionMessage You must specify a then part.
201 public function testEndThenPartNotSpecified()
203 $builder = $this->getTestBuilder();
204 $builder->ifPart = 'test';
209 * Create a test treebuilder with a variable node, and init the validation.
211 * @return TreeBuilder
213 protected function getTestBuilder()
215 $builder = new TreeBuilder();
220 ->variableNode('key')
226 * Close the validation process and finalize with the given config.
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
232 * @return array The finalized config values
234 protected function finalizeTestBuilder($testBuilder, $config = null)
241 ->finalize(null === $config ? array('key' => 'value') : $config)
246 * Return a closure that will return the given value.
248 * @param mixed $val The value that the closure must return
252 protected function returnClosure($val)
254 return function ($v) use ($val) {
260 * Assert that the given test builder, will return the given value.
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
266 protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
268 $this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));