123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?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\Bridge\Twig\Tests\Node;
- use Symfony\Bridge\Twig\Tests\TestCase;
- use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
- class SearchAndRenderBlockNodeTest extends TestCase
- {
- protected function setUp()
- {
- parent::setUp();
- if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
- $this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
- }
- }
- public function testCompileWidget()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- ));
- $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\')',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileWidgetWithVariables()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Array(array(
- new \Twig_Node_Expression_Constant('foo', 0),
- new \Twig_Node_Expression_Constant('bar', 0),
- ), 0),
- ));
- $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithLabel()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Constant('my label', 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithNullLabel()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Constant(null, 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- // "label" => null must not be included in the output!
- // Otherwise the default label is overwritten with null.
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithEmptyStringLabel()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Constant('', 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- // "label" => null must not be included in the output!
- // Otherwise the default label is overwritten with null.
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithDefaultLabel()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithAttributes()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Constant(null, 0),
- new \Twig_Node_Expression_Array(array(
- new \Twig_Node_Expression_Constant('foo', 0),
- new \Twig_Node_Expression_Constant('bar', 0),
- ), 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- // "label" => null must not be included in the output!
- // Otherwise the default label is overwritten with null.
- // https://github.com/symfony/symfony/issues/5029
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithLabelAndAttributes()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Constant('value in argument', 0),
- new \Twig_Node_Expression_Array(array(
- new \Twig_Node_Expression_Constant('foo', 0),
- new \Twig_Node_Expression_Constant('bar', 0),
- new \Twig_Node_Expression_Constant('label', 0),
- new \Twig_Node_Expression_Constant('value in attributes', 0),
- ), 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithLabelThatEvaluatesToNull()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Conditional(
- // if
- new \Twig_Node_Expression_Constant(true, 0),
- // then
- new \Twig_Node_Expression_Constant(null, 0),
- // else
- new \Twig_Node_Expression_Constant(null, 0),
- 0
- ),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- // "label" => null must not be included in the output!
- // Otherwise the default label is overwritten with null.
- // https://github.com/symfony/symfony/issues/5029
- $this->assertEquals(
- sprintf(
- '$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes()
- {
- $arguments = new \Twig_Node(array(
- new \Twig_Node_Expression_Name('form', 0),
- new \Twig_Node_Expression_Conditional(
- // if
- new \Twig_Node_Expression_Constant(true, 0),
- // then
- new \Twig_Node_Expression_Constant(null, 0),
- // else
- new \Twig_Node_Expression_Constant(null, 0),
- 0
- ),
- new \Twig_Node_Expression_Array(array(
- new \Twig_Node_Expression_Constant('foo', 0),
- new \Twig_Node_Expression_Constant('bar', 0),
- new \Twig_Node_Expression_Constant('label', 0),
- new \Twig_Node_Expression_Constant('value in attributes', 0),
- ), 0),
- ));
- $node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
- $compiler = new \Twig_Compiler(new \Twig_Environment());
- // "label" => null must not be included in the output!
- // Otherwise the default label is overwritten with null.
- // https://github.com/symfony/symfony/issues/5029
- $this->assertEquals(
- sprintf(
- '$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_)))',
- $this->getVariableGetter('form')
- ),
- trim($compiler->compile($node)->getSource())
- );
- }
- protected function getVariableGetter($name)
- {
- if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
- return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
- }
- return sprintf('$this->getContext($context, "%s")', $name);
- }
- }
|