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 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
151 public function testThenInvalid()
153 $test = $this->getTestBuilder()
155 ->thenInvalid('Invalid value')
157 $this->finalizeTestBuilder($test);
160 public function testThenUnsetExpression()
162 $test = $this->getTestBuilder()
166 $this->assertEquals(array(), $this->finalizeTestBuilder($test));
170 * @expectedException \RuntimeException
171 * @expectedExceptionMessage You must specify an if part.
173 public function testEndIfPartNotSpecified()
175 $this->getTestBuilder()->end();
179 * @expectedException \RuntimeException
180 * @expectedExceptionMessage You must specify a then part.
182 public function testEndThenPartNotSpecified()
184 $builder = $this->getTestBuilder();
185 $builder->ifPart = 'test';
190 * Create a test treebuilder with a variable node, and init the validation.
192 * @return TreeBuilder
194 protected function getTestBuilder()
196 $builder = new TreeBuilder();
201 ->variableNode('key')
207 * Close the validation process and finalize with the given config.
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
213 * @return array The finalized config values
215 protected function finalizeTestBuilder($testBuilder, $config = null)
222 ->finalize(null === $config ? array('key' => 'value') : $config)
227 * Return a closure that will return the given value.
229 * @param mixed $val The value that the closure must return
233 protected function returnClosure($val)
235 return function ($v) use ($val) {
241 * Assert that the given test builder, will return the given value.
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
247 protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
249 $this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));