8000 [HttpKernel] add tests for DumpDataCollector · symfony/symfony@49f13c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49f13c6

Browse files
[HttpKernel] add tests for DumpDataCollector
1 parent 081363c commit 49f13c6

File tree

2 files changed

+111
-28
lines changed

2 files changed

+111
-28
lines changed

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

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,37 @@
2626
class DumpDataCollector extends DataCollector implements DataDumperInterface
2727
{
2828
private $stopwatch;
29+
private $dataCount = 0;
2930
private $isCollected = true;
30-
private $clonesRoot;
3131
private $clonesCount = 0;
32+
private $clonesIndex = 0;
33+
private $rootRefs;
3234

3335
public function __construct(Stopwatch $stopwatch = null)
3436
{
3537
$this->stopwatch = $stopwatch;
36-
$this->clonesRoot = $this;
38+
39+
// All clones share these properties by reference:
40+
$this->rootRefs = array(
41+
&$this->data,
42+
&$this->dataCount,
43+
&$this->isCollected,
44+
&$this->clonesCount,
45+
);
3746
}
3847

3948
public function __clone()
4049
{
41-
$this->data = array();
42-
$this->clonesRoot->clonesCount++;
50+
$this->clonesIndex = ++$this->clonesCount;
4351
}
4452

4553
public function dump(Data $data)
4654
{
4755
if ($this->stopwatch) {
4856
$this->stopwatch->start('dump');
4957
}
50-
if ($this->clonesRoot->< 8000 span class=pl-c1>isCollected) {
51-
$this->clonesRoot->isCollected = false;
52-
register_shutdown_function(array($this->clonesRoot, 'flushDumps'));
58+
if ($this->isCollected) {
59+
$this->isCollected = false;
5360
}
5461

5562
$trace = PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS : true;
@@ -66,7 +73,7 @@ public function dump(Data $data)
6673

6774
for ($i = 1; $i < 7; ++$i) {
6875
if (isset($trace[$i]['class'], $trace[$i]['function'])
69-
&& 'dump' === $trace[$i]['function']
76+
&& ('dump' === $trace[$i]['function'] || 'debug' === $trace[$i]['function'])
7077
&& 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
7178
) {
7279
$file = $trace[$i]['file'];
@@ -107,7 +114,8 @@ public function dump(Data $data)
107114
$name = substr($file, strrpos($file, '/') + 1);
108115
}
109116

110-
$this->clonesRoot->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
117+
$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
118+
++$this->dataCount;
111119

112120
if ($this->stopwatch) {
113121
$this->stopwatch->stop('dump');
@@ -120,23 +128,28 @@ public function collect(Request $request, Response $response, \Exception $except
120128

121129
public function serialize()
122130
{
123-
$ser = serialize($this->clonesRoot->data);
124-
$this->clonesRoot->data = array();
125-
$this->clonesRoot->isCollected = true;
131+
if ($this->clonesCount !== $this->clonesIndex) {
132+
return 'a:0:{}';
133+
}
134+
135+
$ser = serialize($this->data);
136+
$this->data = array();
137+
$this->dataCount = 0;
138+
$this->isCollected = true;
126139

127140
return $ser;
128141
}
129142

130143
public function unserialize($data)
131144
{
132145
parent::unserialize($data);
133-
134-
$this->clonesRoot = $this;
146+
$this->dataCount = count($this->data);
147+
self::__construct($this->stopwatch);
135148
}
136149

137150
public function getDumpsCount()
138151
{
139-
return count($this->clonesRoot->data);
152+
return $this->dataCount;
140153
}
141154

142155
public function getDumpsExcerpts()
@@ -193,7 +206,7 @@ public function getDumps($getData = false)
193206
}
194207
$dumps = array();
195208

196-
foreach ($this->clonesRoot->data as $dump) {
209+
foreach ($this->data as $dump) {
197210
$json = '';
198211
if ($getData) {
199212
$dumper->dump($dump['data'], function ($line) use (&$json) {$json .= $line;});
@@ -210,11 +223,11 @@ public function getName()
210223
return 'dump';
211224
}
212225

213-
public function flushDumps()
226+
public function __destruct()
214227
{
215-
if (0 === $this->clonesRoot->clonesCount-- && !$this->clonesRoot->isCollected && $this->clonesRoot->data) {
216-
$this->clonesRoot->clonesCount = 0;
217-
$this->clonesRoot->isCollected = true;
228+
if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
229+
$this->clonesCount = 0;
230+
$this->isCollected = true;
218231

219232
$h = headers_list();
220233
$i = count($h);
@@ -223,31 +236,32 @@ public function flushDumps()
223236
--$i;
224237
}
225238

226-
if (stripos($h[$i], 'html')) {
239+
if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
227240
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
228-
$dumper = new HtmlDumper();
241+
$dumper = new HtmlDumper('php://output');
229242
} else {
230-
$dumper = new CliDumper();
243+
$dumper = new CliDumper('php://output');
231244
$dumper->setColors(false);
232245
}
233246

234-
foreach ($this->clonesRoot->data as $i => $dump) {
235-
$this->clonesRoot->data[$i] = null;
247+
foreach ($this->data as $i => $dump) {
248+
$this->data[$i] = null;
236249

237250
if ($dumper instanceof HtmlDumper) {
238251
$dump['name'] = htmlspecialchars($dump['name'], ENT_QUOTES, 'UTF-8');
239252
$dump['file'] = htmlspecialchars($dump['file'], ENT_QUOTES, 'UTF-8');
240253
if ('' !== $dump['file']) {
241254
$dump['name'] = "<abbr title=\"{$dump['file']}\">{$dump['name']}</abbr>";
242255
}
243-
echo "\n<br><span class=\"sf-dump-meta\">in {$dump['name']} on line {$dump['line']}:</span>";
256+
echo "\n<span class=\"sf-dump-meta\">{$dump['name']} on line {$dump['line']}:</span>";
244257
} else {
245-
echo "\nin {$dump['name']} on line {$dump['line']}:\n\n";
258+
echo "{$dump['name']} on line {$dump['line']}:\n";
246259
}
247260
$dumper->dump($dump['data']);
248261
}
249262

250-
$this->clonesRoot->data = array();
263+
$this->data = array();
264+
$this->dataCount = 0;
251265
}
252266
}
253267
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Component\HttpKernel\Tests\DataCollector;
13+
14+
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
15+
use Symfony\Component\VarDumper\Cloner\Data;
16+
17+
/**
18+
* DumpDataCollectorTest
19+
*
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class DumpDataCollectorTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testDump()
25+
{
26+
$data = new Data(array(array(123)));
27+
28+
$collector = new DumpDataCollector();
29+
30+
$this->assertSame('dump', $collector->getName());
31+
32+
$collector->dump($data); $line = __LINE__;
33+
$this->assertSame(1, $collector->getDumpsCount());
34+
35+
$xDump = array(
36+
array(
37+
'data' => '',
38+
'name' => 'DumpDataCollectorTest.php',
39+
'file' => __FILE__,
40+
'line' => $line,
41+
'fileExcerpt' => false,
42+
),
43+
);
44+
45+
$this->assertSame($xDump, $collector->getDumps());
46+
47+
$xDump[0]['data'] = '123';
48+
$this->assertSame($xDump, $collector->getDumps(true));
49+
50+
$this->assertStringStartsWith(
51+
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":1:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}}s:4:"name";s:25:"DumpDataCollectorTest.php";',
52+
str_replace("\0", '', $collector->serialize())
53+
);
54+
55+
$this->assertSame(0, $collector->getDumpsCount());
56+
$this->assertSame('a:0:{}', $collector->serialize());
57+
}
58+
59+
public function testFlush()
60+
{
61+
$data = new Data(array(array(456)));
62+
$collector = new DumpDataCollector();
63+
$collector->dump($data); $line = __LINE__;
64+
65+
ob_start();
66+
$collector = null;
67+
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
68+
}
69+
}

0 commit comments

Comments
 (0)
0