/var/www/hkosl.com/b2b2c/webadmin/libraies/paquettg/php-html-parser/tests/CollectionTest.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

use PHPHtmlParser\Selector;
use 
PHPHtmlParser\Dom\HtmlNode;
use 
PHPHtmlParser\Dom\Tag;
use 
PHPHtmlParser\Dom\Collection;

class 
CollectionTest extends PHPUnit_Framework_TestCase {
    
    public function 
testEach()
    {
        
$root   = new HtmlNode(new Tag('root'));
        
$parent = new HtmlNode(new Tag('div'));
        
$child1 = new HtmlNode(new Tag('a'));
        
$child2 = new HtmlNode(new Tag('p'));
        
$child3 = new HtmlNode(new Tag('a'));
        
$root->addChild($parent);
        
$parent->addChild($child1);
        
$parent->addChild($child2);
        
$child2->addChild($child3);

        
$selector   = new Selector('a');
        
$collection $selector->find($root);
        
$count      0;
        
$collection->each(function ($node) use (&$count) {
            ++
$count;
        });
        
$this->assertEquals(2$count);
    }

    
/**
     * @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
     */
    
public function testCallNoNodes()
    {
        
$collection = new Collection();
        
$collection->innerHtml();
    }

    public function 
testCallMagic()
    {
        
$root   = new HtmlNode(new Tag('root'));
        
$parent = new HtmlNode(new Tag('div'));
        
$child1 = new HtmlNode(new Tag('a'));
        
$child2 = new HtmlNode(new Tag('p'));
        
$child3 = new HtmlNode(new Tag('a'));
        
$root->addChild($parent);
        
$parent->addChild($child1);
        
$parent->addChild($child2);
        
$child2->addChild($child3);

        
$selector = new Selector('div * a');
        
$this->assertEquals($child3->id(), $selector->find($root)->id());
    }

    public function 
testGetMagic()
    {
        
$root   = new HtmlNode(new Tag('root'));
        
$parent = new HtmlNode(new Tag('div'));
        
$child1 = new HtmlNode(new Tag('a'));
        
$child2 = new HtmlNode(new Tag('p'));
        
$child3 = new HtmlNode(new Tag('a'));
        
$root->addChild($parent);
        
$parent->addChild($child1);
        
$parent->addChild($child2);
        
$child2->addChild($child3);

        
$selector = new Selector('div * a');
        
$this->assertEquals($child3->innerHtml$selector->find($root)->innerHtml);
    }

    
/**
     * @expectedException PHPHtmlParser\Exceptions\EmptyCollectionException
     */
    
public function testGetNoNodes()
    {
        
$collection = new Collection();
        
$collection->innerHtml;
    }

    public function 
testToStringMagic()
    {
        
$root   = new HtmlNode(new Tag('root'));
        
$parent = new HtmlNode(new Tag('div'));
        
$child1 = new HtmlNode(new Tag('a'));
        
$child2 = new HtmlNode(new Tag('p'));
        
$child3 = new HtmlNode(new Tag('a'));
        
$root->addChild($parent);
        
$parent->addChild($child1);
        
$parent->addChild($child2);
        
$child2->addChild($child3);

        
$selector = new Selector('div * a');
        
$this->assertEquals((string)$child3, (string)$selector->find($root));
    }

    public function 
testToArray()
    {
        
$root   = new HtmlNode(new Tag('root'));
        
$parent = new HtmlNode(new Tag('div'));
        
$child1 = new HtmlNode(new Tag('a'));
        
$child2 = new HtmlNode(new Tag('p'));
        
$child3 = new HtmlNode(new Tag('a'));
        
$root->addChild($parent);
        
$parent->addChild($child1);
        
$parent->addChild($child2);
        
$child2->addChild($child3);

        
$selector   = new Selector('a');
        
$collection $selector->find($root);
        
$array      $collection->toArray();
        
$lastA      end($array);
        
$this->assertEquals($child3->id(), $lastA->id());
    }
}