TraverserTest.php 4.0 KB

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