Security update for Core, with self-updated composer
[yaffs-website] / vendor / masterminds / html5 / test / HTML5 / Serializer / TraverserTest.php
1 <?php
2 namespace Masterminds\HTML5\Tests\Serializer;
3
4 use Masterminds\HTML5\Serializer\OutputRules;
5 use Masterminds\HTML5\Serializer\Traverser;
6 use Masterminds\HTML5\Parser;
7
8 class TraverserTest extends \Masterminds\HTML5\Tests\TestCase
9 {
10
11     protected $markup = '<!doctype html>
12     <html lang="en">
13       <head>
14         <meta charset="utf-8">
15         <title>Test</title>
16       </head>
17       <body>
18         <p>This is a test.</p>
19       </body>
20     </html>';
21
22     public function setUp()
23     {
24         $this->html5 = $this->getInstance();
25     }
26
27     /**
28      * Using reflection we make a protected method accessible for testing.
29      *
30      * @param string $name
31      *            The name of the method on the Traverser class to test.
32      *
33      * @return \ReflectionMethod \ReflectionMethod for the specified method
34      */
35     public function getProtectedMethod($name)
36     {
37         $class = new \ReflectionClass('\Masterminds\HTML5\Serializer\Traverser');
38         $method = $class->getMethod($name);
39         $method->setAccessible(true);
40
41         return $method;
42     }
43
44     public function getTraverser()
45     {
46         $stream = fopen('php://temp', 'w');
47
48         $dom = $this->html5->loadHTML($this->markup);
49         $t = new Traverser($dom, $stream, $html5->getOptions());
50
51         // We return both the traverser and stream so we can pull from it.
52         return array(
53             $t,
54             $stream
55         );
56     }
57
58     public function testConstruct()
59     {
60         // The traverser needs a place to write the output to. In our case we
61         // use a stream in temp space.
62         $stream = fopen('php://temp', 'w');
63
64         $html5 = $this->getInstance();
65
66         $r = new OutputRules($stream, $this->html5->getOptions());
67         $dom = $this->html5->loadHTML($this->markup);
68
69         $t = new Traverser($dom, $stream, $r, $html5->getOptions());
70
71         $this->assertInstanceOf('\Masterminds\HTML5\Serializer\Traverser', $t);
72     }
73
74     public function testFragment()
75     {
76         $html = '<span class="bar">foo</span><span></span><div>bar</div>';
77         $input = new \Masterminds\HTML5\Parser\StringInputStream($html);
78         $dom = $this->html5->parseFragment($input);
79
80         $this->assertInstanceOf('\DOMDocumentFragment', $dom);
81
82         $stream = fopen('php://temp', 'w');
83         $r = new OutputRules($stream, $this->html5->getOptions());
84         $t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
85
86         $out = $t->walk();
87         $this->assertEquals($html, stream_get_contents($stream, - 1, 0));
88     }
89
90     public function testProcessorInstruction()
91     {
92         $html = '<?foo bar ?>';
93         $input = new \Masterminds\HTML5\Parser\StringInputStream($html);
94         $dom = $this->html5->parseFragment($input);
95
96         $this->assertInstanceOf('\DOMDocumentFragment', $dom);
97
98         $stream = fopen('php://temp', 'w');
99         $r = new OutputRules($stream, $this->html5->getOptions());
100         $t = new Traverser($dom, $stream, $r, $this->html5->getOptions());
101
102         $out = $t->walk();
103         $this->assertEquals($html, stream_get_contents($stream, - 1, 0));
104     }
105 }