Add two tests for error conditions in #3930

This commit is contained in:
Matthias Pigulla
2026-03-17 08:15:20 +01:00
parent 8b93364bf6
commit 25bfb5957c
+23
View File
@@ -13,6 +13,7 @@ namespace Twig\Extra\Html\Tests;
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 @@ class HtmlAttrTest extends TestCase
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