forked from voku/simple_html_dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleHtmlDomNodeTest.php
154 lines (130 loc) Β· 4.8 KB
/
SimpleHtmlDomNodeTest.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
use voku\helper\HtmlDomParser;
/**
* @internal
*/
final class SimpleHtmlDomNodeTest extends \PHPUnit\Framework\TestCase
{
/**
* @param $filename
*
* @return string|null
*/
protected function loadFixture($filename)
{
$path = __DIR__ . '/fixtures/' . $filename;
if (\file_exists($path)) {
return \file_get_contents($path);
}
return null;
}
/**
* @dataProvider findTests
*
* @param $html
* @param $selector
* @param $count
*/
public function testFind($html, $selector, $count)
{
$document = new HtmlDomParser($html);
$nodeList = $document->find('section');
$elements = $nodeList->find($selector);
static::assertInstanceOf(voku\helper\SimpleHtmlDomNodeInterface::class, $elements);
static::assertCount($count, $elements);
foreach ($elements as $node) {
static::assertInstanceOf(voku\helper\SimpleHtmlDom::class, $node);
}
}
/**
* @return array
*/
public function findTests(): array
{
$html = $this->loadFixture('test_page.html');
return [
[$html, '.fake h2', 0],
[$html, 'article', 16],
[$html, '.radio', 3],
[$html, 'input.radio', 3],
[$html, 'ul li', 9],
[$html, 'fieldset#forms__checkbox li, fieldset#forms__radio li', 6],
[$html, 'input[id]', 23],
[$html, 'input[id=in]', 1],
[$html, '#in', 1],
[$html, 'text', 539],
[$html, '*[id]', 51],
];
}
public function testInnerHtml()
{
$html = '<div><p>foo</p><p>bar</p></div>';
$document = new HtmlDomParser($html);
$element = $document->find('p');
static::assertSame('<p>foo</p><p>bar</p>', (string) $element);
static::assertSame(['<p>foo</p>', '<p>bar</p>'], $element->innerHtml());
static::assertSame(['foo', 'bar'], $element->innertext);
}
public function testText()
{
$html = '<div><p>foo</p><p>bar</p></div>';
$document = new HtmlDomParser($html);
$element = $document->find('p');
static::assertSame(['foo', 'bar'], $element->text());
static::assertSame(['foo', 'bar'], $element->plaintext);
}
public function testNonText()
{
$html = '<div><p>foo</p><p>bar</p></div>';
$document = new HtmlDomParser($html);
$element = $document->find('span');
static::assertInstanceOf(\voku\helper\SimpleHtmlDomNodeInterface::class, $element);
static::assertInstanceOf(\voku\helper\SimpleHtmlDomNodeBlank::class, $element);
static::assertSame([], $element->text());
static::assertSame([], $element->plaintext);
}
public function testNonText0()
{
$html = '<div><p>foo</p><p>bar</p></div>';
$document = new HtmlDomParser($html);
$element = $document->find('span', 0);
static::assertInstanceOf(\voku\helper\SimpleHtmlDomInterface::class, $element);
static::assertInstanceOf(\voku\helper\SimpleHtmlDomBlank::class, $element);
static::assertSame('', $element->class);
static::assertSame('', $element->text());
static::assertSame('', $element->plaintext);
}
public function testNonText1()
{
$html = '<div><p>foo</p><p>bar</p></div>';
$document = new HtmlDomParser($html);
$elements = $document->find('span', 1);
static::assertCount(0, $elements);
static::assertInstanceOf(\voku\helper\SimpleHtmlDomInterface::class, $elements);
static::assertInstanceOf(\voku\helper\SimpleHtmlDomBlank::class, $elements);
static::assertSame('', $elements->class);
static::assertSame('', $elements->text());
static::assertSame('', $elements->plaintext);
}
public function testGetFirstDomElement()
{
$html = '<div><p class="lall">foo</p><p>lall</p></div>';
$document = new HtmlDomParser($html);
$elements = $document->findMulti('p');
static::assertCount(2, $elements);
static::assertSame(['lall', ''], $elements->class);
static::assertSame(['foo', 'lall'], $elements->text());
static::assertSame(['foo', 'lall'], $elements->plaintext);
static::assertSame('lall', $elements[0]->class);
static::assertSame('foo', $elements[0]->text());
static::assertSame('foo', $elements[0]->plaintext);
// ---
$html = '<div><p class="lall">foo</p><p>lall</p></div>';
$document = new HtmlDomParser($html);
$element = $document->find('p', 0);
static::assertCount(1, $element);
static::assertSame('lall', $element->class);
static::assertSame('foo', $element->text());
static::assertSame('foo', $element->plaintext);
}
}