SearchAndRenderBlockNodeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
  13. use Twig\Compiler;
  14. use Twig\Environment;
  15. use Twig\Node\Expression\ArrayExpression;
  16. use Twig\Node\Expression\ConditionalExpression;
  17. use Twig\Node\Expression\ConstantExpression;
  18. use Twig\Node\Expression\NameExpression;
  19. use Twig\Node\Node;
  20. class SearchAndRenderBlockNodeTest extends TestCase
  21. {
  22. public function testCompileWidget()
  23. {
  24. $arguments = new Node(array(
  25. new NameExpression('form', 0),
  26. ));
  27. $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
  28. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  29. $this->assertEquals(
  30. sprintf(
  31. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'widget\')',
  32. $this->getVariableGetter('form')
  33. ),
  34. trim($compiler->compile($node)->getSource())
  35. );
  36. }
  37. public function testCompileWidgetWithVariables()
  38. {
  39. $arguments = new Node(array(
  40. new NameExpression('form', 0),
  41. new ArrayExpression(array(
  42. new ConstantExpression('foo', 0),
  43. new ConstantExpression('bar', 0),
  44. ), 0),
  45. ));
  46. $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
  47. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  48. $this->assertEquals(
  49. sprintf(
  50. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 Node(array(
  59. new NameExpression('form', 0),
  60. new ConstantExpression('my label', 0),
  61. ));
  62. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  63. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  64. $this->assertEquals(
  65. sprintf(
  66. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 Node(array(
  75. new NameExpression('form', 0),
  76. new ConstantExpression(null, 0),
  77. ));
  78. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  79. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
  85. $this->getVariableGetter('form')
  86. ),
  87. trim($compiler->compile($node)->getSource())
  88. );
  89. }
  90. public function testCompileLabelWithEmptyStringLabel()
  91. {
  92. $arguments = new Node(array(
  93. new NameExpression('form', 0),
  94. new ConstantExpression('', 0),
  95. ));
  96. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  97. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
  103. $this->getVariableGetter('form')
  104. ),
  105. trim($compiler->compile($node)->getSource())
  106. );
  107. }
  108. public function testCompileLabelWithDefaultLabel()
  109. {
  110. $arguments = new Node(array(
  111. new NameExpression('form', 0),
  112. ));
  113. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  114. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  115. $this->assertEquals(
  116. sprintf(
  117. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
  118. $this->getVariableGetter('form')
  119. ),
  120. trim($compiler->compile($node)->getSource())
  121. );
  122. }
  123. public function testCompileLabelWithAttributes()
  124. {
  125. $arguments = new Node(array(
  126. new NameExpression('form', 0),
  127. new ConstantExpression(null, 0),
  128. new ArrayExpression(array(
  129. new ConstantExpression('foo', 0),
  130. new ConstantExpression('bar', 0),
  131. ), 0),
  132. ));
  133. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  134. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 Node(array(
  149. new NameExpression('form', 0),
  150. new ConstantExpression('value in argument', 0),
  151. new ArrayExpression(array(
  152. new ConstantExpression('foo', 0),
  153. new ConstantExpression('bar', 0),
  154. new ConstantExpression('label', 0),
  155. new ConstantExpression('value in attributes', 0),
  156. ), 0),
  157. ));
  158. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  159. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  160. $this->assertEquals(
  161. sprintf(
  162. '$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 Node(array(
  171. new NameExpression('form', 0),
  172. new ConditionalExpression(
  173. // if
  174. new ConstantExpression(true, 0),
  175. // then
  176. new ConstantExpression(null, 0),
  177. // else
  178. new ConstantExpression(null, 0),
  179. 0
  180. ),
  181. ));
  182. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  183. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 Node(array(
  198. new NameExpression('form', 0),
  199. new ConditionalExpression(
  200. // if
  201. new ConstantExpression(true, 0),
  202. // then
  203. new ConstantExpression(null, 0),
  204. // else
  205. new ConstantExpression(null, 0),
  206. 0
  207. ),
  208. new ArrayExpression(array(
  209. new ConstantExpression('foo', 0),
  210. new ConstantExpression('bar', 0),
  211. new ConstantExpression('label', 0),
  212. new ConstantExpression('value in attributes', 0),
  213. ), 0),
  214. ));
  215. $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
  216. $compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
  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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 (\PHP_VERSION_ID >= 70000) {
  231. return sprintf('($context["%s"] ?? null)', $name, $name);
  232. }
  233. return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
  234. }
  235. }