ExprBuilderTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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, ['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, ['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, ['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 testIfEmptyExpression()
  67. {
  68. $test = $this->getTestBuilder()
  69. ->ifEmpty()
  70. ->then($this->returnClosure('new_value'))
  71. ->end();
  72. $this->assertFinalizedValueIs('new_value', $test, ['key' => []]);
  73. $test = $this->getTestBuilder()
  74. ->ifEmpty()
  75. ->then($this->returnClosure('new_value'))
  76. ->end();
  77. $this->assertFinalizedValueIs('value', $test);
  78. }
  79. public function testIfArrayExpression()
  80. {
  81. $test = $this->getTestBuilder()
  82. ->ifArray()
  83. ->then($this->returnClosure('new_value'))
  84. ->end();
  85. $this->assertFinalizedValueIs('new_value', $test, ['key' => []]);
  86. $test = $this->getTestBuilder()
  87. ->ifArray()
  88. ->then($this->returnClosure('new_value'))
  89. ->end();
  90. $this->assertFinalizedValueIs('value', $test);
  91. }
  92. public function testIfInArrayExpression()
  93. {
  94. $test = $this->getTestBuilder()
  95. ->ifInArray(['foo', 'bar', 'value'])
  96. ->then($this->returnClosure('new_value'))
  97. ->end();
  98. $this->assertFinalizedValueIs('new_value', $test);
  99. $test = $this->getTestBuilder()
  100. ->ifInArray(['foo', 'bar'])
  101. ->then($this->returnClosure('new_value'))
  102. ->end();
  103. $this->assertFinalizedValueIs('value', $test);
  104. }
  105. public function testIfNotInArrayExpression()
  106. {
  107. $test = $this->getTestBuilder()
  108. ->ifNotInArray(['foo', 'bar'])
  109. ->then($this->returnClosure('new_value'))
  110. ->end();
  111. $this->assertFinalizedValueIs('new_value', $test);
  112. $test = $this->getTestBuilder()
  113. ->ifNotInArray(['foo', 'bar', 'value_from_config'])
  114. ->then($this->returnClosure('new_value'))
  115. ->end();
  116. $this->assertFinalizedValueIs('new_value', $test);
  117. }
  118. public function testThenEmptyArrayExpression()
  119. {
  120. $test = $this->getTestBuilder()
  121. ->ifString()
  122. ->thenEmptyArray()
  123. ->end();
  124. $this->assertFinalizedValueIs([], $test);
  125. }
  126. /**
  127. * @dataProvider castToArrayValues
  128. */
  129. public function testcastToArrayExpression($configValue, $expectedValue)
  130. {
  131. $test = $this->getTestBuilder()
  132. ->castToArray()
  133. ->end();
  134. $this->assertFinalizedValueIs($expectedValue, $test, ['key' => $configValue]);
  135. }
  136. public function castToArrayValues()
  137. {
  138. yield ['value', ['value']];
  139. yield [-3.14, [-3.14]];
  140. yield [null, [null]];
  141. yield [['value'], ['value']];
  142. }
  143. /**
  144. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  145. */
  146. public function testThenInvalid()
  147. {
  148. $test = $this->getTestBuilder()
  149. ->ifString()
  150. ->thenInvalid('Invalid value')
  151. ->end();
  152. $this->finalizeTestBuilder($test);
  153. }
  154. public function testThenUnsetExpression()
  155. {
  156. $test = $this->getTestBuilder()
  157. ->ifString()
  158. ->thenUnset()
  159. ->end();
  160. $this->assertEquals([], $this->finalizeTestBuilder($test));
  161. }
  162. /**
  163. * @expectedException \RuntimeException
  164. * @expectedExceptionMessage You must specify an if part.
  165. */
  166. public function testEndIfPartNotSpecified()
  167. {
  168. $this->getTestBuilder()->end();
  169. }
  170. /**
  171. * @expectedException \RuntimeException
  172. * @expectedExceptionMessage You must specify a then part.
  173. */
  174. public function testEndThenPartNotSpecified()
  175. {
  176. $builder = $this->getTestBuilder();
  177. $builder->ifPart = 'test';
  178. $builder->end();
  179. }
  180. /**
  181. * Create a test treebuilder with a variable node, and init the validation.
  182. *
  183. * @return TreeBuilder
  184. */
  185. protected function getTestBuilder()
  186. {
  187. $builder = new TreeBuilder('test');
  188. return $builder
  189. ->getRootNode()
  190. ->children()
  191. ->variableNode('key')
  192. ->validate()
  193. ;
  194. }
  195. /**
  196. * Close the validation process and finalize with the given config.
  197. *
  198. * @param TreeBuilder $testBuilder The tree builder to finalize
  199. * @param array $config The config you want to use for the finalization, if nothing provided
  200. * a simple ['key'=>'value'] will be used
  201. *
  202. * @return array The finalized config values
  203. */
  204. protected function finalizeTestBuilder($testBuilder, $config = null)
  205. {
  206. return $testBuilder
  207. ->end()
  208. ->end()
  209. ->end()
  210. ->buildTree()
  211. ->finalize(null === $config ? ['key' => 'value'] : $config)
  212. ;
  213. }
  214. /**
  215. * Return a closure that will return the given value.
  216. *
  217. * @param mixed $val The value that the closure must return
  218. *
  219. * @return \Closure
  220. */
  221. protected function returnClosure($val)
  222. {
  223. return function ($v) use ($val) {
  224. return $val;
  225. };
  226. }
  227. /**
  228. * Assert that the given test builder, will return the given value.
  229. *
  230. * @param mixed $value The value to test
  231. * @param TreeBuilder $treeBuilder The tree builder to finalize
  232. * @param mixed $config The config values that new to be finalized
  233. */
  234. protected function assertFinalizedValueIs($value, $treeBuilder, $config = null)
  235. {
  236. $this->assertEquals(['key' => $value], $this->finalizeTestBuilder($treeBuilder, $config));
  237. }
  238. }