Skip to content

Commit 4b8bb5c

Browse files
dcmbrsnicolas-grekas
authored andcommitted
fix cache data collector on late collect
1 parent ed9b51e commit 4b8bb5c

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ public function addInstance(string $name, TraceableAdapter $instance): void
3838

3939
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
4040
{
41-
$empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
42-
$this->data = ['instances' => $empty, 'total' => $empty];
43-
foreach ($this->instances as $name => $instance) {
44-
$this->data['instances']['calls'][$name] = $instance->getCalls();
45-
$this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
46-
}
47-
48-
$this->data['instances']['statistics'] = $this->calculateStatistics();
49-
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
41+
$this->lateCollect();
5042
}
5143

5244
public function reset(): void
@@ -59,7 +51,15 @@ public function reset(): void
5951

6052
public function lateCollect(): void
6153
{
62-
$this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']);
54+
$empty = ['calls' => [], 'adapters' => [], 'config' => [], 'options' => [], 'statistics' => []];
55+
$this->data = ['instances' => $empty, 'total' => $empty];
56+
foreach ($this->instances as $name => $instance) {
57+
$this->data['instances']['calls'][$name] = $instance->getCalls();
58+
$this->data['instances']['adapters'][$name] = get_debug_type($instance->getPool());
59+
}
60+
61+
$this->data['instances']['statistics'] = $this->calculateStatistics();
62+
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
6363
}
6464

6565
public function getName(): string

src/Symfony/Component/Cache/Tests/DataCollector/CacheDataCollectorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public function testCollectBeforeEnd()
104104
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
105105
}
106106

107+
public function testLateCollect()
108+
{
109+
$adapter = new TraceableAdapter(new NullAdapter());
110+
111+
$collector = new CacheDataCollector();
112+
$collector->addInstance(self::INSTANCE_NAME, $adapter);
113+
114+
$adapter->get('foo', function () use ($collector) {
115+
$collector->lateCollect();
116+
117+
return 123;
118+
});
119+
120+
$stats = $collector->getStatistics();
121+
$this->assertGreaterThan(0, $stats[self::INSTANCE_NAME]['time']);
122+
$this->assertEquals($stats[self::INSTANCE_NAME]['hits'], 0, 'hits');
123+
$this->assertEquals($stats[self::INSTANCE_NAME]['misses'], 1, 'misses');
124+
$this->assertEquals($stats[self::INSTANCE_NAME]['calls'], 1, 'calls');
125+
}
126+
107127
private function getCacheDataCollectorStatisticsFromEvents(array $traceableAdapterEvents)
108128
{
109129
$traceableAdapterMock = $this->createMock(TraceableAdapter::class);

0 commit comments

Comments
 (0)