+ *
+ * @group legacy
*/
class Psr6CacheTest extends AbstractCacheTest
{
diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php
index a4f1385cee886..6e9b7302db181 100644
--- a/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php
+++ b/src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php
@@ -12,6 +12,8 @@
namespace Symfony\Component\Validator\Tests\Mapping\Factory;
use PHPUnit\Framework\TestCase;
+use Psr\Cache\CacheItemPoolInterface;
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
@@ -76,7 +78,36 @@ public function testMergeParentConstraints()
$this->assertEquals($constraints, $metadata->getConstraints());
}
- public function testWriteMetadataToCache()
+ public function testCachedMetadata()
+ {
+ $cache = new ArrayAdapter();
+ $factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
+
+ $expectedConstraints = [
+ new ConstraintA(['groups' => ['Default', 'EntityParent']]),
+ new ConstraintA(['groups' => ['Default', 'EntityInterfaceA', 'EntityParent']]),
+ ];
+
+ $metadata = $factory->getMetadataFor(self::PARENT_CLASS);
+
+ $this->assertEquals(self::PARENT_CLASS, $metadata->getClassName());
+ $this->assertEquals($expectedConstraints, $metadata->getConstraints());
+
+ $loader = $this->createMock(LoaderInterface::class);
+ $loader->expects($this->never())->method('loadClassMetadata');
+
+ $factory = new LazyLoadingMetadataFactory($loader, $cache);
+
+ $metadata = $factory->getMetadataFor(self::PARENT_CLASS);
+
+ $this->assertEquals(self::PARENT_CLASS, $metadata->getClassName());
+ $this->assertEquals($expectedConstraints, $metadata->getConstraints());
+ }
+
+ /**
+ * @group legacy
+ */
+ public function testWriteMetadataToLegacyCache()
{
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
@@ -115,7 +146,10 @@ public function testWriteMetadataToCache()
$this->assertEquals($parentClassConstraints, $metadata->getConstraints());
}
- public function testReadMetadataFromCache()
+ /**
+ * @group legacy
+ */
+ public function testReadMetadataFromLegacyCache()
{
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
@@ -154,29 +188,19 @@ public function testNonClassNameStringValues()
$this->expectException('Symfony\Component\Validator\Exception\NoSuchMetadataException');
$testedValue = 'error@example.com';
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
- $cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
+ $cache = $this->createMock(CacheItemPoolInterface::class);
$factory = new LazyLoadingMetadataFactory($loader, $cache);
$cache
->expects($this->never())
- ->method('read');
+ ->method('getItem');
$factory->getMetadataFor($testedValue);
}
public function testMetadataCacheWithRuntimeConstraint()
{
- $cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
+ $cache = new ArrayAdapter();
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
- $cache
- ->expects($this->any())
- ->method('write')
- ->willReturnCallback(function ($metadata) { serialize($metadata); })
- ;
-
- $cache->expects($this->any())
- ->method('read')
- ->willReturn(false);
-
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
$metadata->addConstraint(new Callback(function () {}));
diff --git a/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php b/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
index 0767742641e22..95c34470864af 100644
--- a/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
+++ b/src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
@@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Tests;
use PHPUnit\Framework\TestCase;
+use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
use Symfony\Component\Validator\ValidatorBuilder;
use Symfony\Component\Validator\ValidatorBuilderInterface;
@@ -85,6 +86,15 @@ public function testDisableAnnotationMapping()
$this->assertSame($this->builder, $this->builder->disableAnnotationMapping());
}
+ public function testSetMappingCache()
+ {
+ $this->assertSame($this->builder, $this->builder->setMappingCache($this->createMock(CacheItemPoolInterface::class)));
+ }
+
+ /**
+ * @group legacy
+ * @expectedDeprecation Symfony\Component\Validator\ValidatorBuilder::setMetadataCache is deprecated since Symfony 4.4. Use setMappingCache() instead.
+ */
public function testSetMetadataCache()
{
$this->assertSame($this->builder, $this->builder->setMetadataCache(
diff --git a/src/Symfony/Component/Validator/ValidatorBuilder.php b/src/Symfony/Component/Validator/ValidatorBuilder.php
index a65cca4c3d921..67faa54762091 100644
--- a/src/Symfony/Component/Validator/ValidatorBuilder.php
+++ b/src/Symfony/Component/Validator/ValidatorBuilder.php
@@ -15,6 +15,7 @@
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ArrayCache;
+use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Exception\LogicException;
@@ -63,9 +64,9 @@ class ValidatorBuilder implements ValidatorBuilderInterface
private $validatorFactory;
/**
- * @var CacheInterface|null
+ * @var CacheItemPoolInterface|null
*/
- private $metadataCache;
+ private $mappingCache;
/**
* @var TranslatorInterface|null
@@ -228,15 +229,37 @@ public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
}
/**
- * {@inheritdoc}
+ * Sets the cache for caching class metadata.
+ *
+ * @return $this
+ *
+ * @deprecated since Symfony 4.4.
*/
public function setMetadataCache(CacheInterface $cache)
{
+ @trigger_error(sprintf('%s is deprecated since Symfony 4.4. Use setMappingCache() instead.', __METHOD__), E_USER_DEPRECATED);
+
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot set a custom metadata cache after setting a custom metadata factory. Configure your metadata factory instead.');
}
- $this->metadataCache = $cache;
+ $this->mappingCache = $cache;
+
+ return $this;
+ }
+
+ /**
+ * Sets the cache for caching class metadata.
+ *
+ * @return $this
+ */
+ public function setMappingCache(CacheItemPoolInterface $cache)
+ {
+ $this->mappingCache = $cache;
+
+ if (null !== $this->metadataFactory) {
+ throw new ValidatorException('You cannot set a custom mapping cache after setting a custom metadata factory. Configure your metadata factory instead.');
+ }
return $this;
}
@@ -330,7 +353,7 @@ public function getValidator()
$loader = $loaders[0];
}
- $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
+ $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->mappingCache);
}
$validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory();
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy