DumpDataCollectorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\HttpKernel\DataCollector\DumpDataCollector;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\VarDumper\Cloner\Data;
  16. /**
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class DumpDataCollectorTest extends TestCase
  20. {
  21. public function testDump()
  22. {
  23. $data = new Data(array(array(123)));
  24. $collector = new DumpDataCollector();
  25. $this->assertSame('dump', $collector->getName());
  26. $collector->dump($data);
  27. $line = __LINE__ - 1;
  28. $this->assertSame(1, $collector->getDumpsCount());
  29. $dump = $collector->getDumps('html');
  30. $this->assertTrue(isset($dump[0]['data']));
  31. $dump[0]['data'] = preg_replace('/^.*?<pre/', '<pre', $dump[0]['data']);
  32. $dump[0]['data'] = preg_replace('/sf-dump-\d+/', 'sf-dump', $dump[0]['data']);
  33. $xDump = array(
  34. array(
  35. '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",
  36. 'name' => 'DumpDataCollectorTest.php',
  37. 'file' => __FILE__,
  38. 'line' => $line,
  39. 'fileExcerpt' => false,
  40. ),
  41. );
  42. $this->assertEquals($xDump, $dump);
  43. $this->assertStringMatchesFormat('a:3:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
  44. $this->assertSame(0, $collector->getDumpsCount());
  45. $this->assertSame('a:2:{i:0;b:0;i:1;s:5:"UTF-8";}', $collector->serialize());
  46. }
  47. public function testCollectDefault()
  48. {
  49. $data = new Data(array(array(123)));
  50. $collector = new DumpDataCollector();
  51. $collector->dump($data);
  52. $line = __LINE__ - 1;
  53. ob_start();
  54. $collector->collect(new Request(), new Response());
  55. $output = ob_get_clean();
  56. $this->assertSame("DumpDataCollectorTest.php on line {$line}:\n123\n", $output);
  57. $this->assertSame(1, $collector->getDumpsCount());
  58. $collector->serialize();
  59. }
  60. public function testCollectHtml()
  61. {
  62. $data = new Data(array(array(123)));
  63. $collector = new DumpDataCollector(null, 'test://%f:%l');
  64. $collector->dump($data);
  65. $line = __LINE__ - 1;
  66. $file = __FILE__;
  67. $xOutput = <<<EOTXT
  68. <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>:
  69. <span class=sf-dump-num>123</span>
  70. </pre>
  71. EOTXT;
  72. ob_start();
  73. $response = new Response();
  74. $response->headers->set('Content-Type', 'text/html');
  75. $collector->collect(new Request(), $response);
  76. $output = ob_get_clean();
  77. $output = preg_replace('#<(script|style).*?</\1>#s', '', $output);
  78. $output = preg_replace('/sf-dump-\d+/', 'sf-dump', $output);
  79. $this->assertSame($xOutput, trim($output));
  80. $this->assertSame(1, $collector->getDumpsCount());
  81. $collector->serialize();
  82. }
  83. public function testFlush()
  84. {
  85. $data = new Data(array(array(456)));
  86. $collector = new DumpDataCollector();
  87. $collector->dump($data);
  88. $line = __LINE__ - 1;
  89. ob_start();
  90. $collector->__destruct();
  91. $this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
  92. }
  93. }