|
1 | 1 | <?php |
2 | 2 |
|
| 3 | + |
3 | 4 | use PHPHtmlParser\Dom\HtmlNode; |
4 | 5 | use PHPHtmlParser\Dom\TextNode; |
5 | 6 | use PHPHtmlParser\Dom\MockNode; |
@@ -368,4 +369,66 @@ public function testSetAttribute() |
368 | 369 | $node->setAttribute('class', 'foo'); |
369 | 370 | $this->assertEquals('foo', $node->getAttribute('class')); |
370 | 371 | } |
| 372 | + |
| 373 | + public function testCountable() |
| 374 | + { |
| 375 | + $div = new Tag('div'); |
| 376 | + $div->setAttributes([ |
| 377 | + 'class' => [ |
| 378 | + 'value' => 'all', |
| 379 | + 'doubleQuote' => true, |
| 380 | + ], |
| 381 | + ]); |
| 382 | + $a = new Tag('a'); |
| 383 | + $a->setAttributes([ |
| 384 | + 'href' => [ |
| 385 | + 'value' => 'http://google.com', |
| 386 | + 'doubleQuote' => false, |
| 387 | + ], |
| 388 | + ]); |
| 389 | + $br = new Tag('br'); |
| 390 | + $br->selfClosing(); |
| 391 | + |
| 392 | + $parent = new HtmlNode($div); |
| 393 | + $childa = new HtmlNode($a); |
| 394 | + $childbr = new HtmlNode($br); |
| 395 | + $parent->addChild($childa); |
| 396 | + $parent->addChild($childbr); |
| 397 | + $childa->addChild(new TextNode('link')); |
| 398 | + |
| 399 | + $this->assertEquals(count($parent->getChildren()), count($parent)); |
| 400 | + } |
| 401 | + |
| 402 | + public function testIterator() |
| 403 | + { |
| 404 | + $div = new Tag('div'); |
| 405 | + $div->setAttributes([ |
| 406 | + 'class' => [ |
| 407 | + 'value' => 'all', |
| 408 | + 'doubleQuote' => true, |
| 409 | + ], |
| 410 | + ]); |
| 411 | + $a = new Tag('a'); |
| 412 | + $a->setAttributes([ |
| 413 | + 'href' => [ |
| 414 | + 'value' => 'http://google.com', |
| 415 | + 'doubleQuote' => false, |
| 416 | + ], |
| 417 | + ]); |
| 418 | + $br = new Tag('br'); |
| 419 | + $br->selfClosing(); |
| 420 | + |
| 421 | + $parent = new HtmlNode($div); |
| 422 | + $childa = new HtmlNode($a); |
| 423 | + $childbr = new HtmlNode($br); |
| 424 | + $parent->addChild($childa); |
| 425 | + $parent->addChild($childbr); |
| 426 | + $childa->addChild(new TextNode('link')); |
| 427 | + |
| 428 | + $children = 0; |
| 429 | + foreach ($parent as $child) { |
| 430 | + ++$children; |
| 431 | + } |
| 432 | + $this->assertEquals(2, $children); |
| 433 | + } |
371 | 434 | } |
0 commit comments