8000 [FrameworkBundle] Make `ValidatorCacheWarmer` and `SerializeCacheWarm… · symfony/symfony@dc59817 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc59817

Browse files
Okhoshifabpot
authored andcommitted
[FrameworkBundle] Make ValidatorCacheWarmer and SerializeCacheWarmer use kernel.build_dir instead of kernel.cache_dir
1 parent 29da4f5 commit dc59817

File tree

7 files changed

+146
-22
lines changed

7 files changed

+146
-22
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ CHANGELOG
2525
* Set `framework.rate_limiter.limiters.*.lock_factory` to `auto` by default
2626
* Deprecate `RateLimiterFactory` autowiring aliases, use `RateLimiterFactoryInterface` instead
2727
* Allow configuring compound rate limiters
28+
* Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
29+
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
2830

2931
7.2
3032
---

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

+3
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/CacheWarmer/ValidatorCacheWarmer.php

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function __construct(
4141

4242
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool
4343
{
44+
if (!$buildDir) {
45+
return false;
46+
}
47+
4448
$loaders = $this->validatorBuilder->getLoaders();
4549
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter);
4650

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

+1-1
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()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
return static function (ContainerConfigurator $container) {
3030
$container->parameters()
31-
->set('validator.mapping.cache.file', param('kernel.cache_dir').'/validation.php');
31+
->set('validator.mapping.cache.file', '%kernel.build_dir%/validation.php');
3232

3333
$validatorsDir = \dirname((new \ReflectionClass(EmailValidator::class))->getFileName());
3434

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

+64-10
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
}

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

+71-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testWarmUp()
3232
@unlink($file);
3333

3434
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
35-
$warmer->warmUp(\dirname($file));
35+
$warmer->warmUp(\dirname($file), \dirname($file));
3636

3737
$this->assertFileExists($file);
3838

@@ -42,6 +42,53 @@ public function testWarmUp()
4242
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
4343
}
4444

45+
public function testWarmUpAbsoluteFilePath()
46+
{
47+
$validatorBuilder = new ValidatorBuilder();
48+
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
49+
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
50+
$validatorBuilder->addMethodMapping('loadValidatorMetadata');
51+
$validatorBuilder->enableAttributeMapping();
52+
53+
$file = sys_get_temp_dir().'/0/cache-validator.php';
54+
@unlink($file);
55+
56+
$cacheDir = sys_get_temp_dir().'/1';
57+
58+
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
59+
$warmer->warmUp($cacheDir, $cacheDir);
60+
61+
$this->assertFileExists($file);
62+
$this->assertFileDoesNotExist($cacheDir.'/cache-validator.php');
63+
64+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
65+
66+
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
67+
$this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
68+
}
69+
70+
public function testWarmUpWithoutBuilDir()
71+
{
72+
$validatorBuilder = new ValidatorBuilder();
73+
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
74+
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
75+
$validatorBuilder->addMethodMapping('loadValidatorMetadata');
76+
$validatorBuilder->enableAttributeMapping();
77+
78+
$file = sys_get_temp_dir().'/cache-validator.php';
79+
@unlink($file);
80+
81+
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
82+
$warmer->warmUp(\dirname($file));
83+
84+
$this->assertFileDoesNotExist($file);
85+
86+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
87+
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());
90+
}
91+
4592
public function testWarmUpWithAnnotations()
4693
{
4794
$validatorBuilder = new ValidatorBuilder();
@@ -52,7 +99,7 @@ public function testWarmUpWithAnnotations()
5299
@unlink($file);
53100

54101
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
55-
$warmer->warmUp(\dirname($file));
102+
$warmer->warmUp(\dirname($file), \dirname($file));
56103

57104
$this->assertFileExists($file);
58105

@@ -72,7 +119,7 @@ public function testWarmUpWithoutLoader()
72119
@unlink($file);
73120

74121
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
75-
$warmer->warmUp(\dirname($file));
122+
$warmer->warmUp(\dirname($file), \dirname($file));
76123

77124
$this->assertFileExists($file);
78125
}
@@ -85,17 +132,22 @@ public function testClassAutoloadException()
85132
{
86133
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest', false));
87134

135+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
136+
@unlink($file);
137+
88138
$validatorBuilder = new ValidatorBuilder();
89139
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml');
90-
$warmer = new ValidatorCacheWarmer($validatorBuilder, tempnam(sys_get_temp_dir(), __FUNCTION__));
140+
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file);
91141

92142
spl_autoload_register($classloader = function ($class) use ($mappedClass) {
93143
if ($class === $mappedClass) {
94144
throw new \DomainException('This exception should be caught by the warmer.');
95145
}
96146
}, true, true);
97147

98-
$warmer->warmUp('foo');
148+
$warmer->warmUp(\dirname($file), \dirname($file));
149+
150+
$this->assertFileExists($file);
99151

100152
spl_autoload_unregister($classloader);
101153
}
@@ -106,14 +158,14 @@ public function testClassAutoloadException()
106158
*/
107159
public function testClassAutoloadExceptionWithUnrelatedException()
108160
{
109-
$this->expectException(\DomainException::class);
110-
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
161+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
162+
@unlink($file);
111163

112164
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest', false));
113165

114166
$validatorBuilder = new ValidatorBuilder();
115167
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml');
116-
$warmer = new ValidatorCacheWarmer($validatorBuilder, tempnam(sys_get_temp_dir(), __FUNCTION__));
168+
$warmer = new ValidatorCacheWarmer($validatorBuilder, basename($file));
117169

118170
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
119171
if ($class === $mappedClass) {
@@ -122,8 +174,17 @@ public function testClassAutoloadExceptionWithUnrelatedException()
122174
}
123175
}, true, true);
124176

125-
$warmer->warmUp('foo');
177+
$this->expectException(\DomainException::class);
178+
$this->expectExceptionMessage('This exception should not be caught by the warmer.');
179+
180+
try {
181+
$warmer->warmUp(\dirname($file), \dirname($file));
182+
} catch (\DomainException $e) {
183+
$this->assertFileDoesNotExist($file);
126184

127-
spl_autoload_unregister($classLoader);
185+
throw $e;
186+
} finally {
187+
spl_autoload_unregister($classLoader);
188+
}
128189
}
129190
}

0 commit comments

Comments
 (0)
0