From 79b509c4aad24ebdb1ad780cd12441ccf8827af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Tue, 21 May 2024 14:01:24 +0200 Subject: [PATCH 1/2] Support yield rendering strategy in Twig 3.9+ --- .../Templating/Twig/Node/DefaultContext.php | 2 + .../Templating/Twig/Node/RenderZone.php | 17 +++++- .../Twig/Runtime/HelpersRuntime.php | 8 ++- .../Twig/Runtime/RenderingRuntime.php | 6 +-- composer.json | 2 +- phpstan.neon | 5 +- .../Extension/RenderingExtensionTwigTest.php | 6 +-- .../Twig/Node/DefaultContextTest.php | 8 +++ .../Templating/Twig/Node/RenderZoneTest.php | 17 ++++-- .../Twig/Runtime/RenderingRuntimeTest.php | 53 +++++++++++++++++++ 10 files changed, 106 insertions(+), 18 deletions(-) diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php index 4f688e000..2110eeeac 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php @@ -4,10 +4,12 @@ namespace Netgen\Bundle\LayoutsBundle\Templating\Twig\Node; +use Twig\Attribute\YieldReady; use Twig\Compiler; use Twig\Node\Expression\AbstractExpression; use Twig\Node\Node; +#[YieldReady] final class DefaultContext extends Node { public function __construct(AbstractExpression $expr, int $line = 0, ?string $tag = null) diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php index bd721e308..3ddeca6b1 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php @@ -8,12 +8,15 @@ use Netgen\Layouts\API\Values\Layout\Zone; use Netgen\Layouts\View\Twig\ContextualizedTwigTemplate; use Netgen\Layouts\View\ViewInterface; +use Twig\Attribute\YieldReady; use Twig\Compiler; +use Twig\Environment; use Twig\Node\Expression\AbstractExpression; use Twig\Node\Node; use const PHP_EOL; +#[YieldReady] final class RenderZone extends Node { public function __construct(AbstractExpression $zone, ?AbstractExpression $context = null, int $line = 0, ?string $tag = null) @@ -36,9 +39,8 @@ public function compile(Compiler $compiler): void ->write('$nglZoneIdentifier = $nglZone instanceof ' . Zone::class . ' ? $nglZone->getIdentifier() : $nglZone;' . PHP_EOL); $this->compileContextNode($compiler); - $compiler->write('$nglTemplate = new ' . ContextualizedTwigTemplate::class . '($this, $context, $blocks);' . PHP_EOL); - $compiler->write('$this->env->getRuntime("' . RenderingRuntime::class . '")->displayZone($context["nglayouts"]->getLayout(), $nglZoneIdentifier, $nglContext, $nglTemplate);' . PHP_EOL); + $this->compileRenderNode($compiler); } /** @@ -62,4 +64,15 @@ private function compileContextNode(Compiler $compiler): void $compiler->write('$nglContext = ' . ViewInterface::class . '::CONTEXT_DEFAULT;' . PHP_EOL); } + + private function compileRenderNode(Compiler $compiler): void + { + if (Environment::VERSION_ID >= 30900) { + $compiler->write('yield $this->env->getRuntime("' . RenderingRuntime::class . '")->renderZone($context["nglayouts"]->getLayout(), $nglZoneIdentifier, $nglContext, $nglTemplate);' . PHP_EOL); + + return; + } + + $compiler->write('echo $this->env->getRuntime("' . RenderingRuntime::class . '")->renderZone($context["nglayouts"]->getLayout(), $nglZoneIdentifier, $nglContext, $nglTemplate);' . PHP_EOL); + } } diff --git a/bundles/LayoutsBundle/Templating/Twig/Runtime/HelpersRuntime.php b/bundles/LayoutsBundle/Templating/Twig/Runtime/HelpersRuntime.php index 0a3a906e3..1cac5a80a 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Runtime/HelpersRuntime.php +++ b/bundles/LayoutsBundle/Templating/Twig/Runtime/HelpersRuntime.php @@ -19,9 +19,11 @@ use Ramsey\Uuid\Uuid; use Throwable; use Twig\Environment; +use Twig\Extension\CoreExtension; use function array_unshift; use function is_string; +use function method_exists; use function twig_date_converter; final class HelpersRuntime @@ -121,7 +123,11 @@ public function getCountryFlag(string $countryCode): string */ public function formatDateTime(Environment $twig, $dateTime, string $dateFormat = 'medium', string $timeFormat = 'medium'): string { - $dateTime = twig_date_converter($twig, $dateTime); + $coreExtension = $twig->getExtension(CoreExtension::class); + + $dateTime = method_exists($coreExtension, 'convertDate') ? + $coreExtension->convertDate($dateTime) : + twig_date_converter($twig, $dateTime); $formatValues = [ 'none' => IntlDateFormatter::NONE, diff --git a/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntime.php b/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntime.php index 44b874700..4603ea469 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntime.php +++ b/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntime.php @@ -159,9 +159,9 @@ public function renderValue(array $context, $value, array $parameters = [], ?str } /** - * Displays the provided zone. + * Renders the provided zone. */ - public function displayZone(Layout $layout, string $zoneIdentifier, string $viewContext, ContextualizedTwigTemplate $twigTemplate): void + public function renderZone(Layout $layout, string $zoneIdentifier, string $viewContext, ContextualizedTwigTemplate $twigTemplate): string { $locales = null; @@ -178,7 +178,7 @@ public function displayZone(Layout $layout, string $zoneIdentifier, string $view $locales, ); - echo $this->renderValue( + return $this->renderValue( [], new ZoneReference($layout, $zoneIdentifier), [ diff --git a/composer.json b/composer.json index 02f5e0f03..6f44a617a 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "symfony/twig-bundle": "^3.4.47 || ^5.4 || ^6.2", "symfony/validator": "^3.4.47 || ^5.4 || ^6.2", "symfony/yaml": "^3.4.47 || ^5.4 || ^6.2", - "twig/twig": "^2.15 || ^3.4", + "twig/twig": "^2.15 || ^3.9", "sensio/framework-extra-bundle": "^5.4 || ^6.2", "doctrine/dbal": "^2.13 || ^3.5", "doctrine/doctrine-bundle": "^1.12 || ^2.7", diff --git a/phpstan.neon b/phpstan.neon index 559780df3..d92fd4f74 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,6 +7,7 @@ parameters: treatPhpDocTypesAsCertain: false dynamicConstantNames: - Symfony\Component\HttpKernel\Kernel::VERSION_ID + - Twig\Environment::VERSION_ID ignoreErrors: # Doctrine DBAL @@ -49,10 +50,6 @@ parameters: - "#Call to function method_exists\\(\\) with Symfony\\\\Component\\\\HttpFoundation\\\\Request and 'getContentTypeFormat' will always evaluate to true.#" - "#Call to function method_exists\\(\\) with Symfony\\\\Component\\\\DependencyInjection\\\\ContainerBuilder and 'registerAttributeFo…' will always evaluate to true.#" - - - message: '#twig_date_converter not found.#' - path: bundles/LayoutsBundle/Templating/Twig/Runtime/HelpersRuntime.php - # Netgen Layouts specifics - "#Call to function method_exists\\(\\) with Netgen\\\\Layouts\\\\Layout\\\\Resolver\\\\(Condition|Target)TypeInterface and '(export|import)' will always evaluate to true.#" diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Extension/RenderingExtensionTwigTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/Extension/RenderingExtensionTwigTest.php index 7a51ff55e..8a69f56bc 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Extension/RenderingExtensionTwigTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Extension/RenderingExtensionTwigTest.php @@ -54,7 +54,7 @@ protected function setUp(): void } /** - * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::displayZone + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::renderZone * * @dataProvider getTests * @@ -74,7 +74,7 @@ public function testIntegration($file, $message, $condition, $templates, $except } /** - * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::displayZone + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::renderZone * * @dataProvider getTests * @@ -97,7 +97,7 @@ public function testIntegrationWithLocale($file, $message, $condition, $template } /** - * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::displayZone + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::renderZone * * @dataProvider getLegacyTests * diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php index 050c0c841..eea50da23 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php @@ -5,6 +5,7 @@ namespace Netgen\Bundle\LayoutsBundle\Tests\Templating\Twig\Node; use Netgen\Bundle\LayoutsBundle\Templating\Twig\Node\DefaultContext; +use Twig\Environment; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\NameExpression; @@ -13,6 +14,13 @@ */ final class DefaultContextTest extends NodeTestBase { + protected function setUp(): void + { + if (Environment::MAJOR_VERSION === 2) { + self::markTestSkipped('Test requires twig/twig 3.9 to run'); + } + } + /** * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Node\DefaultContext::__construct */ diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php index ba5e24ae9..376f01e8a 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php @@ -9,15 +9,24 @@ use Netgen\Layouts\API\Values\Layout\Zone; use Netgen\Layouts\View\Twig\ContextualizedTwigTemplate; use Netgen\Layouts\View\ViewInterface; +use Twig\Environment; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\NameExpression; /** * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Node\RenderZone::compile * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Node\RenderZone::compileContextNode + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Node\RenderZone::compileRenderNode */ final class RenderZoneTest extends NodeTestBase { + protected function setUp(): void + { + if (Environment::MAJOR_VERSION === 2) { + self::markTestSkipped('Test requires twig/twig 3.9 to run'); + } + } + /** * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Node\RenderZone::__construct */ @@ -69,7 +78,7 @@ public static function getTests(): array \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; \$nglContext = {$contextNodeGetter}; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks); - \$this->env->getRuntime("{$runtimeClass}")->displayZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); + yield \$this->env->getRuntime("{$runtimeClass}")->renderZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); EOT, $environment, ], @@ -81,7 +90,7 @@ public static function getTests(): array \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; \$nglContext = {$contextNodeGetter}; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks); - \$this->env->getRuntime("{$runtimeClass}")->displayZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); + yield \$this->env->getRuntime("{$runtimeClass}")->renderZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); EOT, $environment, ], @@ -93,7 +102,7 @@ public static function getTests(): array \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; \$nglContext = {$viewInterface}::CONTEXT_DEFAULT; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks); - \$this->env->getRuntime("{$runtimeClass}")->displayZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); + yield \$this->env->getRuntime("{$runtimeClass}")->renderZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); EOT, $environment, ], @@ -105,7 +114,7 @@ public static function getTests(): array \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; \$nglContext = {$viewInterface}::CONTEXT_DEFAULT; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks); - \$this->env->getRuntime("{$runtimeClass}")->displayZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); + yield \$this->env->getRuntime("{$runtimeClass}")->renderZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); EOT, $environment, ], diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntimeTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntimeTest.php index 53e0bcc34..2ab0fa41d 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntimeTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Runtime/RenderingRuntimeTest.php @@ -4,13 +4,17 @@ namespace Netgen\Bundle\LayoutsBundle\Tests\Templating\Twig\Runtime; +use Doctrine\Common\Collections\ArrayCollection; use Exception; use Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime; use Netgen\Layouts\API\Service\BlockService; use Netgen\Layouts\API\Values\Block\Block; +use Netgen\Layouts\API\Values\Block\BlockList; use Netgen\Layouts\API\Values\Block\Placeholder; use Netgen\Layouts\API\Values\Collection\Item; use Netgen\Layouts\API\Values\Collection\Slot; +use Netgen\Layouts\API\Values\Layout\Layout; +use Netgen\Layouts\API\Values\Layout\Zone; use Netgen\Layouts\API\Values\LayoutResolver\RuleCondition; use Netgen\Layouts\Block\BlockDefinition; use Netgen\Layouts\Collection\Result\ManualItem; @@ -21,6 +25,7 @@ use Netgen\Layouts\Tests\Stubs\ErrorHandler; use Netgen\Layouts\View\RendererInterface; use Netgen\Layouts\View\Twig\ContextualizedTwigTemplate; +use Netgen\Layouts\View\View\ZoneView\ZoneReference; use Netgen\Layouts\View\ViewInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -60,6 +65,54 @@ protected function setUp(): void ); } + /** + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::__construct + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::getViewContext + * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::renderZone + */ + public function testRenderZone(): void + { + $zone = Zone::fromArray([]); + $blocks = new BlockList(); + $layout = Layout::fromArray(['zones' => new ArrayCollection(['zone' => $zone])]); + + $twigTemplate = new ContextualizedTwigTemplate($this->createMock(Template::class)); + + $this->blockServiceMock + ->expects(self::once()) + ->method('loadZoneBlocks') + ->with( + self::identicalTo($zone), + self::isNull(), + ) + ->willReturn($blocks); + + $this->rendererMock + ->expects(self::once()) + ->method('renderValue') + ->with( + self::isInstanceOf(ZoneReference::class), + self::identicalTo(ViewInterface::CONTEXT_DEFAULT), + self::identicalTo( + [ + 'blocks' => $blocks, + 'twig_template' => $twigTemplate, + ], + ), + ) + ->willReturn('rendered zone'); + + self::assertSame( + 'rendered zone', + $this->runtime->renderZone( + $layout, + 'zone', + ViewInterface::CONTEXT_DEFAULT, + $twigTemplate, + ), + ); + } + /** * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::__construct * @covers \Netgen\Bundle\LayoutsBundle\Templating\Twig\Runtime\RenderingRuntime::getViewContext From 40987764e7d8f8a6e28d3cd8195b9350a0d6194d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Fri, 6 Sep 2024 13:25:06 +0200 Subject: [PATCH 2/2] Fix deprecation in Twig 3.12 --- .../Templating/Twig/Node/DefaultContext.php | 4 ++-- .../Templating/Twig/Node/RenderZone.php | 4 ++-- .../Templating/Twig/TokenParser/DefaultContext.php | 2 +- .../Templating/Twig/TokenParser/RenderZone.php | 2 +- phpstan.tests.neon | 3 +++ .../Twig/TokenParser/DefaultContextTest.php | 12 +++++++++--- .../Templating/Twig/TokenParser/RenderZoneTest.php | 12 +++++++++--- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php index 2110eeeac..2d3cb324f 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContext.php @@ -12,9 +12,9 @@ #[YieldReady] final class DefaultContext extends Node { - public function __construct(AbstractExpression $expr, int $line = 0, ?string $tag = null) + public function __construct(AbstractExpression $expr, int $line = 0) { - parent::__construct(['expr' => $expr], [], $line, $tag); + parent::__construct(['expr' => $expr], [], $line); } public function compile(Compiler $compiler): void diff --git a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php index 3ddeca6b1..f5a89ca66 100644 --- a/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php +++ b/bundles/LayoutsBundle/Templating/Twig/Node/RenderZone.php @@ -19,14 +19,14 @@ #[YieldReady] final class RenderZone extends Node { - public function __construct(AbstractExpression $zone, ?AbstractExpression $context = null, int $line = 0, ?string $tag = null) + public function __construct(AbstractExpression $zone, ?AbstractExpression $context = null, int $line = 0) { $nodes = ['zone' => $zone]; if ($context instanceof AbstractExpression) { $nodes['context'] = $context; } - parent::__construct($nodes, [], $line, $tag); + parent::__construct($nodes, [], $line); } public function compile(Compiler $compiler): void diff --git a/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php b/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php index 0fcf61a7f..978fbaaad 100644 --- a/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php +++ b/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContext.php @@ -17,7 +17,7 @@ public function parse(Token $token): Node $this->parser->getStream()->expect(Token::BLOCK_END_TYPE); - return new DefaultContextNode($expression, $token->getLine(), $this->getTag()); + return new DefaultContextNode($expression, $token->getLine()); } public function getTag(): string diff --git a/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php b/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php index b63b0dda3..35099ec68 100644 --- a/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php +++ b/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZone.php @@ -45,7 +45,7 @@ public function parse(Token $token): Node $stream->expect(Token::BLOCK_END_TYPE); - return new RenderZoneNode($zone, $context, $token->getLine(), $this->getTag()); + return new RenderZoneNode($zone, $context, $token->getLine()); } public function getTag(): string diff --git a/phpstan.tests.neon b/phpstan.tests.neon index 380327450..5d54c1bc3 100644 --- a/phpstan.tests.neon +++ b/phpstan.tests.neon @@ -9,6 +9,7 @@ parameters: treatPhpDocTypesAsCertain: false dynamicConstantNames: - Symfony\Component\HttpKernel\Kernel::VERSION_ID + - Twig\Environment::MAJOR_VERSION excludePaths: - tests/application/public/bundles/ @@ -53,6 +54,8 @@ parameters: message: '#Undefined variable: \$this#' path: tests/lib/Transfer/Output/Visitor/Integration + - "#Call to function method_exists\\(\\) with .* and 'setNodeTag' will always evaluate to true.#" + - message: "#Offset 'db' does not exist on array#" path: tests/lib/Persistence/Doctrine/DatabaseTrait.php diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php index 4e1e2968a..fc9549d17 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/DefaultContextTest.php @@ -15,6 +15,8 @@ use Twig\Parser; use Twig\Source; +use function method_exists; + final class DefaultContextTest extends TestCase { private Environment $environment; @@ -42,10 +44,14 @@ protected function setUp(): void * * @dataProvider compileDataProvider */ - public function testCompile(string $source, DefaultContextNode $node): void + public function testCompile(string $source, DefaultContextNode $node, string $tag): void { $stream = $this->environment->tokenize(new Source($source, '')); + if (method_exists($node, 'setNodeTag')) { + $node->setNodeTag($tag); + } + self::assertSame((string) $node, (string) $this->parser->parse($stream)->getNode('body')->getNode('0')); } @@ -73,16 +79,16 @@ public static function compileDataProvider(): iterable new DefaultContextNode( new NameExpression('foo', 1), 1, - 'nglayouts_default_context', ), + 'nglayouts_default_context', ], [ '{% nglayouts_default_context "foo" %}', new DefaultContextNode( new ConstantExpression('foo', 1), 1, - 'nglayouts_default_context', ), + 'nglayouts_default_context', ], ]; } diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php index 167f49f69..da33610f0 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/TokenParser/RenderZoneTest.php @@ -15,6 +15,8 @@ use Twig\Parser; use Twig\Source; +use function method_exists; + final class RenderZoneTest extends TestCase { private Environment $environment; @@ -42,10 +44,14 @@ protected function setUp(): void * * @dataProvider compileDataProvider */ - public function testCompile(string $source, RenderZoneNode $node): void + public function testCompile(string $source, RenderZoneNode $node, string $tag): void { $stream = $this->environment->tokenize(new Source($source, '')); + if (method_exists($node, 'setNodeTag')) { + $node->setNodeTag($tag); + } + self::assertSame((string) $node, (string) $this->parser->parse($stream)->getNode('body')->getNode('0')); } @@ -74,8 +80,8 @@ public static function compileDataProvider(): iterable new NameExpression('zone', 1), null, 1, - 'nglayouts_render_zone', ), + 'nglayouts_render_zone', ], [ '{% nglayouts_render_zone zone context="json" %}', @@ -83,8 +89,8 @@ public static function compileDataProvider(): iterable new NameExpression('zone', 1), new ConstantExpression('json', 1), 1, - 'nglayouts_render_zone', ), + 'nglayouts_render_zone', ], ]; }