Files
Fabien Potencier 0b6e824ea8 Merge branch '3.x' into 4.x
* 3.x:
  Add void return type hint even in tests
  Run php-cs-fixer sequentially so the void_return src-only customiser is applied
  Fix CHANGELOG
  [Intl] Add format_list filter using PHP 8.5's IntlListFormatter

# Conflicts:
#	.github/workflows/ci.yml
#	CHANGELOG
#	extra/cssinliner-extra/Tests/LegacyFunctionsTest.php
#	extra/html-extra/Tests/CvaTest.php
#	extra/html-extra/Tests/HtmlAttrMergeTest.php
#	extra/html-extra/Tests/HtmlAttrTest.php
#	extra/html-extra/Tests/LegacyFunctionsTest.php
#	extra/inky-extra/Tests/LegacyFunctionsTest.php
#	extra/markdown-extra/Tests/FunctionalTest.php
#	extra/markdown-extra/Tests/LegacyFunctionsTest.php
#	extra/twig-extra-bundle/DependencyInjection/Compiler/MissingExtensionSuggestorPass.php
#	extra/twig-extra-bundle/DependencyInjection/TwigExtraExtension.php
#	extra/twig-extra-bundle/TwigExtraBundle.php
#	src/Extension/CoreExtension.php
#	src/Extension/EscaperExtension.php
#	src/Node/CheckSecurityCallNode.php
#	src/Node/Expression/FunctionExpression.php
#	src/Node/ModuleNode.php
#	src/Node/Node.php
#	src/Node/TypesNode.php
#	src/Resources/core.php
#	src/Resources/debug.php
#	src/Test/IntegrationTestCase.php
#	tests/CustomExtensionTest.php
#	tests/EnvironmentTest.php
#	tests/ExpressionParserTest.php
#	tests/Extension/CoreTest.php
#	tests/Extension/EscaperTest.php
#	tests/Extension/LegacyDebugFunctionsTest.php
#	tests/Extension/LegacyStringLoaderFunctionsTest.php
#	tests/Extension/SandboxStateChangeTest.php
#	tests/Extension/SandboxTest.php
#	tests/LexerTest.php
#	tests/Node/Expression/CallTest.php
#	tests/Node/Expression/ConditionalTest.php
#	tests/Node/NodeTest.php
#	tests/Resources/LegacyCoreTest.php
#	tests/TemplateTest.php
#	tests/Util/CallableArgumentsExtractorTest.php
2026-07-12 13:55:37 +02:00

158 lines
5.7 KiB
PHP

<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Tests\NodeVisitor;
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Expression\BlockReferenceExpression;
use Twig\Node\Expression\ParentExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\ForNode;
use Twig\Node\Node;
use Twig\NodeVisitor\OptimizerNodeVisitor;
use Twig\Source;
class OptimizerTest extends TestCase
{
public function testConstructor(): void
{
$this->expectNotToPerformAssertions();
new OptimizerNodeVisitor(OptimizerNodeVisitor::OPTIMIZE_FOR);
}
public function testRenderBlockOptimizer(): void
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$stream = $env->parse($env->tokenize(new Source('{{ block("foo") }}', 'index')));
$node = $stream->getNode('body')->getNode(0);
$this->assertInstanceOf(BlockReferenceExpression::class, $node);
$this->assertTrue($node->getAttribute('output'));
}
public function testRenderParentBlockOptimizer(): void
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$stream = $env->parse($env->tokenize(new Source('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index')));
$node = $stream->getNode('blocks')->getNode('content')->getNode(0)->getNode('body');
$this->assertInstanceOf(ParentExpression::class, $node);
$this->assertTrue($node->getAttribute('output'));
}
public function testForVarOptimizer(): void
{
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);
$template = '{% for i, j in foo %}{{ loop.index }}{{ i }}{{ j }}{% endfor %}';
$stream = $env->parse($env->tokenize(new Source($template, 'index')));
foreach (['loop', 'i', 'j'] as $target) {
$this->checkForVarConfiguration($stream, $target);
}
}
public function checkForVarConfiguration(Node $node, $target): void
{
foreach ($node as $n) {
if (ContextVariable::class === $n::class && $target === $n->getAttribute('name')) {
$this->assertTrue($n->getAttribute('always_defined'));
} else {
$this->checkForVarConfiguration($n, $target);
}
}
}
#[DataProvider('getTestsForForLoopOptimizer')]
public function testForLoopOptimizer($template, $expected): void
{
$env = new Environment(new ArrayLoader(), ['cache' => false]);
$stream = $env->parse($env->tokenize(new Source($template, 'index')));
foreach ($expected as $target => $withLoop) {
$this->assertTrue($this->checkForLoopConfiguration($stream, $target, $withLoop), \sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
}
}
public static function getTestsForForLoopOptimizer()
{
return [
['{% for i in foo %}{% endfor %}', ['i' => false]],
['{% for i in foo %}{{ loop.index }}{% endfor %}', ['i' => true]],
['{% for i in foo %}{% for j in foo %}{% endfor %}{% endfor %}', ['i' => false, 'j' => false]],
['{% for i in foo %}{% include "foo" %}{% endfor %}', ['i' => true]],
['{% for i in foo %}{% include "foo" only %}{% endfor %}', ['i' => false]],
['{% for i in foo %}{% include "foo" with { "foo": "bar" } only %}{% endfor %}', ['i' => false]],
['{% for i in foo %}{% include "foo" with { "foo": loop.index } only %}{% endfor %}', ['i' => true]],
['{% for i in foo %}{% for j in foo %}{{ loop.index }}{% endfor %}{% endfor %}', ['i' => false, 'j' => true]],
['{% for i in foo %}{% for j in foo %}{{ loop.parent.loop.index }}{% endfor %}{% endfor %}', ['i' => true, 'j' => true]],
['{% for i in foo %}{% set l = loop %}{% for j in foo %}{{ l.index }}{% endfor %}{% endfor %}', ['i' => true, 'j' => false]],
['{% for i in foo %}{% for j in foo %}{{ foo.parent.loop.index }}{% endfor %}{% endfor %}', ['i' => false, 'j' => false]],
['{% for i in foo %}{% for j in foo %}{{ loop["parent"].loop.index }}{% endfor %}{% endfor %}', ['i' => true, 'j' => true]],
['{% for i in foo %}{{ include("foo") }}{% endfor %}', ['i' => true]],
['{% for i in foo %}{{ include("foo", with_context = false) }}{% endfor %}', ['i' => false]],
['{% for i in foo %}{{ include("foo", with_context = true) }}{% endfor %}', ['i' => true]],
['{% for i in foo %}{{ include("foo", { "foo": "bar" }, with_context = false) }}{% endfor %}', ['i' => false]],
['{% for i in foo %}{{ include("foo", { "foo": loop.index }, with_context = false) }}{% endfor %}', ['i' => true]],
];
}
public function checkForLoopConfiguration(Node $node, $target, $withLoop)
{
foreach ($node as $n) {
if ($n instanceof ForNode) {
if ($target === $n->getNode('value_target')->getAttribute('name')) {
return $withLoop == $n->getAttribute('with_loop');
}
}
$ret = $this->checkForLoopConfiguration($n, $target, $withLoop);
if (null !== $ret) {
return $ret;
}
}
}
}