summaryrefslogtreecommitdiff
path: root/plugins/af_readability/vendor/masterminds/html5/test/HTML5/Serializer/TraverserTest.php
blob: d4ae7b351702015b96c9c6f86db140c607796cbc (plain)
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
<?php

namespace Masterminds\HTML5\Tests\Serializer;

use Masterminds\HTML5\Serializer\OutputRules;
use Masterminds\HTML5\Serializer\Traverser;

class TraverserTest extends \Masterminds\HTML5\Tests\TestCase
{
    protected $markup = '<!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Test</title>
      </head>
      <body>
        <p>This is a test.</p>
      </body>
    </html>';

    public function setUp()
    {
        $this->html5 = $this->getInstance();
    }

    /**
     * Using reflection we make a protected method accessible for testing.
     *
     * @param string $name
     *                     The name of the method on the Traverser class to test
     *
     * @return \ReflectionMethod \ReflectionMethod for the specified method
     */
    public function getProtectedMethod($name)
    {
        $class = new \ReflectionClass('\Masterminds\HTML5\Serializer\Traverser');
        $method = $class->getMethod($name);
        $method->setAccessible(true);

        return $method;
    }

    public function getTraverser()
    {
        $stream = fopen('php://temp', 'w');

        $dom = $this->html5->loadHTML($this->markup);
        $t = new Traverser($dom, $stream, $html5->getOptions());

        // We return both the traverser and stream so we can pull from it.
        return array(
            $t,
            $stream,
        );
    }

    public function testConstruct()
    {
        // The traverser needs a place to write the output to. In our case we
        // use a stream in temp space.
        $stream = fopen('php://temp', 'w');

        $html5 = $this->getInstance();

        $r = new OutputRules($stream, $this->html5->getOptions());
        $dom = $this->html5->loadHTML($this->markup);

        $t = new Traverser($dom, $stream, $r, $html5->getOptions());

        $this->assertInstanceOf('\Masterminds\HTML5\Serializer\Traverser', $t);
    }

    public function testFragmentDeprecated()
    {
        $html = '<span class="bar">foo</span><span></span><div>bar</div>';
        $input = new \Masterminds\HTML5\Parser\StringInputStream($html);
        $dom = $this->html5->parseFragment($input);

        $this->assertInstanceOf('\DOMDocumentFragment', $dom);

        $stream = fopen('php://temp', 'w');
        $r = new OutputRules($stream, $this->html5->getOptions());
        $t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
        $t->walk();

        $this->assertEquals($html, stream_get_contents($stream, -1, 0));
    }

    public function testFragment()
    {
        $html = '<span class="bar">foo</span><span></span><div>bar</div>';
        $dom = $this->html5->parseFragment($html);

        $this->assertInstanceOf('\DOMDocumentFragment', $dom);

        $stream = fopen('php://temp', 'w');
        $r = new OutputRules($stream, $this->html5->getOptions());
        $t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
        $t->walk();

        $this->assertEquals($html, stream_get_contents($stream, -1, 0));
    }

    public function testProcessorInstructionDeprecated()
    {
        $html = '<?foo bar ?>';
        $input = new \Masterminds\HTML5\Parser\StringInputStream($html);
        $dom = $this->html5->parseFragment($input);

        $this->assertInstanceOf('\DOMDocumentFragment', $dom);

        $stream = fopen('php://temp', 'w');
        $r = new OutputRules($stream, $this->html5->getOptions());

        $t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
        $t->walk();

        $this->assertEquals($html, stream_get_contents($stream, -1, 0));
    }

    public function testProcessorInstruction()
    {
        $html = '<?foo bar ?>';
        $dom = $this->html5->parseFragment($html);

        $this->assertInstanceOf('\DOMDocumentFragment', $dom);

        $stream = fopen('php://temp', 'w');
        $r = new OutputRules($stream, $this->html5->getOptions());

        $t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
        $t->walk();

        $this->assertEquals($html, stream_get_contents($stream, -1, 0));
    }
}