Skip to content

Commit d0942df

Browse files
committed
Add cache-clearning functionality
1 parent aaa0dba commit d0942df

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/Property/Selector/SpecificityCalculator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
* Utility class to calculate the specificity of a CSS selector.
99
*
1010
* The results are cached to avoid recalculating the specificity of the same selector multiple times.
11-
*
12-
* @internal
1311
*/
1412
final class SpecificityCalculator
1513
{
@@ -56,7 +54,11 @@ final class SpecificityCalculator
5654
private static $cache = [];
5755

5856
/**
57+
* Calculates the specificity of the given CSS selector.
58+
*
5959
* @return int<0, max>
60+
*
61+
* @internal
6062
*/
6163
public static function calculate(string $selector): int
6264
{
@@ -72,4 +74,12 @@ public static function calculate(string $selector): int
7274

7375
return self::$cache[$selector];
7476
}
77+
78+
/**
79+
* Clears the cache in order to lower memory usage.
80+
*/
81+
public static function clearCache(): void
82+
{
83+
self::$cache = [];
84+
}
7585
}

tests/Unit/Property/Selector/SpecificityCalculatorTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
final class SpecificityCalculatorTest extends TestCase
1414
{
15+
protected function tearDown(): void
16+
{
17+
SpecificityCalculator::clearCache();
18+
}
19+
1520
/**
1621
* @return array<string, array{0: non-empty-string, 1: int<0, max>}>
1722
*/
@@ -42,4 +47,48 @@ public function calculateReturnsSpecificityForProvidedSelector(
4247
): void {
4348
self::assertSame($expectedSpecificity, SpecificityCalculator::calculate($selector));
4449
}
50+
51+
/**
52+
* @test
53+
*
54+
* @param non-empty-string $selector
55+
* @param int<0, max> $expectedSpecificity
56+
*
57+
* @dataProvider provideSelectorsAndSpecificities
58+
*/
59+
public function calculateAfterClearingCacheReturnsSpecificityForProvidedSelector(
60+
string $selector,
61+
int $expectedSpecificity
62+
): void {
63+
SpecificityCalculator::clearCache();
64+
65+
self::assertSame($expectedSpecificity, SpecificityCalculator::calculate($selector));
66+
}
67+
68+
/**
69+
* @test
70+
*/
71+
public function calculateCalledTwoTimesReturnsSameSpecificityForProvidedSelector(): void
72+
{
73+
$selector = '#test .help';
74+
75+
$firstResult = SpecificityCalculator::calculate($selector);
76+
$secondResult = SpecificityCalculator::calculate($selector);
77+
78+
self::assertSame($firstResult, $secondResult);
79+
}
80+
81+
/**
82+
* @test
83+
*/
84+
public function calculateCalledReturnsSameSpecificityForProvidedSelectorBeforeAndAfterClearingCache(): void
85+
{
86+
$selector = '#test .help';
87+
88+
$firstResult = SpecificityCalculator::calculate($selector);
89+
SpecificityCalculator::clearCache();
90+
$secondResult = SpecificityCalculator::calculate($selector);
91+
92+
self::assertSame($firstResult, $secondResult);
93+
}
4594
}

0 commit comments

Comments
 (0)