Skip to content

Commit e7551d5

Browse files
committed
minor symfony#61101 [FrameworkBundle] Fix cache warmers tests (MatTheCat)
This PR was merged into the 7.3 branch. Discussion ---------- [FrameworkBundle] Fix cache warmers tests | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | N/A | License | MIT While updating tests for symfony#60568 I saw `SerializerCacheWarmerTest` and `ValidatorCacheWarmerTest`’s `testWarmUpWithoutBuilDir` fail. Since there will be no warm up without a build directory it was weird a cache item could get hit in that case. Turns out that was because the `PhpArrayAdapter`’s `$valuesCache` would retain them from previous tests. This PR adds a `getArrayPool` method to the test cases which allows them to clear it on tearDown, so that tests don’t impact each other. Alternatively they could clear their own adapter or pass it different files, but this seemed the safest approach to me. Commits ------- fb9e64e [FrameworkBundle] Fix cache warmers tests
2 parents d2b1513 + fb9e64e commit e7551d5

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121

2222
class SerializerCacheWarmerTest extends TestCase
2323
{
24+
private PhpArrayAdapter $arrayPool;
25+
26+
protected function tearDown(): void
27+
{
28+
parent::tearDown();
29+
30+
if (isset($this->arrayPool)) {
31+
$this->arrayPool->clear();
32+
unset($this->arrayPool);
33+
}
34+
}
35+
36+
private function getArrayPool(string $file): PhpArrayAdapter
37+
{
38+
return $this->arrayPool = new PhpArrayAdapter($file, new NullAdapter());
39+
}
40+
2441
/**
2542
* @dataProvider loaderProvider
2643
*/
@@ -34,7 +51,7 @@ public function testWarmUp(array $loaders)
3451

3552
$this->assertFileExists($file);
3653

37-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
54+
$arrayPool = $this->getArrayPool($file);
3855

3956
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
4057
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
@@ -56,7 +73,7 @@ public function testWarmUpAbsoluteFilePath(array $loaders)
5673
$this->assertFileExists($file);
5774
$this->assertFileDoesNotExist($cacheDir.'/cache-serializer.php');
5875

59-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
76+
$arrayPool = $this->getArrayPool($file);
6077

6178
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
6279
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
@@ -75,10 +92,10 @@ public function testWarmUpWithoutBuildDir(array $loaders)
7592

7693
$this->assertFileDoesNotExist($file);
7794

78-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
95+
$arrayPool = $this->getArrayPool($file);
7996

80-
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
81-
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
97+
$this->assertFalse($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
98+
$this->assertFalse($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
8299
}
83100

84101
public static function loaderProvider(): array

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@
2020

2121
class ValidatorCacheWarmerTest extends TestCase
2222
{
23+
private PhpArrayAdapter $arrayPool;
24+
25+
protected function tearDown(): void
26+
{
27+
parent::tearDown();
28+
29+
if (isset($this->arrayPool)) {
30+
$this->arrayPool->clear();
31+
unset($this->arrayPool);
32+
}
33+
}
34+
35+
private function getArrayPool(string $file): PhpArrayAdapter
36+
{
37+
return $this->arrayPool = new PhpArrayAdapter($file, new NullAdapter());
38+
}
39+
2340
public function testWarmUp()
2441
{
2542
$validatorBuilder = new ValidatorBuilder();
@@ -36,7 +53,7 @@ public function testWarmUp()
3653

3754
$this->assertFileExists($file);
3855

39-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
56+
$arrayPool = $this->getArrayPool($file);
4057

4158
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
4259
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
@@ -61,7 +78,7 @@ public function testWarmUpAbsoluteFilePath()
6178
$this->assertFileExists($file);
6279
$this->assertFileDoesNotExist($cacheDir.'/cache-validator.php');
6380

64-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
81+
$arrayPool = $this->getArrayPool($file);
6582

6683
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
6784
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
@@ -83,10 +100,10 @@ public function testWarmUpWithoutBuilDir()
83100

84101
$this->assertFileDoesNotExist($file);
85102

86-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
103+
$arrayPool = $this->getArrayPool($file);
87104

88-
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
89-
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
105+
$this->assertFalse($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
106+
$this->assertFalse($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
90107
}
91108

92109
public function testWarmUpWithAnnotations()
@@ -103,7 +120,7 @@ public function testWarmUpWithAnnotations()
103120

104121
$this->assertFileExists($file);
105122

106-
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
123+
$arrayPool = $this->getArrayPool($file);
107124

108125
$item = $arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category');
109126
$this->assertTrue($item->isHit());

0 commit comments

Comments
 (0)