|
29 | 29 | use Gt\DomTemplate\Test\TestHelper\Model\Customer;
|
30 | 30 | use PHPUnit\Framework\TestCase;
|
31 | 31 | use stdClass;
|
| 32 | +use IteratorAggregate; |
| 33 | +use Traversable; |
| 34 | +use ArrayIterator; |
32 | 35 |
|
33 | 36 | class DocumentBinderTest extends TestCase {
|
34 | 37 | /**
|
@@ -1332,6 +1335,37 @@ public function isTimeCurrentlyDaytime():bool {
|
1332 | 1335 | self::assertSame("Is it day or night? It's daytime!", $dayOrNight->textContent);
|
1333 | 1336 | }
|
1334 | 1337 |
|
| 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 | + |
1335 | 1369 | private function documentBinderDependencies(HTMLDocument $document, mixed...$otherObjectList):array {
|
1336 | 1370 | $htmlAttributeBinder = new HTMLAttributeBinder();
|
1337 | 1371 | $htmlAttributeCollection = new HTMLAttributeCollection();
|
|
0 commit comments