From 276d69239646ab21b402737b4cfc2ccaa452de59 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 17 Mar 2023 14:10:48 -0400 Subject: [PATCH] [Twig] Fixing a bug where a strinagable attribute was converted too late This caused LiveComponents to try to use the Stringable object in its checksum and to json_encode() the object into the props data, instead of using the final, string value --- src/TwigComponent/src/ComponentFactory.php | 1 + .../tests/Integration/ComponentFactoryTest.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/TwigComponent/src/ComponentFactory.php b/src/TwigComponent/src/ComponentFactory.php index 2eab234da85..7726995cab1 100644 --- a/src/TwigComponent/src/ComponentFactory.php +++ b/src/TwigComponent/src/ComponentFactory.php @@ -86,6 +86,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat // ensure remaining data is scalar foreach ($data as $key => $value) { if ($value instanceof \Stringable) { + $data[$key] = (string) $value; continue; } diff --git a/src/TwigComponent/tests/Integration/ComponentFactoryTest.php b/src/TwigComponent/tests/Integration/ComponentFactoryTest.php index 505f376dcfa..4f73dd3edea 100644 --- a/src/TwigComponent/tests/Integration/ComponentFactoryTest.php +++ b/src/TwigComponent/tests/Integration/ComponentFactoryTest.php @@ -100,14 +100,14 @@ public function testExceptionThrownIfUnableToWritePassedDataToPropertyAndIsNotSc public function testStringableObjectCanBePassedToComponent(): void { - $attributes = (string) $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() { + $attributes = $this->factory()->create('component_a', ['propB' => 'B', 'data-item-id-param' => new class() { public function __toString(): string { return 'test'; } - }])->getAttributes(); + }])->getAttributes()->all(); - self::assertSame(' data-item-id-param="test"', $attributes); + self::assertSame(['data-item-id-param' => 'test'], $attributes); } public function testTwigComponentServiceTagMustHaveKey(): void