Permalink
Browse files

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?
  • Loading branch information...
2 parents ac127ca + d1e5de3 commit 5b1a7a0a205b2c465a957142b03f1fc3202966e1 @fabpot fabpot committed Jun 28, 2012
Showing with 210 additions and 0 deletions.
  1. +210 −0 Tests/Definition/Builder/ExprBuilderTest.php
@@ -0,0 +1,210 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Config\Tests\Definition\Builder;
+
+use Symfony\Component\Config\Definition\Builder\TreeBuilder;
+
+
+class ExprBuilderTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testAlwaysExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->always($this->returnClosure('new_value'))
+ ->end();
+
+ $this->assertFinalizedValueIs('new_value', $test);
+ }
+
+ public function testIfTrueExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifTrue()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test, array('key'=>true));
+
+ $test = $this->getTestBuilder()
+ ->ifTrue( function($v){ return true; })
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test);
+
+ $test = $this->getTestBuilder()
+ ->ifTrue( function($v){ return false; })
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('value',$test);
+ }
+
+ public function testIfStringExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifString()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test);
+
+ $test = $this->getTestBuilder()
+ ->ifString()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs(45, $test, array('key'=>45));
+
+ }
+
+ public function testIfNullExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifNull()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test, array('key'=>null));
+
+ $test = $this->getTestBuilder()
+ ->ifNull()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('value', $test);
+ }
+
+ public function testIfArrayExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifArray()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test, array('key'=>array()));
+
+ $test = $this->getTestBuilder()
+ ->ifArray()
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('value', $test);
+ }
+
+ public function testIfInArrayExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifInArray(array('foo', 'bar', 'value'))
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test);
+
+ $test = $this->getTestBuilder()
+ ->ifInArray(array('foo', 'bar'))
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('value', $test);
+ }
+
+ public function testIfNotInArrayExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifNotInArray(array('foo', 'bar'))
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test);
+
+ $test = $this->getTestBuilder()
+ ->ifNotInArray(array('foo', 'bar', 'value_from_config' ))
+ ->then($this->returnClosure('new_value'))
+ ->end();
+ $this->assertFinalizedValueIs('new_value', $test);
+ }
+
+ public function testThenEmptyArrayExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifString()
+ ->thenEmptyArray()
+ ->end();
+ $this->assertFinalizedValueIs(array(), $test);
+ }
+
+ /**
+ * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
+ */
+ public function testThenInvalid()
+ {
+ $test = $this->getTestBuilder()
+ ->ifString()
+ ->thenInvalid('Invalid value')
+ ->end();
+ $this->finalizeTestBuilder($test);
+ }
+
+ public function testThenUnsetExpression()
+ {
+ $test = $this->getTestBuilder()
+ ->ifString()
+ ->thenUnset()
+ ->end();
+ $this->assertEquals(array(), $this->finalizeTestBuilder($test));
+ }
+
+ /**
+ * Create a test treebuilder with a variable node, and init the validation
+ * @return TreeBuilder
+ */
+ protected function getTestBuilder()
+ {
+ $builder = new TreeBuilder();
+ return $builder
+ ->root('test')
+ ->children()
+ ->variableNode('key')
+ ->validate()
+ ;
+ }
+
+ /**
+ * Close the validation process and finalize with the given config
+ * @param TreeBuilder $testBuilder The tree builder to finalize
+ * @param array $config The config you want to use for the finalization, if nothing provided
+ * a simple array('key'=>'value') will be used
+ * @return array The finalized config values
+ */
+ protected function finalizeTestBuilder($testBuilder, $config=null)
+ {
+ return $testBuilder
+ ->end()
+ ->end()
+ ->end()
+ ->buildTree()
+ ->finalize($config === null ? array('key'=>'value') : $config)
+ ;
+ }
+
+ /**
+ * Return a closure that will return the given value
+ * @param $val The value that the closure must return
+ * @return Closure
+ */
+ protected function returnClosure($val) {
+ return function($v) use ($val) {
+ return $val;
+ };
+ }
+
+ /**
+ * Assert that the given test builder, will return the given value
+ * @param mixed $value The value to test
+ * @param TreeBuilder $test The tree builder to finalize
+ * @param mixed $config The config values that new to be finalized
+ */
+ protected function assertFinalizedValueIs($value, $treeBuilder, $config=null)
+ {
+ $this->assertEquals(array('key'=>$value), $this->finalizeTestBuilder($treeBuilder, $config));
+ }
+}

0 comments on commit 5b1a7a0

Please sign in to comment.