Skip to content

Commit cf8a376

Browse files
committed
test: add test to missing coverage line
1 parent f35d1e2 commit cf8a376

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

test/phpunit/DocumentBinderTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
use Gt\DomTemplate\Test\TestHelper\Model\Customer;
3030
use PHPUnit\Framework\TestCase;
3131
use stdClass;
32+
use IteratorAggregate;
33+
use Traversable;
34+
use ArrayIterator;
3235

3336
class DocumentBinderTest extends TestCase {
3437
/**
@@ -1332,6 +1335,37 @@ public function isTimeCurrentlyDaytime():bool {
13321335
self::assertSame("Is it day or night? It's daytime!", $dayOrNight->textContent);
13331336
}
13341337

1338+
/**
1339+
* When passing an object to bindData, the object could be both
1340+
* key-value-pairs and iterable (notably an IteratorAggregate).
1341+
*/
1342+
public function testBindData_iterableObject():void {
1343+
$document = new HTMLDocument(HTMLPageContent::HTML_LIST);
1344+
$sut = new DocumentBinder($document);
1345+
$sut->setDependencies(...$this->documentBinderDependencies($document));
1346+
1347+
$obj = new class implements IteratorAggregate {
1348+
public string $id = "ABC123";
1349+
public string $name = "Example";
1350+
1351+
public function getIterator():Traversable {
1352+
return new ArrayIterator(["One", "Two", "Three", "Four"]);
1353+
}
1354+
};
1355+
1356+
$sut->bindData($obj);
1357+
$h1 = $document->querySelector("h1");
1358+
$ul = $document->querySelector("ul");
1359+
$ol = $document->querySelector("ol");
1360+
1361+
// The object should have its properties bound to the page:
1362+
self::assertSame("Example", $h1->textContent);
1363+
// but it should bind itself as an iterator to the list:
1364+
self::assertCount(4, $ul->children);
1365+
// and the un-attributed list should not change:
1366+
self::assertCount(1, $ol->children);
1367+
}
1368+
13351369
private function documentBinderDependencies(HTMLDocument $document, mixed...$otherObjectList):array {
13361370
$htmlAttributeBinder = new HTMLAttributeBinder();
13371371
$htmlAttributeCollection = new HTMLAttributeCollection();

test/phpunit/TestHelper/HTMLPageContent.php

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ class HTMLPageContent {
265265

266266
const HTML_LIST = <<<HTML
267267
<!doctype html>
268+
<h1 data-bind:text="name">List name</h1>
268269
<ul>
269270
<li data-list data-bind:text>Template item!</li>
270271
</ul>

0 commit comments

Comments
 (0)