Skip to content

Commit 40b6b23

Browse files
committed
merged branch jeanmonod/config-unittest-on-exprbuilder (PR #4570)
Commits ------- 9d730be Method rename and phpdoc fixes e01a95e Add a set of unit tests for the ExprBuilder class Discussion ---------- Add a set of unit tests for the Config/ExprBuilder class --------------------------------------------------------------------------- by travisbot at 2012-06-13T14:55:31Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1611400) (merged e01a95e1 into c07e9163). --------------------------------------------------------------------------- by jeanmonod at 2012-06-13T22:04:52Z Hi there, I write all these tests because I'll come latter with an other PR that extend the ExprBuilder functionality. But I'm not sure I use the best way for testing this class. It's working, but some refactoring suggestions will be welcome... @stof and @schmittjoh what do you think about that?
2 parents 9d1213e + 22f81f5 commit 40b6b23

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
16+
17+
class ExprBuilderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
20+
public function testAlwaysExpression()
21+
{
22+
$test = $this->getTestBuilder()
23+
->always($this->returnClosure('new_value'))
24+
->end();
25+
26+
$this->assertFinalizedValueIs('new_value', $test);
27+
}
28+
29+
public function testIfTrueExpression()
30+
{
31+
$test = $this->getTestBuilder()
32+
->ifTrue()
33+
->then($this->returnClosure('new_value'))
34+
->end();
35+
$this->assertFinalizedValueIs('new_value', $test, array('key'=>true));
36+
37+
$test = $this->getTestBuilder()
38+
->ifTrue( function($v){ return true; })
39+
->then($this->returnClosure('new_value'))
40+
->end();
41+
$this->assertFinalizedValueIs('new_value', $test);
42+
43+
$test = $this->getTestBuilder()
44+
->ifTrue( function($v){ return false; })
45+
->then($this->returnClosure('new_value'))
46+
->end();
47+
$this->assertFinalizedValueIs('value',$test);
48+
}
49+
50+
public function testIfStringExpression()
51+
{
52+
$test = $this->getTestBuilder()
53+
->ifString()
54+
->then($this->returnClosure('new_value'))
55+
->end();
56+
$this->assertFinalizedValueIs('new_value', $test);
57+
58+
$test = $this->getTestBuilder()
59+
->ifString()
60+
->then($this->returnClosure('new_value'))
61+
->end();
62+
$this->assertFinalizedValueIs(45, $test, array('key'=>45));
63+
64+
}
65+
66+
public function testIfNullExpression()
67+
{
68+
$test = $this->getTestBuilder()
69+
->ifNull()
70+
->then($this->returnClosure('new_value'))
71+
->end();
72+
$this->assertFinalizedValueIs('new_value', $test, array('key'=>null));
73+
74+
$test = $this->getTestBuilder()
75+
->ifNull()
76+
->then($this->returnClosure('new_value'))
77+
->end();
78+
$this->assertFinalizedValueIs('value', $test);
79+
}
80+
81+
public function testIfArrayExpression()
82+
{
83+
$test = $this->getTestBuilder()
84+
->ifArray()
85+
->then($this->returnClosure('new_value'))
86+
->end();
87+
$this->assertFinalizedValueIs('new_value', $test, array('key'=>array()));
88+
89+
$test = $this->getTestBuilder()
90+
->ifArray()
91+
->then($this->returnClosure('new_value'))
92+
->end();
93+
$this->assertFinalizedValueIs('value', $test);
94+
}
95+
96+
public function testIfInArrayExpression()
97+
{
98+
$test = $this->getTestBuilder()
99+
->ifInArray(array('foo', 'bar', 'value'))
100+
->then($this->returnClosure('new_value'))
101+
->end();
102+
$this->assertFinalizedValueIs('new_value', $test);
103+
104+
$test = $this->getTestBuilder()
105+
->ifInArray(array('foo', 'bar'))
106+
->then($this->returnClosure('new_value'))
107+
->end();
108+
$this->assertFinalizedValueIs('value', $test);
109+
}
110+
111+
public function testIfNotInArrayExpression()
112+
{
113+
$test = $this->getTestBuilder()
114+
->ifNotInArray(array('foo', 'bar'))
115+
->then($this->returnClosure('new_value'))
116+
->end();
117+
$this->assertFinalizedValueIs('new_value', $test);
118+
119+
$test = $this->getTestBuilder()
120+
->ifNotInArray(array('foo', 'bar', 'value_from_config' ))
121+
->then($this->returnClosure('new_value'))
122+
->end();
123+
$this->assertFinalizedValueIs('new_value', $test);
124+
}
125+
126+
public function testThenEmptyArrayExpression()
127+
{
128+
$test = $this->getTestBuilder()
129+
->ifString()
130+
->thenEmptyArray()
131+
->end();
132+
$this->assertFinalizedValueIs(array(), $test);
133+
}
134+
135+
/**
136+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
137+
*/
138+
public function testThenInvalid()
139+
{
140+
$test = $this->getTestBuilder()
141+
->ifString()
142+
->thenInvalid('Invalid value')
143+
->end();
144+
$this->finalizeTestBuilder($test);
145+
}
146+
147+
public function testThenUnsetExpression()
148+
{
149+
$test = $this->getTestBuilder()
150+
->ifString()
151+
->thenUnset()
152+
->end();
153+
$this->assertEquals(array(), $this->finalizeTestBuilder($test));
154+
}
155+
156+
/**
157+
* Create a test treebuilder with a variable node, and init the validation
158+
* @return TreeBuilder
159+
*/
160+
protected function getTestBuilder()
161+
{
162+
$builder = new TreeBuilder();
163+
return $builder
164+
->root('test')
165+
->children()
166+
->variableNode('key')
167+
->validate()
168+
;
169+
}
170+
171+
/**
172+
* Close the validation process and finalize with the given config
173+
* @param TreeBuilder $testBuilder The tree builder to finalize
174+
* @param array $config The config you want to use for the finalization, if nothing provided
175+
* a simple array('key'=>'value') will be used
176+
* @return array The finalized config values
177+
*/
178+
protected function finalizeTestBuilder($testBuilder, $config=null)
179+
{
180+
return $testBuilder
181+
->end()
182+
->end()
183+
->end()
184+
->buildTree()
185+
->finalize($config === null ? array('key'=>'value') : $config)
186+
;
187+
}
188+
189+
/**
190+
* Return a closure that will return the given value
191+
* @param $val The value that the closure must return
192+
* @return Closure
193+
*/
194+
protected function returnClosure($val) {
195+
return function($v) use ($val) {
196+
return $val;
197+
};
198+
}
199+
200+
/**
201+
* Assert that the given test builder, will return the given value
202+
* @param mixed $value The value to test
203+
* @param TreeBuilder $test The tree builder to finalize
204+
* @param mixed $config The config values that new to be finalized
205+
*/
206+
protected function assertFinalizedValueIs($value, $treeBuilder, $config=null)
207+
{
208+
$this->assertEquals(array('key'=>$value), $this->finalizeTestBuilder($treeBuilder, $config));
209+
}
210+
}

0 commit comments

Comments
 (0)