No Description

DumpDataCollectorTest.php 3.7KB

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