DumpNodeTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Node;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Node\DumpNode;
  13. use Twig\Compiler;
  14. use Twig\Environment;
  15. use Twig\Node\Expression\NameExpression;
  16. use Twig\Node\Node;
  17. class DumpNodeTest extends TestCase
  18. {
  19. public function testNoVar()
  20. {
  21. $node = new DumpNode('bar', null, 7);
  22. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
  23. $compiler = new Compiler($env);
  24. $expected = <<<'EOTXT'
  25. if ($this->env->isDebug()) {
  26. $barvars = array();
  27. foreach ($context as $barkey => $barval) {
  28. if (!$barval instanceof \Twig\Template) {
  29. $barvars[$barkey] = $barval;
  30. }
  31. }
  32. // line 7
  33. \Symfony\Component\VarDumper\VarDumper::dump($barvars);
  34. }
  35. EOTXT;
  36. $this->assertSame($expected, $compiler->compile($node)->getSource());
  37. }
  38. public function testIndented()
  39. {
  40. $node = new DumpNode('bar', null, 7);
  41. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
  42. $compiler = new Compiler($env);
  43. $expected = <<<'EOTXT'
  44. if ($this->env->isDebug()) {
  45. $barvars = array();
  46. foreach ($context as $barkey => $barval) {
  47. if (!$barval instanceof \Twig\Template) {
  48. $barvars[$barkey] = $barval;
  49. }
  50. }
  51. // line 7
  52. \Symfony\Component\VarDumper\VarDumper::dump($barvars);
  53. }
  54. EOTXT;
  55. $this->assertSame($expected, $compiler->compile($node, 1)->getSource());
  56. }
  57. public function testOneVar()
  58. {
  59. $vars = new Node(array(
  60. new NameExpression('foo', 7),
  61. ));
  62. $node = new DumpNode('bar', $vars, 7);
  63. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
  64. $compiler = new Compiler($env);
  65. $expected = <<<'EOTXT'
  66. if ($this->env->isDebug()) {
  67. // line 7
  68. \Symfony\Component\VarDumper\VarDumper::dump(%foo%);
  69. }
  70. EOTXT;
  71. if (\PHP_VERSION_ID >= 70000) {
  72. $expected = preg_replace('/%(.*?)%/', '($context["$1"] ?? null)', $expected);
  73. } else {
  74. $expected = preg_replace('/%(.*?)%/', '(isset($context["$1"]) ? $context["$1"] : null)', $expected);
  75. }
  76. $this->assertSame($expected, $compiler->compile($node)->getSource());
  77. }
  78. public function testMultiVars()
  79. {
  80. $vars = new Node(array(
  81. new NameExpression('foo', 7),
  82. new NameExpression('bar', 7),
  83. ));
  84. $node = new DumpNode('bar', $vars, 7);
  85. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
  86. $compiler = new Compiler($env);
  87. $expected = <<<'EOTXT'
  88. if ($this->env->isDebug()) {
  89. // line 7
  90. \Symfony\Component\VarDumper\VarDumper::dump(array(
  91. "foo" => %foo%,
  92. "bar" => %bar%,
  93. ));
  94. }
  95. EOTXT;
  96. if (\PHP_VERSION_ID >= 70000) {
  97. $expected = preg_replace('/%(.*?)%/', '($context["$1"] ?? null)', $expected);
  98. } else {
  99. $expected = preg_replace('/%(.*?)%/', '(isset($context["$1"]) ? $context["$1"] : null)', $expected);
  100. }
  101. $this->assertSame($expected, $compiler->compile($node)->getSource());
  102. }
  103. }