Skip to content

Commit 313900f

Browse files
committed
Add two tests for error conditions in #3930
1 parent 21037a3 commit 313900f

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Tests/HtmlAttrTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Twig\Environment;
16+
use Twig\Error\RuntimeError;
1617
use Twig\Extra\Html\HtmlAttr\AttributeValueInterface;
1718
use Twig\Extra\Html\HtmlAttr\SeparatedTokenList;
1819
use Twig\Extra\Html\HtmlExtension;
@@ -269,6 +270,28 @@ public function getIterator(): \Traversable
269270

270271
self::assertSame('data-controller="dropdown tooltip" data-action="click->dropdown#toggle mouseover->tooltip#show"', $result);
271272
}
273+
274+
public function testDataAttributeWithNonJsonEncodableValueThrowsRuntimeError()
275+
{
276+
$this->expectException(RuntimeError::class);
277+
$this->expectExceptionMessage('The "data-bad" attribute value cannot be JSON encoded.');
278+
279+
HtmlExtension::htmlAttr(
280+
new Environment(new ArrayLoader()),
281+
['data-bad' => [\INF]] // INF cannot be JSON-encoded
282+
);
283+
}
284+
285+
public function testNonStringableObjectAsAttributeValueThrowsRuntimeError()
286+
{
287+
$this->expectException(RuntimeError::class);
288+
$this->expectExceptionMessage('The "title" attribute value should be a scalar, an iterable, or an object implementing "Stringable"');
289+
290+
HtmlExtension::htmlAttr(
291+
new Environment(new ArrayLoader()),
292+
['title' => new \stdClass()]
293+
);
294+
}
272295
}
273296

274297
class StringableStub implements \Stringable

0 commit comments

Comments
 (0)