Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Node\DumpNode;
use Twig\Compiler;
use Twig\Environment;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
class DumpNodeTest extends TestCase
{
public function testNoVar()
{
$node = new DumpNode('bar', null, 7);
$env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
$compiler = new Compiler($env);
$expected = <<<'EOTXT'
if ($this->env->isDebug()) {
$barvars = array();
foreach ($context as $barkey => $barval) {
if (!$barval instanceof \Twig\Template) {
$barvars[$barkey] = $barval;
}
}
// line 7
\Symfony\Component\VarDumper\VarDumper::dump($barvars);
}
EOTXT;
$this->assertSame($expected, $compiler->compile($node)->getSource());
}
public function testIndented()
{
$node = new DumpNode('bar', null, 7);
$env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
$compiler = new Compiler($env);
$expected = <<<'EOTXT'
if ($this->env->isDebug()) {
$barvars = array();
foreach ($context as $barkey => $barval) {
if (!$barval instanceof \Twig\Template) {
$barvars[$barkey] = $barval;
}
}
// line 7
\Symfony\Component\VarDumper\VarDumper::dump($barvars);
}
EOTXT;
$this->assertSame($expected, $compiler->compile($node, 1)->getSource());
}
public function testOneVar()
{
$vars = new Node(array(
new NameExpression('foo', 7),
));
$node = new DumpNode('bar', $vars, 7);
$env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
$compiler = new Compiler($env);
$expected = <<<'EOTXT'
if ($this->env->isDebug()) {
// line 7
\Symfony\Component\VarDumper\VarDumper::dump(%foo%);
}
EOTXT;
if (\PHP_VERSION_ID >= 70000) {
$expected = preg_replace('/%(.*?)%/', '($context["$1"] ?? null)', $expected);
} else {
$expected = preg_replace('/%(.*?)%/', '(isset($context["$1"]) ? $context["$1"] : null)', $expected);
}
$this->assertSame($expected, $compiler->compile($node)->getSource());
}
public function testMultiVars()
{
$vars = new Node(array(
new NameExpression('foo', 7),
new NameExpression('bar', 7),
));
$node = new DumpNode('bar', $vars, 7);
$env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
$compiler = new Compiler($env);
$expected = <<<'EOTXT'
if ($this->env->isDebug()) {
// line 7
\Symfony\Component\VarDumper\VarDumper::dump(array(
"foo" => %foo%,
"bar" => %bar%,
));
}
EOTXT;
if (\PHP_VERSION_ID >= 70000) {
$expected = preg_replace('/%(.*?)%/', '($context["$1"] ?? null)', $expected);
} else {
$expected = preg_replace('/%(.*?)%/', '(isset($context["$1"]) ? $context["$1"] : null)', $expected);
}
$this->assertSame($expected, $compiler->compile($node)->getSource());
}
}

View File

@@ -0,0 +1,82 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Node\FormThemeNode;
use Twig\Compiler;
use Twig\Environment;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
class FormThemeTest extends TestCase
{
public function testConstructor()
{
$form = new NameExpression('form', 0);
$resources = new Node(array(
new ConstantExpression('tpl1', 0),
new ConstantExpression('tpl2', 0),
));
$node = new FormThemeNode($form, $resources, 0);
$this->assertEquals($form, $node->getNode('form'));
$this->assertEquals($resources, $node->getNode('resources'));
}
public function testCompile()
{
$form = new NameExpression('form', 0);
$resources = new ArrayExpression(array(
new ConstantExpression(0, 0),
new ConstantExpression('tpl1', 0),
new ConstantExpression(1, 0),
new ConstantExpression('tpl2', 0),
), 0);
$node = new FormThemeNode($form, $resources, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(%s, array(0 => "tpl1", 1 => "tpl2"));',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
$resources = new ConstantExpression('tpl1', 0);
$node = new FormThemeNode($form, $resources, 0);
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->setTheme(%s, "tpl1");',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
protected function getVariableGetter($name)
{
if (\PHP_VERSION_ID >= 70000) {
return sprintf('($context["%s"] ?? null)', $name);
}
return sprintf('(isset($context["%s"]) ? $context["%1$s"] : null)', $name);
}
}

View File

@@ -0,0 +1,280 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;
use Twig\Compiler;
use Twig\Environment;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
class SearchAndRenderBlockNodeTest extends TestCase
{
public function testCompileWidget()
{
$arguments = new Node(array(
new NameExpression('form', 0),
));
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'widget\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileWidgetWithVariables()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ArrayExpression(array(
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabel()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ConstantExpression('my label', 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithNullLabel()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ConstantExpression(null, 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithEmptyStringLabel()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ConstantExpression('', 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
// "label" => null must not be included in the output!
// Otherwise the default label is overwritten with null.
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithDefaultLabel()
{
$arguments = new Node(array(
new NameExpression('form', 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithAttributes()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ConstantExpression(null, 0),
new ArrayExpression(array(
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
// "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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabelAndAttributes()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ConstantExpression('value in argument', 0),
new ArrayExpression(array(
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
new ConstantExpression('label', 0),
new ConstantExpression('value in attributes', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
$this->assertEquals(
sprintf(
'$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => "value in argument"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
public function testCompileLabelWithLabelThatEvaluatesToNull()
{
$arguments = new Node(array(
new NameExpression('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
// then
new ConstantExpression(null, 0),
// else
new ConstantExpression(null, 0),
0
),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
// "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->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->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 Node(array(
new NameExpression('form', 0),
new ConditionalExpression(
// if
new ConstantExpression(true, 0),
// then
new ConstantExpression(null, 0),
// else
new ConstantExpression(null, 0),
0
),
new ArrayExpression(array(
new ConstantExpression('foo', 0),
new ConstantExpression('bar', 0),
new ConstantExpression('label', 0),
new ConstantExpression('value in attributes', 0),
), 0),
));
$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);
$compiler = new Compiler(new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock()));
// "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->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_)))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}
protected function getVariableGetter($name)
{
if (\PHP_VERSION_ID >= 70000) {
return sprintf('($context["%s"] ?? null)', $name);
}
return sprintf('(isset($context["%s"]) ? $context["%1$s"] : null)', $name);
}
}

View File

@@ -0,0 +1,66 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Node\TransNode;
use Twig\Compiler;
use Twig\Environment;
use Twig\Node\Expression\NameExpression;
use Twig\Node\TextNode;
/**
* @author Asmir Mustafic <goetas@gmail.com>
*/
class TransNodeTest extends TestCase
{
public function testCompileStrict()
{
$body = new TextNode('trans %var%', 0);
$vars = new NameExpression('foo', 0);
$node = new TransNode($body, null, null, $vars);
$env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array('strict_variables' => true));
$compiler = new Compiler($env);
$this->assertEquals(
sprintf(
'echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->getTranslator()->trans("trans %%var%%", array_merge(array("%%var%%" => %s), %s), "messages");',
$this->getVariableGetterWithoutStrictCheck('var'),
$this->getVariableGetterWithStrictCheck('foo')
),
trim($compiler->compile($node)->getSource())
);
}
protected function getVariableGetterWithoutStrictCheck($name)
{
if (\PHP_VERSION_ID >= 70000) {
return sprintf('($context["%s"] ?? null)', $name);
}
return sprintf('(isset($context["%s"]) ? $context["%1$s"] : null)', $name);
}
protected function getVariableGetterWithStrictCheck($name)
{
if (Environment::MAJOR_VERSION >= 2) {
return sprintf('(isset($context["%s"]) || array_key_exists("%1$s", $context) ? $context["%1$s"] : (function () { throw new Twig_Error_Runtime(\'Variable "%1$s" does not exist.\', 0, $this->getSourceContext()); })())', $name);
}
if (\PHP_VERSION_ID >= 70000) {
return sprintf('($context["%s"] ?? $this->getContext($context, "%1$s"))', $name);
}
return sprintf('(isset($context["%s"]) ? $context["%1$s"] : $this->getContext($context, "%1$s"))', $name);
}
}