8000 [WebProfilerBundle] Fix dump header not being displayed · symfony/symfony@21f3389 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21f3389

Browse files
committed
[WebProfilerBundle] Fix dump header not being displayed
1 parent 6303708 commit 21f3389

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Bundle\WebProfilerBundle\Tests\Twig;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension;
16+
use Symfony\Component\VarDumper\Cloner\VarCloner;
17+
use Twig\Environment;
18+
use Twig\Extension\CoreExtension;
19+
use Twig\Extension\EscaperExtension;
20+
use Twig\Loader\LoaderInterface;
21+
22+
class WebProfilerExtensionTest extends TestCase
23+
{
24+
/**
25+
* @dataProvider provideMessages
26+
*/
27+
public function testDumpHeaderIsDisplayed(string $message, array $context, bool $dump1HasHeader, bool $dump2HasHeader)
28+
{
29+
class_exists(CoreExtension::class); // Load twig_convert_encoding()
30+
class_exists(EscaperExtension::class); // Load twig_escape_filter()
31+
32+
$twigEnvironment = $this->mockTwigEnvironment();
33+
$varCloner = new VarCloner();
34+
35+
$webProfilerExtension = new WebProfilerExtension();
36+
37+
$needle = 'window.Sfdump';
38+
39+
$dump1 = $webProfilerExtension->dumpLog($twigEnvironment, $message, $varCloner->cloneVar($context));
40+
self::assertSame($dump1HasHeader, str_contains($dump1, $needle));
41+
42+
$dump2 = $webProfilerExtension->dumpData($twigEnvironment, $varCloner->cloneVar([]));
43+
self::assertSame($dump2HasHeader, str_contains($dump2, $needle));
44+
}
45+
46+
public function provideMessages(): iterable
47+
{
48+
yield ['Some message', ['foo' => 'foo', 'bar' => 'bar'], false, true];
49+
yield ['Some message {@see some text}', ['foo' => 'foo', 'bar' => 'bar'], false, true];
50+
yield ['Some message {foo}', ['foo' => 'foo', 'bar' => 'bar'], true, false];
51+
yield ['Some message {foo}', ['bar' => 'bar'], false, true];
52+
}
53+
54+
private function mockTwigEnvironment()
55+
{
56+
$twigEnvironment = $this->createMock(Environment::class);
57+
58+
$loader = $this->createMock(LoaderInterface::class);
59+
$loader
60+
->expects($this->any())
61+
->method('exists')
62+
->willReturn(true);
63+
64+
$twigEnvironment->expects($this->any())->method('getLoader')->willReturn($loader);
65+
66+
return $twigEnvironment;
67+
}
68+
}

src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,19 @@ public function dumpLog(Environment $env, $message, Data $context = null)
9898
$message = twig_escape_filter($env, $message);
9999
$message = preg_replace('/&quot;(.*?)&quot;/', '&quot;<b>$1</b>&quot;', $message);
100100

101-
if (null === $context || !str_contains($message, '{')) {
101+
$replacements = [];
102+
foreach ($context ?? [] as $k => $v) {
103+
$k = '{'.twig_escape_filter($env, $k).'}';
104+
if (str_contains($message, $k)) {
105+
$replacements[$k] = $v;
106+
}
107+
}
108+
109+
if (!$replacements) {
102110
return '<span class="dump-inline">'.$message.'</span>';
103111
}
104112

105-
$replacements = [];
106-
foreach ($context as $k => $v) {
107-
$k = '{'.twig_escape_filter($env, $k).'}';
113+
foreach ($replacements as $k => $v) {
108114
$replacements['&quot;<b>'.$k.'</b>&quot;'] = $replacements['&quot;'.$k.'&quot;'] = $replacements[$k] = $this->dumpData($env, $v);
109115
}
110116

0 commit comments

Comments
 (0)
0