HtmlDumperTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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;
  11. use Symfony\Component\VarDumper\Cloner\VarCloner;
  12. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class HtmlDumperTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testGet()
  19. {
  20. require __DIR__.'/Fixtures/dumb-var.php';
  21. $dumper = new HtmlDumper('php://output');
  22. $dumper->setDumpHeader('<foo></foo>');
  23. $dumper->setDumpBoundaries('<bar>', '</bar>');
  24. $cloner = new VarCloner();
  25. $cloner->addCasters(array(
  26. ':stream' => function ($res, $a) {
  27. unset($a['uri'], $a['wrapper_data']);
  28. return $a;
  29. },
  30. ));
  31. $data = $cloner->cloneVar($var);
  32. ob_start();
  33. $dumper->dump($data);
  34. $out = ob_get_clean();
  35. $out = preg_replace('/[ \t]+$/m', '', $out);
  36. $var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8');
  37. $intMax = PHP_INT_MAX;
  38. preg_match('/sf-dump-\d+/', $out, $dumpId);
  39. $dumpId = $dumpId[0];
  40. $res = (int) $var['res'];
  41. $closure54 = '';
  42. $r = defined('HHVM_VERSION') ? '' : '<a class=sf-dump-ref>#%d</a>';
  43. if (PHP_VERSION_ID >= 50400) {
  44. $closure54 = <<<EOTXT
  45. <span class=sf-dump-meta>class</span>: "<span class=sf-dump-str title="48 characters">Symfony\Component\VarDumper\Tests\HtmlDumperTest</span>"
  46. <span class=sf-dump-meta>this</span>: <abbr title="Symfony\Component\VarDumper\Tests\HtmlDumperTest" class=sf-dump-note>HtmlDumperTest</abbr> {{$r} &#8230;}
  47. EOTXT;
  48. }
  49. $this->assertStringMatchesFormat(
  50. <<<EOTXT
  51. <foo></foo><bar><span class=sf-dump-note>array:24</span> [<samp>
  52. "<span class=sf-dump-key>number</span>" => <span class=sf-dump-num>1</span>
  53. <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>
  54. "<span class=sf-dump-key>const</span>" => <span class=sf-dump-num>1.1</span>
  55. <span class=sf-dump-key>1</span> => <span class=sf-dump-const>true</span>
  56. <span class=sf-dump-key>2</span> => <span class=sf-dump-const>false</span>
  57. <span class=sf-dump-key>3</span> => <span class=sf-dump-num>NAN</span>
  58. <span class=sf-dump-key>4</span> => <span class=sf-dump-num>INF</span>
  59. <span class=sf-dump-key>5</span> => <span class=sf-dump-num>-INF</span>
  60. <span class=sf-dump-key>6</span> => <span class=sf-dump-num>{$intMax}</span>
  61. "<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="5 characters">d&#233;j&#224;</span>\\n"
  62. <span class=sf-dump-key>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;</span>\\x00"
  63. "<span class=sf-dump-key>[]</span>" => []
  64. "<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp>
  65. <span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
  66. <span class=sf-dump-meta>stream_type</span>: "<span class=sf-dump-str title="5 characters">STDIO</span>"
  67. <span class=sf-dump-meta>mode</span>: "<span class=sf-dump-str>r</span>"
  68. <span class=sf-dump-meta>unread_bytes</span>: <span class=sf-dump-num>0</span>
  69. <span class=sf-dump-meta>seekable</span>: <span class=sf-dump-const>true</span>
  70. <span class=sf-dump-meta>timed_out</span>: <span class=sf-dump-const>false</span>
  71. <span class=sf-dump-meta>blocked</span>: <span class=sf-dump-const>true</span>
  72. <span class=sf-dump-meta>eof</span>: <span class=sf-dump-const>false</span>
  73. <span class=sf-dump-meta>options</span>: []
  74. </samp>}
  75. "<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>
  76. +<span class=sf-dump-public title="Public property">foo</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
  77. +"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"
  78. </samp>}
  79. "<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {{$r}<samp>{$closure54}
  80. <span class=sf-dump-meta>parameters</span>: <span class=sf-dump-note>array:2</span> [<samp>
  81. "<span class=sf-dump-key>\$a</span>" => []
  82. "<span class=sf-dump-key>&amp;\$b</span>" => <span class=sf-dump-note>array:2</span> [<samp>
  83. "<span class=sf-dump-key>typeHint</span>" => "<span class=sf-dump-str title="3 characters">PDO</span>"
  84. "<span class=sf-dump-key>default</span>" => <span class=sf-dump-const>null</span>
  85. </samp>]
  86. </samp>]
  87. <span class=sf-dump-meta>file</span>: "<span class=sf-dump-str title="%d characters">{$var['file']}</span>"
  88. <span class=sf-dump-meta>line</span>: "<span class=sf-dump-str title="%d characters">{$var['line']} to {$var['line']}</span>"
  89. </samp>}
  90. "<span class=sf-dump-key>line</span>" => <span class=sf-dump-num>{$var['line']}</span>
  91. "<span class=sf-dump-key>nobj</span>" => <span class=sf-dump-note>array:1</span> [<samp>
  92. <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>}
  93. </samp>]
  94. "<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>
  95. <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>]
  96. </samp>]
  97. <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>
  98. "<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>}
  99. "<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>}
  100. "<span class=sf-dump-key>snobj2</span>" => {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="3 occurrences">#%d</a>}
  101. "<span class=sf-dump-key>file</span>" => "<span class=sf-dump-str title="%d characters">{$var['file']}</span>"
  102. b"<span class=sf-dump-key>bin-key-&#233;</span>" => ""
  103. </samp>]
  104. </bar>
  105. EOTXT
  106. ,
  107. $out
  108. );
  109. }
  110. public function testCharset()
  111. {
  112. if (!extension_loaded('mbstring')) {
  113. $this->markTestSkipped('This test requires mbstring.');
  114. }
  115. $var = mb_convert_encoding('Словарь', 'CP1251', 'UTF-8');
  116. $dumper = new HtmlDumper('php://output', 'CP1251');
  117. $dumper->setDumpHeader('<foo></foo>');
  118. $dumper->setDumpBoundaries('<bar>', '</bar>');
  119. $cloner = new VarCloner();
  120. $data = $cloner->cloneVar($var);
  121. $out = fopen('php://memory', 'r+b');
  122. $dumper->dump($data, $out);
  123. rewind($out);
  124. $out = stream_get_contents($out);
  125. $this->assertStringMatchesFormat(
  126. <<<EOTXT
  127. <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>"
  128. </bar>
  129. EOTXT
  130. ,
  131. $out
  132. );
  133. }
  134. }