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