From 0c01de6e7f63c273c97c9afe6dc10883879e8704 Mon Sep 17 00:00:00 2001 From: Mikael Pajunen Date: Fri, 30 Dec 2016 00:33:11 +0200 Subject: [PATCH 1/2] [FrameworkBundle] Add annotated validator cache test case --- .../CacheWarmer/ValidatorCacheWarmerTest.php | 34 +++++++++++++++++++ .../Tests/Fixtures/Validation/Category.php | 15 ++++++++ .../Validation/Resources/categories.yml | 9 +++++ .../Tests/Fixtures/Validation/SubCategory.php | 13 +++++++ 4 files changed, 71 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Category.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Resources/categories.yml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/SubCategory.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php index 0e3fe47ce5ff8..c5d7e42abb358 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php @@ -51,6 +51,40 @@ public function testWarmUp() $this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author', $values); } + public function testWarmUpWithAnnotations() + { + $validatorBuilder = new ValidatorBuilder(); + $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml'); + $validatorBuilder->enableAnnotationMapping(); + + $file = sys_get_temp_dir().'/cache-validator-with-annotations.php'; + @unlink($file); + + $fallbackPool = new ArrayAdapter(); + + $warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool); + $warmer->warmUp(dirname($file)); + + $this->assertFileExists($file); + + $values = require $file; + + $this->assertInternalType('array', $values); + $this->assertCount(2, $values); + $this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values); + $this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.SubCategory', $values); + + // Simple check to make sure that at least one constraint is actually cached, in this case the "id" property Type. + $this->assertContains('"int"', $values['Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category']); + + $values = $fallbackPool->getValues(); + + $this->assertInternalType('array', $values); + $this->assertCount(2, $values); + $this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values); + $this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.SubCategory', $values); + } + public function testWarmUpWithoutLoader() { $validatorBuilder = new ValidatorBuilder(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Category.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Category.php new file mode 100644 index 0000000000000..c7972c50ae202 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Category.php @@ -0,0 +1,15 @@ + Date: Fri, 30 Dec 2016 01:22:50 +0200 Subject: [PATCH 2/2] [Validator] Fix invalid metadata factory state --- .../Mapping/Factory/LazyLoadingMetadataFactory.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php b/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php index f5894f49b093a..1b677715cd469 100644 --- a/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php +++ b/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php @@ -99,8 +99,12 @@ public function getMetadataFor($value) return $this->loadedClasses[$class]; } - if (null !== $this->cache && false !== ($this->loadedClasses[$class] = $this->cache->read($class))) { - return $this->loadedClasses[$class]; + if (null !== $this->cache) { + $cached = $this->cache->read($class); + + if (false !== $cached) { + return $this->loadedClasses[$class] = $cached; + } } if (!class_exists($class) && !interface_exists($class)) {