SearchAndRenderBlockNodeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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\Bridge\Twig\Tests\Node;
  11. use Symfony\Bridge\Twig\Tests\TestCase;
  12. use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
  13. class SearchAndRenderBlockNodeTest extends TestCase
  14. {
  15. protected function setUp()
  16. {
  17. parent::setUp();
  18. if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
  19. $this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
  20. }
  21. }
  22. public function testCompileWidget()
  23. {
  24. $arguments = new \Twig_Node(array(
  25. new \Twig_Node_Expression_Name('form', 0),
  26. ));
  27. $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
  28. $compiler = new \Twig_Compiler(new \Twig_Environment());
  29. $this->assertEquals(
  30. sprintf(
  31. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\')',
  32. $this->getVariableGetter('form')
  33. ),
  34. trim($compiler->compile($node)->getSource())
  35. );
  36. }
  37. public function testCompileWidgetWithVariables()
  38. {
  39. $arguments = new \Twig_Node(array(
  40. new \Twig_Node_Expression_Name('form', 0),
  41. new \Twig_Node_Expression_Array(array(
  42. new \Twig_Node_Expression_Constant('foo', 0),
  43. new \Twig_Node_Expression_Constant('bar', 0),
  44. ), 0),
  45. ));
  46. $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
  47. $compiler = new \Twig_Compiler(new \Twig_Environment());
  48. $this->assertEquals(
  49. sprintf(
  50. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
  51. $this->getVariableGetter('form')
  52. ),
  53. trim($compiler->compile($node)->getSource())
  54. );
  55. }
  56. public function testCompileLabelWithLabel()
  57. {
  58. $arguments = new \Twig_Node(array(
  59. new \Twig_Node_Expression_Name('form', 0),
  60. new \Twig_Node_Expression_Constant('my label', 0),
  61. ));
  62. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  63. $compiler = new \Twig_Compiler(new \Twig_Environment());
  64. $this->assertEquals(
  65. sprintf(
  66. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
  67. $this->getVariableGetter('form')
  68. ),
  69. trim($compiler->compile($node)->getSource())
  70. );
  71. }
  72. public function testCompileLabelWithNullLabel()
  73. {
  74. $arguments = new \Twig_Node(array(
  75. new \Twig_Node_Expression_Name('form', 0),
  76. new \Twig_Node_Expression_Constant(null, 0),
  77. ));
  78. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  79. $compiler = new \Twig_Compiler(new \Twig_Environment());
  80. // "label" => null must not be included in the output!
  81. // Otherwise the default label is overwritten with null.
  82. $this->assertEquals(
  83. sprintf(
  84. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
  85. $this->getVariableGetter('form')
  86. ),
  87. trim($compiler->compile($node)->getSource())
  88. );
  89. }
  90. public function testCompileLabelWithEmptyStringLabel()
  91. {
  92. $arguments = new \Twig_Node(array(
  93. new \Twig_Node_Expression_Name('form', 0),
  94. new \Twig_Node_Expression_Constant('', 0),
  95. ));
  96. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  97. $compiler = new \Twig_Compiler(new \Twig_Environment());
  98. // "label" => null must not be included in the output!
  99. // Otherwise the default label is overwritten with null.
  100. $this->assertEquals(
  101. sprintf(
  102. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
  103. $this->getVariableGetter('form')
  104. ),
  105. trim($compiler->compile($node)->getSource())
  106. );
  107. }
  108. public function testCompileLabelWithDefaultLabel()
  109. {
  110. $arguments = new \Twig_Node(array(
  111. new \Twig_Node_Expression_Name('form', 0),
  112. ));
  113. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  114. $compiler = new \Twig_Compiler(new \Twig_Environment());
  115. $this->assertEquals(
  116. sprintf(
  117. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
  118. $this->getVariableGetter('form')
  119. ),
  120. trim($compiler->compile($node)->getSource())
  121. );
  122. }
  123. public function testCompileLabelWithAttributes()
  124. {
  125. $arguments = new \Twig_Node(array(
  126. new \Twig_Node_Expression_Name('form', 0),
  127. new \Twig_Node_Expression_Constant(null, 0),
  128. new \Twig_Node_Expression_Array(array(
  129. new \Twig_Node_Expression_Constant('foo', 0),
  130. new \Twig_Node_Expression_Constant('bar', 0),
  131. ), 0),
  132. ));
  133. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  134. $compiler = new \Twig_Compiler(new \Twig_Environment());
  135. // "label" => null must not be included in the output!
  136. // Otherwise the default label is overwritten with null.
  137. // https://github.com/symfony/symfony/issues/5029
  138. $this->assertEquals(
  139. sprintf(
  140. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
  141. $this->getVariableGetter('form')
  142. ),
  143. trim($compiler->compile($node)->getSource())
  144. );
  145. }
  146. public function testCompileLabelWithLabelAndAttributes()
  147. {
  148. $arguments = new \Twig_Node(array(
  149. new \Twig_Node_Expression_Name('form', 0),
  150. new \Twig_Node_Expression_Constant('value in argument', 0),
  151. new \Twig_Node_Expression_Array(array(
  152. new \Twig_Node_Expression_Constant('foo', 0),
  153. new \Twig_Node_Expression_Constant('bar', 0),
  154. new \Twig_Node_Expression_Constant('label', 0),
  155. new \Twig_Node_Expression_Constant('value in attributes', 0),
  156. ), 0),
  157. ));
  158. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  159. $compiler = new \Twig_Compiler(new \Twig_Environment());
  160. $this->assertEquals(
  161. sprintf(
  162. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
  163. $this->getVariableGetter('form')
  164. ),
  165. trim($compiler->compile($node)->getSource())
  166. );
  167. }
  168. public function testCompileLabelWithLabelThatEvaluatesToNull()
  169. {
  170. $arguments = new \Twig_Node(array(
  171. new \Twig_Node_Expression_Name('form', 0),
  172. new \Twig_Node_Expression_Conditional(
  173. // if
  174. new \Twig_Node_Expression_Constant(true, 0),
  175. // then
  176. new \Twig_Node_Expression_Constant(null, 0),
  177. // else
  178. new \Twig_Node_Expression_Constant(null, 0),
  179. 0
  180. ),
  181. ));
  182. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  183. $compiler = new \Twig_Compiler(new \Twig_Environment());
  184. // "label" => null must not be included in the output!
  185. // Otherwise the default label is overwritten with null.
  186. // https://github.com/symfony/symfony/issues/5029
  187. $this->assertEquals(
  188. sprintf(
  189. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
  190. $this->getVariableGetter('form')
  191. ),
  192. trim($compiler->compile($node)->getSource())
  193. );
  194. }
  195. public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
  196. {
  197. $arguments = new \Twig_Node(array(
  198. new \Twig_Node_Expression_Name('form', 0),
  199. new \Twig_Node_Expression_Conditional(
  200. // if
  201. new \Twig_Node_Expression_Constant(true, 0),
  202. // then
  203. new \Twig_Node_Expression_Constant(null, 0),
  204. // else
  205. new \Twig_Node_Expression_Constant(null, 0),
  206. 0
  207. ),
  208. new \Twig_Node_Expression_Array(array(
  209. new \Twig_Node_Expression_Constant('foo', 0),
  210. new \Twig_Node_Expression_Constant('bar', 0),
  211. new \Twig_Node_Expression_Constant('label', 0),
  212. new \Twig_Node_Expression_Constant('value in attributes', 0),
  213. ), 0),
  214. ));
  215. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  216. $compiler = new \Twig_Compiler(new \Twig_Environment());
  217. // "label" => null must not be included in the output!
  218. // Otherwise the default label is overwritten with null.
  219. // https://github.com/symfony/symfony/issues/5029
  220. $this->assertEquals(
  221. sprintf(
  222. '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
  223. $this->getVariableGetter('form')
  224. ),
  225. trim($compiler->compile($node)->getSource())
  226. );
  227. }
  228. protected function getVariableGetter($name)
  229. {
  230. if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
  231. return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
  232. }
  233. return sprintf('$this->getContext($context, "%s")', $name);
  234. }
  235. }