HtmlDumperTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Tests\Dumper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\VarDumper\Cloner\VarCloner;
  13. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class HtmlDumperTest extends TestCase
  18. {
  19. public function testGet()
  20. {
  21. if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
  22. $this->markTestSkipped('A custom file_link_format is defined.');
  23. }
  24. require __DIR__.'/../Fixtures/dumb-var.php';
  25. $dumper = new HtmlDumper('php://output');
  26. $dumper->setDumpHeader('<foo></foo>');
  27. $dumper->setDumpBoundaries('<bar>', '</bar>');
  28. $cloner = new VarCloner();
  29. $cloner->addCasters([
  30. ':stream' => function ($res, $a) {
  31. unset($a['uri'], $a['wrapper_data']);
  32. return $a;
  33. },
  34. ]);
  35. $data = $cloner->cloneVar($var);
  36. ob_start();
  37. $dumper->dump($data);
  38. $out = ob_get_clean();
  39. $out = preg_replace('/[ \t]+$/m', '', $out);
  40. $var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8');
  41. $intMax = PHP_INT_MAX;
  42. preg_match('/sf-dump-\d+/', $out, $dumpId);
  43. $dumpId = $dumpId[0];
  44. $res = (int) $var['res'];
  45. $this->assertStringMatchesFormat(
  46. <<<EOTXT
  47. <foo></foo><bar><span class=sf-dump-note>array:24</span> [<samp>
  48. "<span class=sf-dump-key>number</span>" => <span class=sf-dump-num>1</span>
  49. <span class=sf-dump-key>0</span> => <a class=sf-dump-ref href=#{$dumpId}-ref01 title="2 occurrences">&amp;1</a> <span class=sf-dump-const>null</span>
  50. "<span class=sf-dump-key>const</span>" => <span class=sf-dump-num>1.1</span>
  51. <span class=sf-dump-key>1</span> => <span class=sf-dump-const>true</span>
  52. <span class=sf-dump-key>2</span> => <span class=sf-dump-const>false</span>
  53. <span class=sf-dump-key>3</span> => <span class=sf-dump-num>NAN</span>
  54. <span class=sf-dump-key>4</span> => <span class=sf-dump-num>INF</span>
  55. <span class=sf-dump-key>5</span> => <span class=sf-dump-num>-INF</span>
  56. <span class=sf-dump-key>6</span> => <span class=sf-dump-num>{$intMax}</span>
  57. "<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="5 characters">d&%s;j&%s;<span class="sf-dump-default sf-dump-ns">\\n</span></span>"
  58. <span class=sf-dump-key>7</span> => b"""
  59. <span class=sf-dump-str title="11 binary or non-UTF-8 characters">&eacute;<span class="sf-dump-default">\\x00</span>test<span class="sf-dump-default">\\t</span><span class="sf-dump-default sf-dump-ns">\\n</span></span>
  60. <span class=sf-dump-str title="11 binary or non-UTF-8 characters">ing</span>
  61. """
  62. "<span class=sf-dump-key>[]</span>" => []
  63. "<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp>
  64. %A <span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
  65. <span class=sf-dump-meta>stream_type</span>: "<span class=sf-dump-str title="5 characters">STDIO</span>"
  66. <span class=sf-dump-meta>mode</span>: "<span class=sf-dump-str>r</span>"
  67. <span class=sf-dump-meta>unread_bytes</span>: <span class=sf-dump-num>0</span>
  68. <span class=sf-dump-meta>seekable</span>: <span class=sf-dump-const>true</span>
  69. %A <span class=sf-dump-meta>options</span>: []
  70. </samp>}
  71. "<span class=sf-dump-key>obj</span>" => <abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="2 occurrences">#%d</a><samp id={$dumpId}-ref2%d>
  72. +<span class=sf-dump-public title="Public property">foo</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
  73. +"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"
  74. </samp>}
  75. "<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure(\$a, PDO &amp;\$b = null)</span> {<a class=sf-dump-ref>#%d</a><samp>
  76. <span class=sf-dump-meta>class</span>: "<span class=sf-dump-str title="Symfony\Component\VarDumper\Tests\Dumper\HtmlDumperTest
  77. 55 characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-class">Symfony\Component\VarDumper\Tests\Dumper</span><span class=sf-dump-ellipsis>\</span>HtmlDumperTest</span>"
  78. <span class=sf-dump-meta>this</span>: <abbr title="Symfony\Component\VarDumper\Tests\Dumper\HtmlDumperTest" class=sf-dump-note>HtmlDumperTest</abbr> {<a class=sf-dump-ref>#%d</a> &%s;}
  79. <span class=sf-dump-meta>file</span>: "<span class=sf-dump-str title="{$var['file']}
  80. %d characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class=sf-dump-ellipsis>%e</span>Tests%eFixtures%edumb-var.php</span>"
  81. <span class=sf-dump-meta>line</span>: "<span class=sf-dump-str title="%d characters">{$var['line']} to {$var['line']}</span>"
  82. </samp>}
  83. "<span class=sf-dump-key>line</span>" => <span class=sf-dump-num>{$var['line']}</span>
  84. "<span class=sf-dump-key>nobj</span>" => <span class=sf-dump-note>array:1</span> [<samp>
  85. <span class=sf-dump-index>0</span> => <a class=sf-dump-ref href=#{$dumpId}-ref03 title="2 occurrences">&amp;3</a> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="3 occurrences">#%d</a>}
  86. </samp>]
  87. "<span class=sf-dump-key>recurs</span>" => <a class=sf-dump-ref href=#{$dumpId}-ref04 title="2 occurrences">&amp;4</a> <span class=sf-dump-note>array:1</span> [<samp id={$dumpId}-ref04>
  88. <span class=sf-dump-index>0</span> => <a class=sf-dump-ref href=#{$dumpId}-ref04 title="2 occurrences">&amp;4</a> <span class=sf-dump-note>array:1</span> [<a class=sf-dump-ref href=#{$dumpId}-ref04 title="2 occurrences">&amp;4</a>]
  89. </samp>]
  90. <span class=sf-dump-key>8</span> => <a class=sf-dump-ref href=#{$dumpId}-ref01 title="2 occurrences">&amp;1</a> <span class=sf-dump-const>null</span>
  91. "<span class=sf-dump-key>sobj</span>" => <abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="2 occurrences">#%d</a>}
  92. "<span class=sf-dump-key>snobj</span>" => <a class=sf-dump-ref href=#{$dumpId}-ref03 title="2 occurrences">&amp;3</a> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="3 occurrences">#%d</a>}
  93. "<span class=sf-dump-key>snobj2</span>" => {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="3 occurrences">#%d</a>}
  94. "<span class=sf-dump-key>file</span>" => "<span class=sf-dump-str title="%d characters">{$var['file']}</span>"
  95. b"<span class=sf-dump-key>bin-key-&%s;</span>" => ""
  96. </samp>]
  97. </bar>
  98. EOTXT
  99. ,
  100. $out
  101. );
  102. }
  103. public function testCharset()
  104. {
  105. $var = mb_convert_encoding('Словарь', 'CP1251', 'UTF-8');
  106. $dumper = new HtmlDumper('php://output', 'CP1251');
  107. $dumper->setDumpHeader('<foo></foo>');
  108. $dumper->setDumpBoundaries('<bar>', '</bar>');
  109. $cloner = new VarCloner();
  110. $data = $cloner->cloneVar($var);
  111. $out = $dumper->dump($data, true);
  112. $this->assertStringMatchesFormat(
  113. <<<'EOTXT'
  114. <foo></foo><bar>b"<span class=sf-dump-str title="7 binary or non-UTF-8 characters">&#1057;&#1083;&#1086;&#1074;&#1072;&#1088;&#1100;</span>"
  115. </bar>
  116. EOTXT
  117. ,
  118. $out
  119. );
  120. }
  121. public function testAppend()
  122. {
  123. $out = fopen('php://memory', 'r+b');
  124. $dumper = new HtmlDumper();
  125. $dumper->setDumpHeader('<foo></foo>');
  126. $dumper->setDumpBoundaries('<bar>', '</bar>');
  127. $cloner = new VarCloner();
  128. $dumper->dump($cloner->cloneVar(123), $out);
  129. $dumper->dump($cloner->cloneVar(456), $out);
  130. $out = stream_get_contents($out, -1, 0);
  131. $this->assertSame(<<<'EOTXT'
  132. <foo></foo><bar><span class=sf-dump-num>123</span>
  133. </bar>
  134. <bar><span class=sf-dump-num>456</span>
  135. </bar>
  136. EOTXT
  137. ,
  138. $out
  139. );
  140. }
  141. }