[Validator] Fix LazyLoadingMetadataFactory with PSR6Cache for non classname if tested values isn't an existing class

This commit is contained in:
Pascal Montoya 2018-04-06 07:52:15 +02:00 committed by Pascal Montoya
parent 16edba5d99
commit cd914209bd
No known key found for this signature in database
GPG Key ID: 6C8C858C247C9140
2 changed files with 19 additions and 4 deletions

View File

@ -90,6 +90,10 @@ class LazyLoadingMetadataFactory implements MetadataFactoryInterface
return $this->loadedClasses[$class];
}
if (!class_exists($class) && !interface_exists($class)) {
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
}
if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
// Include constraints from the parent class
$this->mergeConstraints($metadata);
@ -97,10 +101,6 @@ class LazyLoadingMetadataFactory implements MetadataFactoryInterface
return $this->loadedClasses[$class] = $metadata;
}
if (!class_exists($class) && !interface_exists($class)) {
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
}
$metadata = new ClassMetadata($class);
if (null !== $this->loader) {

View File

@ -149,6 +149,21 @@ class LazyLoadingMetadataFactoryTest extends TestCase
$this->assertEquals($metadata, $factory->getMetadataFor(self::PARENT_CLASS));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
*/
public function testNonClassNameStringValues()
{
$testedValue = 'error@example.com';
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory($loader, $cache);
$cache
->expects($this->never())
->method('read');
$factory->getMetadataFor($testedValue);
}
public function testMetadataCacheWithRuntimeConstraint()
{
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();