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 testIfArrayExpression()
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 testIfInArrayExpression()
96 $test = $this->getTestBuilder()
97 ->ifInArray(array('foo', 'bar', 'value'))
98 ->then($this->returnClosure('new_value'))
100 $this->assertFinalizedValueIs('new_value', $test);
102 $test = $this->getTestBuilder()
103 ->ifInArray(array('foo', 'bar'))
104 ->then($this->returnClosure('new_value'))
106 $this->assertFinalizedValueIs('value', $test);
109 public function testIfNotInArrayExpression()
111 $test = $this->getTestBuilder()
112 ->ifNotInArray(array('foo', 'bar'))
113 ->then($this->returnClosure('new_value'))
115 $this->assertFinalizedValueIs('new_value', $test);
117 $test = $this->getTestBuilder()
118 ->ifNotInArray(array('foo', 'bar', 'value_from_config'))
119 ->then($this->returnClosure('new_value'))
121 $this->assertFinalizedValueIs('new_value', $test);
124 public function testThenEmptyArrayExpression()
126 $test = $this->getTestBuilder()
130 $this->assertFinalizedValueIs(array(), $test);
134 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
136 public function testThenInvalid()
138 $test = $this->getTestBuilder()
140 ->thenInvalid('Invalid value')
142 $this->finalizeTestBuilder($test);
145 public function testThenUnsetExpression()
147 $test = $this->getTestBuilder()
151 $this->assertEquals(array(), $this->finalizeTestBuilder($test));
155 * @expectedException \RuntimeException
156 * @expectedExceptionMessage You must specify an if part.
158 public function testEndIfPartNotSpecified()
160 $this->getTestBuilder()->end();
164 * @expectedException \RuntimeException
165 * @expectedExceptionMessage You must specify a then part.
167 public function testEndThenPartNotSpecified()
169 $builder = $this->getTestBuilder();
170 $builder->ifPart = 'test';
175 * Create a test treebuilder with a variable node, and init the validation.
177 * @return TreeBuilder
179 protected function getTestBuilder()
181 $builder = new TreeBuilder();
186 ->variableNode('key')
192 * Close the validation process and finalize with the given config.
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
198 * @return array The finalized config values
200 protected function finalizeTestBuilder($testBuilder, $config = null)
207 ->finalize(null === $config ? array('key' => 'value') : $config)
212 * Return a closure that will return the given value.
214 * @param mixed $val The value that the closure must return
218 protected function returnClosure($val)
220 return function ($v) use ($val) {
226 * Assert that the given test builder, will return the given value.
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
232 protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
234 $this->assertEquals(array('key' => $value), $this->finalizeTestBuilder($treeBuilder, $config));