Skip to content

Commit c8746a4

Browse files
[DebugBundle] add tests for twig and for the bundle
1 parent 8d5d970 commit c8746a4

File tree

7 files changed

+185
-12
lines changed

7 files changed

+185
-12
lines changed

src/Symfony/Bridge/Twig/Node/DumpNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function compile(\Twig_Compiler $compiler)
6262
} else {
6363
$compiler
6464
->addDebugInfo($this)
65-
->write('\Symfony\Component\VarDumper\VarDumper::dump(array(')
65+
->write('\Symfony\Component\VarDumper\VarDumper::dump(array('."\n")
6666
->indent()
6767
;
6868
foreach ($values as $node) {
@@ -80,13 +80,13 @@ public function compile(\Twig_Compiler $compiler)
8080
}
8181
$compiler
8282
->outdent()
83-
->raw("));\n")
83+
->write("));\n")
8484
;
8585
}
8686

8787
$compiler
8888
->outdent()
89-
->write("}\n")
89+
->raw("}\n")
9090
;
9191
}
9292
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Extension;
13+
14+
use Symfony\Bridge\Twig\Extension\DumpExtension;
15+
use Symfony\Component\VarDumper\VarDumper;
16+
17+
class DumpExtensionTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider getDumpParams
21+
*/
22+
public function testDebugDump($template, $debug, $expectedOutput, $expectedDumped)
23+
{
24+
$twig = new \Twig_Environment(new \Twig_Loader_String(), array(
25+
'debug' => $debug,
26+
'cache' => false,
27+
'optimizations' => 0,
28+
));
29+
$twig->addExtension(new DumpExtension());
30+
31+
$dumped = null;
32+
$exception = null;
33+
$prevDumper = VarDumper::setHandler(function ($var) use (&$dumped) {$dumped = $var;});
34+
35+
try {
36+
$this->assertEquals($expectedOutput, $twig->render($template));
37+
} catch (\Exception $exception) {
38+
}
39+
40+
VarDumper::setHandler($prevDumper);
41+
42+
if (null !== $exception) {
43+
throw $exception;
44+
}
45+
46+
$this->assertSame($expectedDumped, $dumped);
47+
}
48+
49+
public function getDumpParams()
50+
{
51+
return array(
52+
array('A{% dump %}B', true, 'AB', array()),
53+
array('A{% set foo="bar"%}B{% dump %}C', true, 'ABC', array('foo' => 'bar')),
54+
array('A{% dump %}B', false, 'AB', null),
55+
);
56+
}
57+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Node;
13+
14+
use Symfony\Bridge\Twig\Node\DumpNode;
15+
16+
class DumpNodeTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testNoVar()
19+
{
20+
$node = new DumpNode('bar', null, 7);
21+
22+
$env = new \Twig_Environment();
23+
$compiler = new \Twig_Compiler($env);
24+
25+
$expected = <<<'EOTXT'
26+
if ($this->env->isDebug()) {
27+
$barvars = array();
28+
foreach ($context as $barkey => $barval) {
29+
if (!$barval instanceof \Twig_Template) {
30+
$barvars[$barkey] = $barval;
31+
}
32+
}
33+
// line 7
34+
\Symfony\Component\VarDumper\VarDumper::dump($barvars);
35+
}
36+
37+
EOTXT;
38+
39+
$this->assertSame($expected, $compiler->compile($node)->getSource());
40+
}
41+
42+
public function testOneVar()
43+
{
44+
$vars = new \Twig_Node(array(
45+
new \Twig_Node_Expression_Name('foo', 7),
46+
));
47+
$node = new DumpNode('bar', $vars, 7);
48+
49+
$env = new \Twig_Environment();
50+
$compiler = new \Twig_Compiler($env);
51+
52+
$expected = <<<'EOTXT'
53+
if ($this->env->isDebug()) {
54+
// line 7
55+
\Symfony\Component\VarDumper\VarDumper::dump(%foo%);
56+
}
57+
58+
EOTXT;
59+
$expected = preg_replace('/%(.*?)%/', version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["$1"]) ? $context["$1"] : null)' : '$this->getContext($context, "$1")', $expected);
60+
61+
$this->assertSame($expected, $compiler->compile($node)->getSource());
62+
}
63+
64+
public function testMultiVars()
65+
{
66+
$vars = new \Twig_Node(array(
67+
new \Twig_Node_Expression_Name('foo', 7),
68+
new \Twig_Node_Expression_Name('bar', 7),
69+
));
70+
$node = new DumpNode('bar', $vars, 7);
71+
72+
$env = new \Twig_Environment();
73+
$compiler = new \Twig_Compiler($env);
74+
75+
$expected = <<<'EOTXT'
76+
if ($this->env->isDebug()) {
77+
// line 7
78+
\Symfony\Component\VarDumper\VarDumper::dump(array(
79+
"foo" => %foo%,
80+
"bar" => %bar%,
81+
));
82+
}
83+
84+
EOTXT;
85+
$expected = preg_replace('/%(.*?)%/', version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["$1"]) ? $context["$1"] : null)' : '$this->getContext($context, "$1")', $expected);
86+
87+
$this->assertSame($expected, $compiler->compile($node)->getSource());
88+
}
89+
}

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function dump(Data $data)
5454

5555
$trace = PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS : true;
5656
if (PHP_VERSION_ID >= 50400) {
57-
$trace = debug_backtrace($trace, 6);
57+
$trace = debug_backtrace($trace, 7);
5858
} else {
5959
$trace = debug_backtrace($trace);
6060
}
@@ -72,10 +72,11 @@ public function dump(Data $data)
7272
$file = $trace[$i]['file'];
7373
$line = $trace[$i]['line'];
7474

75-
while (++$i < 6) {
76-
if (isset($trace[$i]['function']) && empty($trace[$i]['class'])) {
75+
while (++$i < 7) {
76+
if (isset($trace[$i]['function']) && empty($trace[$i]['class']) && 'call_user_func' !== $trace[$i]['function']) {
7777
$file = $trace[$i]['file'];
7878
$line = $trace[$i]['line'];
79+
7980
break;
8081
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) {
8182
$info = $trace[$i]['object'];

src/Symfony/Component/HttpKernel/EventListener/DumpListener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

14-
use Symfony\Component\Debug\Debug;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1616
use Symfony\Component\HttpKernel\KernelEvents;
17-
use Symfony\Component\DependencyInjection\ContainerInterface;
17+
use Symfony\Component\VarDumper\VarDumper;
1818

1919
/**
2020
* Configures dump() handler.
@@ -43,11 +43,11 @@ public function configure()
4343
$dumper = $this->dumper;
4444
$this->container = null;
4545

46-
Debug::setDumpHandler(function ($var) use ($container, $dumper) {
46+
VarDumper::setHandler(function ($var) use ($container, $dumper) {
4747
$dumper = $container->get($dumper);
4848
$cloner = $container->get('var_dumper.cloner');
4949
$handler = function ($var) use ($dumper, $cloner) {$dumper->dump($cloner->cloneVar($var));};
50-
Debug::setDumpHandler($handler);
50+
VarDumper::setHandler($handler);
5151
$handler($var);
5252
});
5353
}

src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public static function dump($var)
3333
};
3434
}
3535

36-
return call_user_func(self::$handler, $h);
36+
return call_user_func(self::$handler, $var);
3737
}
3838

3939
public static function setHandler($callable)
4040
{
41-
if (!is_callable($callable, true)) {
41+
if (null !== $callable && !is_callable($callable, true)) {
4242
throw new \InvalidArgumentException('Invalid PHP callback.');
4343
}
4444

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
10+
<testsuites>
11+
<testsuite name="Symfony VarDumper Component Test Suite">
12+
<directory>./Tests/</directory>
13+
</testsuite>
14+
</testsuites>
15+
16+
<filter>
17+
<whitelist>
18+
<directory>./</directory>
19+
<exclude>
20+
<directory>./Tests</directory>
21+
<directory>./Resources</directory>
22+
<directory>./vendor</directory>
23+
</exclude>
24+
</whitelist>
25+
</filter>
26+
</phpunit>

0 commit comments

Comments
 (0)