8000 minor #38898 [Cache] Add tests on CacheDataCollector (ScullWM) · lucasaba/symfony@038497c · GitHub
[go: up one dir, main page]

Skip to content

Commit 038497c

Browse files
committed
minor symfony#38898 [Cache] Add tests on CacheDataCollector (ScullWM)
This PR was merged into the 4.4 branch. Discussion ---------- [Cache] Add tests on CacheDataCollector | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. --> The `calculateStatistics()` method of `Symfony\Component\Cache\DataCollector\CacheDataCollector` contain a lot of logic and manipulate multi-dimensional array that could be refactor with VO. But before doing this, I would add test on this part. Commits ------- 7b4310f Add tests on CacheDataCollector
2 parents ecf9859 + 7b4310f commit 038497c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Cache\Tests\Marshaller;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Cache\Adapter\TraceableAdapter;
16+
use Symfony\Component\Cache\DataCollector\CacheDataCollector;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
20+
class CacheDataCollectorTest extends TestCase
21+
{
22+
private const INSTANCE_NAME = 'test';
23+
24+
public function testEmptyDataCollector()
25+
{
26+
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([]);
27+
28+
$this->assertEquals($statistics[self::INSTANCE_NAME]['calls'], 0, 'calls');
29+
$this->assertEquals($statistics[self::INSTANCE_NAME]['reads'], 0, 'reads');
30+
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 0, 'hits');
31+
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 0, 'misses');
32+
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 0, 'writes');
33+
}
34+
35+
public function testOneEventDataCollector()
36+
{
37+
$traceableAdapterEvent = new \stdClass();
38+
$traceableAdapterEvent->name = 'getItem';
39+
$traceableAdapterEvent->start = 0;
40+
$traceableAdapterEvent->end = 0;
41+
$traceableAdapterEvent->hits = 0;
42+
43+
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([$traceableAdapterEvent]);
44+
45+
$this->assertEquals($statistics[self::INSTANCE_NAME]['calls'], 1, 'calls');
46+
$this->assertEquals($statistics[self::INSTANCE_NAME]['reads'], 1, 'reads');
47+
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 0, 'hits');
48+
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 1, 'misses');
49+
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 0, 'writes');
50+
}
51+
52+
public function testHitedEventDataCollector()
53+
{
54+
$traceableAdapterEvent = new \stdClass();
55+
$traceableAdapterEvent->name = 'hasItem';
56+
$traceableAdapterEvent->start = 0;
57+
$traceableAdapterEvent->end = 0;
58+
$traceableAdapterEvent->hits = 1;
59+
$traceableAdapterEvent->misses = 0;
60+
$traceableAdapterEvent->result = ['foo' => false];
61+
62+
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([$traceableAdapterEvent]);
63+
64+
$this->assertEquals($statistics[self::INSTANCE_NAME]['calls'], 1, 'calls');
65+
$this->assertEquals($statistics[self::INSTANCE_NAME]['reads'], 1, 'reads');
66+
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 1, 'hits');
67+
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 0, 'misses');
68+
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 0, 'writes');
69+
}
70+
71+
public function testSavedEventDataCollector()
72+
{
73+
$traceableAdapterEvent = new \stdClass();
74+
$traceableAdapterEvent->name = 'save';
75+
$traceableAdapterEvent->start = 0;
76+
$traceableAdapterEvent->end = 0;
77+
78+
$statistics = $this->getCacheDataCollectorStatisticsFromEvents([$traceableAdapterEvent]);
79+
80+
$this->assertEquals($statistics[self::INSTANCE_NAME]['calls'], 1, 'calls');
81+
$this->assertEquals($statistics[self::INSTANCE_NAME]['reads'], 0, 'reads');
82+
$this->assertEquals($statistics[self::INSTANCE_NAME]['hits'], 0, 'hits');
83+
$this->assertEquals($statistics[self::INSTANCE_NAME]['misses'], 0, 'misses');
84+
$this->assertEquals($statistics[self::INSTANCE_NAME]['writes'], 1, 'writes');
85+
}
86+
87+
private function getCacheDataCollectorStatisticsFromEvents(array $traceableAdapterEvents)
88+
{
89+
$traceableAdapterMock = $this->createMock(TraceableAdapter::class);
90+
$traceableAdapterMock->method('getCalls')->willReturn($traceableAdapterEvents);
91+
92+
$cacheDataCollector = new CacheDataCollector();
93+
$cacheDataCollector->addInstance(self::INSTANCE_NAME, $traceableAdapterMock);
94+
$cacheDataCollector->collect(new Request(), new Response());
95+
96+
return $cacheDataCollector->getStatistics();
97+
}
98+
}

src/Symfony/Component/Cache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"symfony/config": "^4.2|^5.0",
3838
"symfony/dependency-injection": "^3.4|^4.1|^5.0",
3939
"symfony/filesystem": "^4.4|^5.0",
40+
"symfony/http-kernel": "^4.4|^5.0",
4041
"symfony/var-dumper": "^4.4|^5.0"
4142
},
4243
"conflict": {

0 commit comments

Comments
 (0)
0