From aeb54b38892386a4d160288bba43ab3024158279 Mon Sep 17 00:00:00 2001 From: matheo Date: Thu, 28 Sep 2023 10:59:12 +0200 Subject: [PATCH] rename props key and add a test --- src/LiveComponent/phpunit.xml.dist.bak | 34 +++++++++++++++++++ src/TwigComponent/src/ComponentRenderer.php | 2 +- src/TwigComponent/src/Twig/PropsNode.php | 2 +- ...with_variable_already_in_context.html.twig | 5 +++ .../templates/components/Message.html.twig | 5 +++ .../Integration/ComponentExtensionTest.php | 7 ++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/LiveComponent/phpunit.xml.dist.bak create mode 100644 src/TwigComponent/tests/Fixtures/templates/anonymous_component_with_variable_already_in_context.html.twig create mode 100644 src/TwigComponent/tests/Fixtures/templates/components/Message.html.twig diff --git a/src/LiveComponent/phpunit.xml.dist.bak b/src/LiveComponent/phpunit.xml.dist.bak new file mode 100644 index 00000000000..989e6115584 --- /dev/null +++ b/src/LiveComponent/phpunit.xml.dist.bak @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + ./tests/ + + + + + + ./src + + + + + + + diff --git a/src/TwigComponent/src/ComponentRenderer.php b/src/TwigComponent/src/ComponentRenderer.php index 3cbbd2f25b6..d2d87ad4b90 100644 --- a/src/TwigComponent/src/ComponentRenderer.php +++ b/src/TwigComponent/src/ComponentRenderer.php @@ -135,7 +135,7 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR // add attributes [$metadata->getAttributesVar() => $mounted->getAttributes()], $props, - ['props' => $props] + ['__props' => $props] ); $event = new PreRenderEvent($mounted, $metadata, $variables); diff --git a/src/TwigComponent/src/Twig/PropsNode.php b/src/TwigComponent/src/Twig/PropsNode.php index 81cfe20e94e..26592854ce4 100644 --- a/src/TwigComponent/src/Twig/PropsNode.php +++ b/src/TwigComponent/src/Twig/PropsNode.php @@ -37,7 +37,7 @@ public function compile(Compiler $compiler): void $compiler ->write('$propsNames[] = \''.$name.'\';') ->write('$context[\'attributes\'] = $context[\'attributes\']->remove(\''.$name.'\');') - ->write('if (!isset($context[\'props\'][\''.$name.'\'])) {'); + ->write('if (!isset($context[\'__props\'][\''.$name.'\'])) {'); if (!$this->hasNode($name)) { $compiler diff --git a/src/TwigComponent/tests/Fixtures/templates/anonymous_component_with_variable_already_in_context.html.twig b/src/TwigComponent/tests/Fixtures/templates/anonymous_component_with_variable_already_in_context.html.twig new file mode 100644 index 00000000000..803e2c8572a --- /dev/null +++ b/src/TwigComponent/tests/Fixtures/templates/anonymous_component_with_variable_already_in_context.html.twig @@ -0,0 +1,5 @@ +{% set message = 'bar' %} + + +

Hey!

+
\ No newline at end of file diff --git a/src/TwigComponent/tests/Fixtures/templates/components/Message.html.twig b/src/TwigComponent/tests/Fixtures/templates/components/Message.html.twig new file mode 100644 index 00000000000..74d3279110a --- /dev/null +++ b/src/TwigComponent/tests/Fixtures/templates/components/Message.html.twig @@ -0,0 +1,5 @@ +{% props message = 'foo' %} + +

{{ message }}

+{% block content %} +{% endblock %} \ No newline at end of file diff --git a/src/TwigComponent/tests/Integration/ComponentExtensionTest.php b/src/TwigComponent/tests/Integration/ComponentExtensionTest.php index 1e238f8d694..1fd8a961ac2 100644 --- a/src/TwigComponent/tests/Integration/ComponentExtensionTest.php +++ b/src/TwigComponent/tests/Integration/ComponentExtensionTest.php @@ -195,6 +195,13 @@ public function testRenderAnonymousComponentWithNonScalarProps(): void $this->assertStringContainsString('class variable defined? no', $output); } + public function testComponentPropsOverwriteContextValue(): void + { + $output = self::getContainer()->get(Environment::class)->render('anonymous_component_with_variable_already_in_context.html.twig'); + + $this->assertStringContainsString('

foo

', $output); + } + private function renderComponent(string $name, array $data = []): string { return self::getContainer()->get(Environment::class)->render('render_component.html.twig', [