File: HtmlParserTest.php

package info (click to toggle)
php-oscarotero-html-parser 0.1.8-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 116 kB
  • sloc: php: 127; makefile: 17
file content (72 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download | duplicates (2)
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
<?php
declare(strict_types = 1);

namespace HtmlParser\Tests;

use PHPUnit\Framework\TestCase;
use HtmlParser\Parser;
use DOMDocument;
use DOMDocumentFragment;

class HtmlParserTest extends TestCase
{
    public function testHtmlFragment()
    {
        $html = '<img src="http://example.com/image.png?123456" alt="Image"><span>Hello world</span>';

        $fragment = Parser::parseFragment($html);

        $this->assertInstanceOf(DOMDocumentFragment::class, $fragment);
        $this->assertCount(2, $fragment->childNodes);
        $this->assertSame('img', $fragment->childNodes->item(0)->tagName);
        $this->assertSame($html, Parser::stringify($fragment));
    }

    public function testHtmlDocument()
    {
        $html = <<<HTML
<!DOCTYPE html>
<html><body>
        <img src="http://example.com/image.png?123456" alt="Image">
</body></html>
HTML;

        $document = Parser::parse($html);

        $this->assertInstanceOf(DOMDocument::class, $document);
        $this->assertCount(1, $document->getElementsByTagName('html'));
        $this->assertCount(1, $document->getElementsByTagName('body'));
        $this->assertCount(1, $document->getElementsByTagName('img'));

        $this->assertSame($html, trim(Parser::stringify($document)));
    }

    public function testHtmlDocumentFragment()
    {
        $html = '<img src="http://example.com/image.png?123456" alt="Image">';

        $htmlFinal = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><img src="http://example.com/image.png?123456" alt="Image"></body></html>
HTML;

        $document = Parser::parse($html);

        $this->assertInstanceOf(DOMDocument::class, $document);
        $this->assertCount(1, $document->getElementsByTagName('html'));
        $this->assertCount(1, $document->getElementsByTagName('body'));
        $this->assertCount(1, $document->getElementsByTagName('img'));

        $this->assertSame($htmlFinal, trim(Parser::stringify($document)));
    }

    public function testOnlyText()
    {
        $html = 'hello world';
        $document = Parser::parse($html);

        $this->assertInstanceOf(DOMDocument::class, $document);
        $this->assertCount(1, $document->getElementsByTagName('html'));
        $this->assertCount(1, $document->getElementsByTagName('body'));
    }
}