DumpDataCollectorTest.php 5.4KB

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