Skip to content

Commit

Permalink
test: add test to missing coverage line
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 25, 2024
1 parent f35d1e2 commit cf8a376
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/phpunit/DocumentBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use Gt\DomTemplate\Test\TestHelper\Model\Customer;
use PHPUnit\Framework\TestCase;
use stdClass;
use IteratorAggregate;
use Traversable;
use ArrayIterator;

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

/**
* When passing an object to bindData, the object could be both
* key-value-pairs and iterable (notably an IteratorAggregate).
*/
public function testBindData_iterableObject():void {
$document = new HTMLDocument(HTMLPageContent::HTML_LIST);
$sut = new DocumentBinder($document);
$sut->setDependencies(...$this->documentBinderDependencies($document));

$obj = new class implements IteratorAggregate {
public string $id = "ABC123";
public string $name = "Example";

public function getIterator():Traversable {
return new ArrayIterator(["One", "Two", "Three", "Four"]);
}
};

$sut->bindData($obj);
$h1 = $document->querySelector("h1");
$ul = $document->querySelector("ul");
$ol = $document->querySelector("ol");

// The object should have its properties bound to the page:
self::assertSame("Example", $h1->textContent);
// but it should bind itself as an iterator to the list:
self::assertCount(4, $ul->children);
// and the un-attributed list should not change:
self::assertCount(1, $ol->children);
}

private function documentBinderDependencies(HTMLDocument $document, mixed...$otherObjectList):array {
$htmlAttributeBinder = new HTMLAttributeBinder();
$htmlAttributeCollection = new HTMLAttributeCollection();
Expand Down
1 change: 1 addition & 0 deletions test/phpunit/TestHelper/HTMLPageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class HTMLPageContent {

const HTML_LIST = <<<HTML
<!doctype html>
<h1 data-bind:text="name">List name</h1>
<ul>
<li data-list data-bind:text>Template item!</li>
</ul>
Expand Down

0 comments on commit cf8a376

Please sign in to comment.