Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc8746a4

Browse files
[DebugBundle] add tests for twig and for the bundle
1 parent8d5d970 commitc8746a4

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 ($valuesas$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+
namespaceSymfony\Bridge\Twig\Tests\Extension;
13+
14+
useSymfony\Bridge\Twig\Extension\DumpExtension;
15+
useSymfony\Component\VarDumper\VarDumper;
16+
17+
class DumpExtensionTestextends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider getDumpParams
21+
*/
22+
publicfunctiontestDebugDump($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(newDumpExtension());
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+
publicfunctiongetDumpParams()
50+
{
51+
returnarray(
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+
namespaceSymfony\Bridge\Twig\Tests\Node;
13+
14+
useSymfony\Bridge\Twig\Node\DumpNode;
15+
16+
class DumpNodeTestextends \PHPUnit_Framework_TestCase
17+
{
18+
publicfunctiontestNoVar()
19+
{
20+
$node =newDumpNode('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+
publicfunctiontestOneVar()
43+
{
44+
$vars =new \Twig_Node(array(
45+
new \Twig_Node_Expression_Name('foo',7),
46+
));
47+
$node =newDumpNode('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+
publicfunctiontestMultiVars()
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 =newDumpNode('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
namespaceSymfony\Component\HttpKernel\EventListener;
1313

14-
useSymfony\Component\Debug\Debug;
14+
useSymfony\Component\DependencyInjection\ContainerInterface;
1515
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
1616
useSymfony\Component\HttpKernel\KernelEvents;
17-
useSymfony\Component\DependencyInjection\ContainerInterface;
17+
useSymfony\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-
returncall_user_func(self::$handler,$h);
36+
returncall_user_func(self::$handler,$var);
3737
}
3838

3939
publicstaticfunctionsetHandler($callable)
4040
{
41-
if (!is_callable($callable,true)) {
41+
if (null !==$callable &&!is_callable($callable,true)) {
4242
thrownew \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+
<phpunitxmlns: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+
<testsuitename="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)

[8]ページ先頭

©2009-2025 Movatter.jp