From 1e5dea44f954f5f495ff7e5133f4aa91b0b7dd93 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 2 Sep 2024 18:18:00 +0200 Subject: [PATCH 1/3] Fix MacroTest --- tests/Node/MacroTest.php | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index ef36774f7..f05adf636 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -36,8 +36,6 @@ class MacroTest extends NodeTestCase public function getTests() { - $tests = []; - $arguments = new Node([ 'foo' => new ConstantExpression(null, 1), 'bar' => new ConstantExpression('Foo', 1), @@ -46,7 +44,7 @@ class MacroTest extends NodeTestCase $body = new BodyNode([new TextNode('foo', 1)]); $node = new MacroNode('foo', $body, $arguments, 1); - $text[] = [$node, << [$node, <<env->getCharset()); + })(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); } EOF - , new Environment(new ArrayLoader()), + , new Environment(new ArrayLoader(), ['use_yield' => true]), ]; - return $tests; + yield 'with use_yield = false' => [$node, <<macros; + \$context = \$this->env->mergeGlobals([ + "foo" => \$__foo__, + "bar" => \$__bar__, + "varargs" => \$__varargs__, + ]); + + \$blocks = []; + + return ('' === \$tmp = \\Twig\\Extension\\CoreExtension::captureOutput((function () use (&\$context, \$macros, \$blocks) { + yield "foo"; + return; yield ''; + })())) ? '' : new Markup(\$tmp, \$this->env->getCharset()); +} +EOF + , new Environment(new ArrayLoader(), ['use_yield' => false]), + ]; } } From 3a4d513ffed965a2898375147a0b877c84d9d721 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 2 Sep 2024 17:04:53 +0200 Subject: [PATCH 2/3] Fix testExtensionsAreNotInitializedWhenRenderingACompiledTemplate --- tests/EnvironmentTest.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index 9338e78f2..a5f082ecb 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -178,19 +178,26 @@ class EnvironmentTest extends TestCase // force compilation $twig = new Environment($loader = new ArrayLoader(['index' => '{{ foo }}']), $options); + $twig->addExtension($extension = new class extends AbstractExtension { + public bool $throw = false; + + public function getFilters(): array + { + if ($this->throw) { + throw new \RuntimeException('Extension are not supposed to be initialized.'); + } + + return parent::getFilters(); + } + }); $key = $cache->generateKey('index', $twig->getTemplateClass('index')); $cache->write($key, $twig->compileSource(new Source('{{ foo }}', 'index'))); // check that extensions won't be initialized when rendering a template that is already in the cache - $twig = $this - ->getMockBuilder(Environment::class) - ->setConstructorArgs([$loader, $options]) - ->setMethods(['initExtensions']) - ->getMock() - ; - - $twig->expects($this->never())->method('initExtensions'); + $twig = new Environment($loader, $options); + $extension->throw = true; + $twig->addExtension($extension); // render template $output = $twig->render('index', ['foo' => 'bar']); From 09a43a9f6fff2fc703e62e3555d10df0e5beffae Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Tue, 3 Sep 2024 09:54:27 +0200 Subject: [PATCH 3/3] Replace `return; yield` with `yield from []` This has the same effect, but looks less hacky and makes PHPStan happy. https://phpstan.org/r/df7fcc88-1df8-428e-b675-5dc6965c34d6 Previously this was needed to work properly with output capturing. --- src/Node/BlockNode.php | 2 +- src/Node/CaptureNode.php | 2 +- src/Node/ModuleNode.php | 2 +- tests/Node/BlockTest.php | 2 +- tests/Node/MacroTest.php | 4 ++-- tests/Node/ModuleTest.php | 2 +- tests/Node/SetTest.php | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Node/BlockNode.php b/src/Node/BlockNode.php index 2ee74a8d2..3c06f155b 100644 --- a/src/Node/BlockNode.php +++ b/src/Node/BlockNode.php @@ -42,7 +42,7 @@ class BlockNode extends Node $compiler ->subcompile($this->getNode('body')) - ->write("return; yield '';\n") // needed when body doesn't yield anything + ->write("yield from [];\n") ->outdent() ->write("}\n\n") ; diff --git a/src/Node/CaptureNode.php b/src/Node/CaptureNode.php index 0162113c1..3b7f0b6d8 100644 --- a/src/Node/CaptureNode.php +++ b/src/Node/CaptureNode.php @@ -39,7 +39,7 @@ class CaptureNode extends Node ->raw("(function () use (&\$context, \$macros, \$blocks) {\n") ->indent() ->subcompile($this->getNode('body')) - ->write("return; yield '';\n") + ->write("yield from [];\n") ->outdent() ->write('})()') ; diff --git a/src/Node/ModuleNode.php b/src/Node/ModuleNode.php index 264a0e67f..b57e643e8 100644 --- a/src/Node/ModuleNode.php +++ b/src/Node/ModuleNode.php @@ -353,7 +353,7 @@ final class ModuleNode extends Node $compiler->subcompile($this->getNode('display_end')); if (!$this->hasNode('parent')) { - $compiler->write("return; yield '';\n"); // ensure at least one yield call even for templates with no output + $compiler->write("yield from [];\n"); } $compiler diff --git a/tests/Node/BlockTest.php b/tests/Node/BlockTest.php index 02de54b4a..06405d1c3 100644 --- a/tests/Node/BlockTest.php +++ b/tests/Node/BlockTest.php @@ -40,7 +40,7 @@ public function block_foo(array \$context, array \$blocks = []): iterable { \$macros = \$this->macros; yield "foo"; - return; yield ''; + yield from []; } EOF , new Environment(new ArrayLoader()), diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index f05adf636..405046b37 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -59,7 +59,7 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) return ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) { yield "foo"; - return; yield ''; + yield from []; })(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); } EOF @@ -81,7 +81,7 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) return ('' === \$tmp = \\Twig\\Extension\\CoreExtension::captureOutput((function () use (&\$context, \$macros, \$blocks) { yield "foo"; - return; yield ''; + yield from []; })())) ? '' : new Markup(\$tmp, \$this->env->getCharset()); } EOF diff --git a/tests/Node/ModuleTest.php b/tests/Node/ModuleTest.php index 0b7dd5ddb..10c4d7fa5 100644 --- a/tests/Node/ModuleTest.php +++ b/tests/Node/ModuleTest.php @@ -103,7 +103,7 @@ class __TwigTemplate_%x extends Template \$macros = \$this->macros; // line 1 yield "foo"; - return; yield ''; + yield from []; } /** diff --git a/tests/Node/SetTest.php b/tests/Node/SetTest.php index f250b80ee..9bb93a367 100644 --- a/tests/Node/SetTest.php +++ b/tests/Node/SetTest.php @@ -57,7 +57,7 @@ EOF // line 1 \$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) { yield "foo"; - return; yield ''; + yield from []; })(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); EOF , new Environment(new ArrayLoader()), @@ -67,7 +67,7 @@ EOF // line 1 $context["foo"] = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros, $blocks) { yield "foo"; - return; yield ''; + yield from []; })())) ? '' : new Markup($tmp, $this->env->getCharset()); EOF , new Environment(new ArrayLoader()),