DumpDataCollectorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\HttpKernel\Tests\DataCollector;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
  15. use Symfony\Component\VarDumper\Cloner\Data;
  16. use Symfony\Component\VarDumper\Dumper\CliDumper;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class DumpDataCollectorTest extends TestCase
  21. {
  22. public function testDump()
  23. {
  24. $data = new Data(array(array(123)));
  25. $collector = new DumpDataCollector();
  26. $this->assertSame('dump', $collector->getName());
  27. $collector->dump($data);
  28. $line = __LINE__ - 1;
  29. $this->assertSame(1, $collector->getDumpsCount());
  30. $dump = $collector->getDumps('html');
  31. $this->assertArrayHasKey('data', $dump[0]);
  32. $dump[0]['data'] = preg_replace('/^.*?<pre/', '<pre', $dump[0]['data']);
  33. $dump[0]['data'] = preg_replace('/sf-dump-\d+/', 'sf-dump', $dump[0]['data']);
  34. $xDump = array(
  35. array(
  36. 'data' => "<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-num>123</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n",
  37. 'name' => 'DumpDataCollectorTest.php',
  38. 'file' => __FILE__,
  39. 'line' => $line,
  40. 'fileExcerpt' => false,
  41. ),
  42. );
  43. $this->assertSame($xDump, $dump);
  44. $this->assertStringMatchesFormat('a:3:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
  45. $this->assertSame(0, $collector->getDumpsCount());
  46. $this->assertSame('a:2:{i:0;b:0;i:1;s:5:"UTF-8";}', $collector->serialize());
  47. }
  48. public function testCollectDefault()
  49. {
  50. $data = new Data(array(array(123)));
  51. $collector = new DumpDataCollector();
  52. $collector->dump($data);
  53. $line = __LINE__ - 1;
  54. ob_start();
  55. $collector->collect(new Request(), new Response());
  56. $output = preg_replace("/\033\[[^m]*m/", '', ob_get_clean());
  57. if (\PHP_VERSION_ID >= 50400) {
  58. $this->assertSame("DumpDataCollectorTest.php on line {$line}:\n123\n", $output);
  59. } else {
  60. $this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n123\n", $output);
  61. }
  62. $this->assertSame(1, $collector->getDumpsCount());
  63. $collector->serialize();
  64. }
  65. public function testCollectHtml()
  66. {
  67. $data = new Data(array(array(123)));
  68. $collector = new DumpDataCollector(null, 'test://%f:%l');
  69. $collector->dump($data);
  70. $line = __LINE__ - 1;
  71. $file = __FILE__;
  72. if (\PHP_VERSION_ID >= 50400) {
  73. $xOutput = <<<EOTXT
  74. <pre class=sf-dump id=sf-dump data-indent-pad=" "><a href="test://{$file}:{$line}" title="{$file}"><span class=sf-dump-meta>DumpDataCollectorTest.php</span></a> on line <span class=sf-dump-meta>{$line}</span>:
  75. <span class=sf-dump-num>123</span>
  76. </pre>
  77. EOTXT;
  78. } else {
  79. $len = \strlen("DumpDataCollectorTest.php on line {$line}:");
  80. $xOutput = <<<EOTXT
  81. <pre class=sf-dump id=sf-dump data-indent-pad=" ">"<span class=sf-dump-str title="{$len} characters">DumpDataCollectorTest.php on line {$line}:</span>"
  82. </pre>
  83. <pre class=sf-dump id=sf-dump data-indent-pad=" "><span class=sf-dump-num>123</span>
  84. </pre>
  85. EOTXT;
  86. }
  87. ob_start();
  88. $response = new Response();
  89. $response->headers->set('Content-Type', 'text/html');
  90. $collector->collect(new Request(), $response);
  91. $output = ob_get_clean();
  92. $output = preg_replace('#<(script|style).*?</\1>#s', '', $output);
  93. $output = preg_replace('/sf-dump-\d+/', 'sf-dump', $output);
  94. $this->assertSame($xOutput, trim($output));
  95. $this->assertSame(1, $collector->getDumpsCount());
  96. $collector->serialize();
  97. }
  98. public function testFlush()
  99. {
  100. $data = new Data(array(array(456)));
  101. $collector = new DumpDataCollector();
  102. $collector->dump($data);
  103. $line = __LINE__ - 1;
  104. ob_start();
  105. $collector->__destruct();
  106. $output = preg_replace("/\033\[[^m]*m/", '', ob_get_clean());
  107. if (\PHP_VERSION_ID >= 50400) {
  108. $this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", $output);
  109. } else {
  110. $this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n456\n", $output);
  111. }
  112. }
  113. public function testFlushNothingWhenDataDumperIsProvided()
  114. {
  115. $data = new Data(array(array(456)));
  116. $dumper = new CliDumper('php://output');
  117. $collector = new DumpDataCollector(null, null, null, null, $dumper);
  118. ob_start();
  119. $collector->dump($data);
  120. $line = __LINE__ - 1;
  121. $output = preg_replace("/\033\[[^m]*m/", '', ob_get_clean());
  122. if (\PHP_VERSION_ID >= 50400) {
  123. $this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", $output);
  124. } else {
  125. $this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n456\n", $output);
  126. }
  127. ob_start();
  128. $collector->__destruct();
  129. $this->assertEmpty(ob_get_clean());
  130. }
  131. }