2012-07-25 12:20:23 +01:00
|
|
|
<?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\Node\SearchAndRenderBlockNode;
|
|
|
|
|
2013-09-17 15:25:42 +01:00
|
|
|
class SearchAndRenderBlockNodeTest extends \PHPUnit_Framework_TestCase
|
2012-07-25 12:20:23 +01:00
|
|
|
{
|
|
|
|
public function testCompileWidget()
|
|
|
|
{
|
|
|
|
$arguments = new \Twig_Node(array(
|
|
|
|
new \Twig_Node_Expression_Name('form', 0),
|
|
|
|
));
|
|
|
|
|
|
|
|
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'widget\')',
|
2012-07-25 12:20:23 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
|
2012-07-25 12:20:23 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
|
2012-07-25 12:20:23 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
2012-07-25 18:03:07 +01:00
|
|
|
// "label" => null must not be included in the output!
|
|
|
|
// Otherwise the default label is overwritten with null.
|
2012-07-25 12:20:23 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\')',
|
2012-07-25 18:03:07 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 18:03:07 +01:00
|
|
|
|
|
|
|
// "label" => null must not be included in the output!
|
|
|
|
// Otherwise the default label is overwritten with null.
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\')',
|
2012-07-25 12:20:23 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\')',
|
2012-07-25 12:20:23 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
2012-07-25 18:03:07 +01:00
|
|
|
// "label" => null must not be included in the output!
|
|
|
|
// Otherwise the default label is overwritten with null.
|
|
|
|
// https://github.com/symfony/symfony/issues/5029
|
2012-07-25 12:20:23 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
|
2012-07-25 12:20:23 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 12:20:23 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
sprintf(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
|
2012-07-25 18:03:07 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 18:03:07 +01:00
|
|
|
|
|
|
|
// "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(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
|
2012-07-25 18:03:07 +01:00
|
|
|
$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);
|
|
|
|
|
2015-08-13 08:59:41 +01:00
|
|
|
$compiler = new \Twig_Compiler(new \Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
2012-07-25 18:03:07 +01:00
|
|
|
|
|
|
|
// "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(
|
2016-09-28 19:43:20 +01:00
|
|
|
'$this->env->getExtension(\'Symfony\Bridge\Twig\Extension\FormExtension\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in attributes") + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? array() : array("label" => $_label_)))',
|
2012-07-25 12:20:23 +01:00
|
|
|
$this->getVariableGetter('form')
|
|
|
|
),
|
|
|
|
trim($compiler->compile($node)->getSource())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getVariableGetter($name)
|
|
|
|
{
|
2014-11-17 14:32:09 +00:00
|
|
|
if (PHP_VERSION_ID >= 50400) {
|
2012-07-25 12:20:23 +01:00
|
|
|
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('$this->getContext($context, "%s")', $name);
|
|
|
|
}
|
|
|
|
}
|