Skip to content

Commit e0f12d8

Browse files
committed
[FrameworkBundle] Make SerializeCacheWarmer use kernel.build_dir instead of cache_dir
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
1 parent 0d85535 commit e0f12d8

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CHANGELOG
2323
the `#[AsController]` attribute is no longer required
2424
* Deprecate setting the `framework.profiler.collect_serializer_data` config option to `false`
2525
* Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
26+
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
2627

2728
7.2
2829
---

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function __construct(
4141

4242
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool
4343
{
44+
if (!$buildDir) {
45+
return false;
46+
}
4447
if (!$this->loaders) {
4548
return true;
4649
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
return static function (ContainerConfigurator $container) {
5858
$container->parameters()
59-
->set('serializer.mapping.cache.file', '%kernel.cache_dir%/serialization.php')
59+
->set('serializer.mapping.cache.file', '%kernel.build_dir%/serialization.php')
6060
;
6161

6262
$container->services()
@@ -164,7 +164,10 @@
164164

165165
->set('serializer.mapping.cache.symfony', CacheItemPoolInterface::class)
166166
->factory([PhpArrayAdapter::class, 'create'])
167-
->args([param('serializer.mapping.cache.file'), service('cache.serializer')])
167+
->args([
168+
param('serializer.mapping.cache.file'),
169+
service('cache.serializer'),
170+
])
168171

169172
->set('serializer.mapping.cache_class_metadata_factory', CacheClassMetadataFactory::class)
170173
->decorate('serializer.mapping.class_metadata_factory')

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

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,50 @@ public function testWarmUp(array $loaders)
3030
@unlink($file);
3131

3232
$warmer = new SerializerCacheWarmer($loaders, $file);
33-
$warmer->warmUp(\dirname($file));
33+
$warmer->warmUp(\dirname($file), \dirname($file));
34+
35+
$this->assertFileExists($file);
36+
37+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
38+
39+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
40+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
41+
}
42+
43+
/**
44+
* @dataProvider loaderProvider
45+
*/
46+
public function testWarmUpAbsoluteFilePath(array $loaders)
47+
{
48+
$file = sys_get_temp_dir().'/0/cache-serializer.php';
49+
@unlink($file);
50+
51+
$cacheDir = sys_get_temp_dir().'/1';
52+
53+
$warmer = new SerializerCacheWarmer($loaders, $file);
54+
$warmer->warmUp($cacheDir, $cacheDir);
3455

3556
$this->assertFileExists($file);
57+
$this->assertFileDoesNotExist($cacheDir.'/cache-serializer.php');
58+
59+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
60+
61+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
62+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
63+
}
64+
65+
/**
66+
* @dataProvider loaderProvider
67+
*/
68+
public function testWarmUpWithoutBuildDir(array $loaders)
69+
{
70+
$file = sys_get_temp_dir().'/cache-serializer.php';
71+
@unlink($file);
72+
73+
$warmer = new SerializerCacheWarmer($loaders, $file);
74+
$warmer->warmUp(\dirname($file));
75+
76+
$this->assertFileDoesNotExist($file);
3677

3778
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
3879

@@ -66,7 +107,7 @@ public function testWarmUpWithoutLoader()
66107
@unlink($file);
67108

68109
$warmer = new SerializerCacheWarmer([], $file);
69-
$warmer->warmUp(\dirname($file));
110+
$warmer->warmUp(\dirname($file), \dirname($file));
70111

71112
$this->assertFileExists($file);
72113
}
@@ -79,15 +120,19 @@ public function testClassAutoloadException()
79120
{
80121
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
81122

82-
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__));
123+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
124+
@unlink($file);
125+
126+
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], $file);
83127

84128
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
85129
if ($class === $mappedClass) {
86130
throw new \DomainException('This exception should be caught by the warmer.');
87131
}
88132
}, true, true);
89133

90-
$warmer->warmUp('foo');
134+
$warmer->warmUp(\dirname($file), \dirname($file));
135+
$this->assertFileExists($file);
91136

92137
spl_autoload_unregister($classLoader);
93138
}
@@ -98,12 +143,12 @@ public function testClassAutoloadException()
98143
*/
99144
public function testClassAutoloadExceptionWithUnrelatedException()
100145
{
101-
$this->expectException(\DomainException::class);
102-
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
103-
104146
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
105147

106-
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__));
148+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
149+
@unlink($file);
150+
151+
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], basename($file));
107152

108153
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
109154
if ($class === $mappedClass) {
@@ -112,8 +157,17 @@ public function testClassAutoloadExceptionWithUnrelatedException()
112157
}
113158
}, true, true);
114159

115-
$warmer->warmUp('foo');
160+
$this->expectException(\DomainException::class);
161+
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
162+
163+
try {
164+
$warmer->warmUp(\dirname($file), \dirname($file));
165+
} catch (\DomainException $e) {
166+
$this->assertFileDoesNotExist($file);
116167

117-
spl_autoload_unregister($classLoader);
168+
throw $e;
169+
} finally {
170+
spl_autoload_unregister($classLoader);
171+
}
118172
}
119173
}

0 commit comments

Comments
 (0)