ExprBuilderTest.php 6.8 KB

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