DumpExtensionTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Bridge\Twig\Tests\Extension;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\DumpExtension;
  13. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  14. use Symfony\Component\VarDumper\VarDumper;
  15. use Symfony\Component\VarDumper\Cloner\VarCloner;
  16. use Twig\Environment;
  17. use Twig\Loader\ArrayLoader;
  18. class DumpExtensionTest extends TestCase
  19. {
  20. /**
  21. * @dataProvider getDumpTags
  22. */
  23. public function testDumpTag($template, $debug, $expectedOutput, $expectedDumped)
  24. {
  25. $extension = new DumpExtension(new VarCloner());
  26. $twig = new Environment(new ArrayLoader(array('template' => $template)), array(
  27. 'debug' => $debug,
  28. 'cache' => false,
  29. 'optimizations' => 0,
  30. ));
  31. $twig->addExtension($extension);
  32. $dumped = null;
  33. $exception = null;
  34. $prevDumper = VarDumper::setHandler(function ($var) use (&$dumped) { $dumped = $var; });
  35. try {
  36. $this->assertEquals($expectedOutput, $twig->render('template'));
  37. } catch (\Exception $exception) {
  38. }
  39. VarDumper::setHandler($prevDumper);
  40. if (null !== $exception) {
  41. throw $exception;
  42. }
  43. $this->assertSame($expectedDumped, $dumped);
  44. }
  45. public function getDumpTags()
  46. {
  47. return array(
  48. array('A{% dump %}B', true, 'AB', array()),
  49. array('A{% set foo="bar"%}B{% dump %}C', true, 'ABC', array('foo' => 'bar')),
  50. array('A{% dump %}B', false, 'AB', null),
  51. );
  52. }
  53. /**
  54. * @dataProvider getDumpArgs
  55. */
  56. public function testDump($context, $args, $expectedOutput, $debug = true)
  57. {
  58. $extension = new DumpExtension(new VarCloner());
  59. $twig = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array(
  60. 'debug' => $debug,
  61. 'cache' => false,
  62. 'optimizations' => 0,
  63. ));
  64. array_unshift($args, $context);
  65. array_unshift($args, $twig);
  66. $dump = call_user_func_array(array($extension, 'dump'), $args);
  67. if ($debug) {
  68. $this->assertStringStartsWith('<script>', $dump);
  69. $dump = preg_replace('/^.*?<pre/', '<pre', $dump);
  70. $dump = preg_replace('/sf-dump-\d+/', 'sf-dump', $dump);
  71. }
  72. $this->assertEquals($expectedOutput, $dump);
  73. }
  74. public function getDumpArgs()
  75. {
  76. return array(
  77. array(array(), array(), '', false),
  78. array(array(), array(), "<pre class=sf-dump id=sf-dump data-indent-pad=\" \">[]\n</pre><script>Sfdump(\"sf-dump\")</script>\n"),
  79. array(
  80. array(),
  81. array(123, 456),
  82. "<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"
  83. ."<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-num>456</span>\n</pre><script>Sfdump(\"sf-dump\")</script>\n",
  84. ),
  85. array(
  86. array('foo' => 'bar'),
  87. array(),
  88. "<pre class=sf-dump id=sf-dump data-indent-pad=\" \"><span class=sf-dump-note>array:1</span> [<samp>\n"
  89. ." \"<span class=sf-dump-key>foo</span>\" => \"<span class=sf-dump-str title=\"3 characters\">bar</span>\"\n"
  90. ."</samp>]\n"
  91. ."</pre><script>Sfdump(\"sf-dump\")</script>\n",
  92. ),
  93. );
  94. }
  95. public function testCustomDumper()
  96. {
  97. $output = '';
  98. $lineDumper = function ($line) use (&$output) {
  99. $output .= $line;
  100. };
  101. $dumper = new HtmlDumper($lineDumper);
  102. $dumper->setDumpHeader('');
  103. $dumper->setDumpBoundaries(
  104. '<pre class=sf-dump-test id=%s data-indent-pad="%s">',
  105. '</pre><script>Sfdump("%s")</script>'
  106. );
  107. $extension = new DumpExtension(new VarCloner(), $dumper);
  108. $twig = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array(
  109. 'debug' => true,
  110. 'cache' => false,
  111. 'optimizations' => 0,
  112. ));
  113. $dump = $extension->dump($twig, array(), 'foo');
  114. $dump = preg_replace('/sf-dump-\d+/', 'sf-dump', $dump);
  115. $this->assertEquals(
  116. '<pre class=sf-dump-test id=sf-dump data-indent-pad=" ">"'.
  117. "<span class=sf-dump-str title=\"3 characters\">foo</span>\"\n".
  118. "</pre><script>Sfdump(\"sf-dump\")</script>\n",
  119. $dump,
  120. 'Custom dumper should be used to dump data.'
  121. );
  122. $this->assertEmpty($output, 'Dumper output should be ignored.');
  123. }
  124. }