forked from bupt1987/html-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlTest.php
More file actions
46 lines (37 loc) · 1.2 KB
/
HtmlTest.php
File metadata and controls
46 lines (37 loc) · 1.2 KB
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
<?php
class HtmlTest extends PHPUnit_Framework_TestCase {
public function testDom() {
$sHtml = self::getHtml();
$oDom = new \HtmlParser\ParserDom($sHtml);
$this->assertEquals('p4', $oDom->find('p', -1)->getPlainText());
$this->assertEquals('p_id', $oDom->find('p[id]', 0)->getPlainText());
$this->assertEquals('p_id_2', $oDom->find('p[id=p_id_2]', 0)->getPlainText());
$this->assertEquals('p2', $oDom->find('p[!id]', 1)->getPlainText());
$this->assertEquals('测试1', $oDom->find('#test1', 0)->getPlainText());
$oPClass = $oDom->find('p.test_class1', 0);
$this->assertEquals('p1', $oPClass->getPlainText());
$this->assertEquals('test_class test_class1', $oPClass->getAttr('class'));
$lCheck = array(
'p1',
'p2',
'p3',
'p_id',
'p_id_2',
);
$lPTag = $oDom->find('p.test_class');
$this->assertEquals(5, count($lPTag));
$lPText = array();
foreach ($lPTag as $oPTag) {
$lPText[] = $oPTag->getPlainText();
}
$this->assertEquals($lCheck, $lPText);
$this->assertEquals($oDom->node instanceof \DOMNode, true);
}
private static function getHtml() {
static $sHtml;
if ($sHtml === null) {
$sHtml = file_get_contents(__DIR__ . '/test.html');
}
return $sHtml;
}
}