From 25bfb5957c390cbeebb7a9d30239d83ec572ca18 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Tue, 17 Mar 2026 08:15:20 +0100 Subject: [PATCH] Add two tests for error conditions in #3930 --- extra/html-extra/Tests/HtmlAttrTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/extra/html-extra/Tests/HtmlAttrTest.php b/extra/html-extra/Tests/HtmlAttrTest.php index 5c23ebe7c1e..41f704cbadf 100644 --- a/extra/html-extra/Tests/HtmlAttrTest.php +++ b/extra/html-extra/Tests/HtmlAttrTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Twig\Environment; +use Twig\Error\RuntimeError; use Twig\Extra\Html\HtmlAttr\AttributeValueInterface; use Twig\Extra\Html\HtmlAttr\SeparatedTokenList; use Twig\Extra\Html\HtmlExtension; @@ -269,6 +270,28 @@ public function getIterator(): \Traversable self::assertSame('data-controller="dropdown tooltip" data-action="click->dropdown#toggle mouseover->tooltip#show"', $result); } + + public function testDataAttributeWithNonJsonEncodableValueThrowsRuntimeError() + { + $this->expectException(RuntimeError::class); + $this->expectExceptionMessage('The "data-bad" attribute value cannot be JSON encoded.'); + + HtmlExtension::htmlAttr( + new Environment(new ArrayLoader()), + ['data-bad' => [\INF]] // INF cannot be JSON-encoded + ); + } + + public function testNonStringableObjectAsAttributeValueThrowsRuntimeError() + { + $this->expectException(RuntimeError::class); + $this->expectExceptionMessage('The "title" attribute value should be a scalar, an iterable, or an object implementing "Stringable"'); + + HtmlExtension::htmlAttr( + new Environment(new ArrayLoader()), + ['title' => new \stdClass()] + ); + } } class StringableStub implements \Stringable