8000 [Validator] Fix LazyLoadingMetadataFactory with PSR6Cache for non cla… · symfony/symfony@e5e5a41 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5e5a41

Browse files
author
Pascal Montoya
committed
[Validator] Fix LazyLoadingMetadataFactory with PSR6Cache for non classname if tested values isn't existing class
1 parent 96e7ded commit e5e5a41

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ public function getMetadataFor($value)
9999
return $this->loadedClasses[$class];
100100
}
101101

102+
if (!class_exists($class) && !interface_exists($class)) {
103+
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
104+
}
105+
102106
if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
103107
// Include constraints from the parent class
104108
$this->mergeConstraints($metadata);
105109

106110
return $this->loadedClasses[$class] = $metadata;
107111
}
108112

109-
if (!class_exists($class) && !interface_exists($class)) {
110-
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
111-
}
112-
113113
$metadata = new ClassMetadata($class);
114114

115115
if (null !== $this->loader) {

src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Mapping\Factory;
1313

14+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1415
use Symfony\Component\Validator\Constraints\Callback;
16+
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
1517
use Symfony\Component\Validator\Mapping\ClassMetadata;
1618
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
1719
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
@@ -168,6 +170,24 @@ public function testMetadataCacheWithRuntimeConstraint()
168170

169171
$metadata = $factory->getMetadataFor(self::CLASS_NAME);
170172
}
173+
174+
public function testNonClassNameStringValues()
175+
{
176+
$testedValue = 'error@example.com';
177+
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
178+
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
179+
$factory = new LazyLoadingMetadataFactory($loader, $cache);
180+
$cache
181+
->method('read')
182+
->with($testedValue)
183+
->willThrowException(new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters {}()/\@:', $testedValue)));
184+
$this->expectException(NoSuchMetadataException::class);
185+
try {
186+
$factory->getMetadataFor($testedValue);
187+
} catch (InvalidArgumentException $exception) {
188+
$this->fail(sprintf('Unexpected %s thrown', InvalidArgumentException::class));
189+
}
190+
}
171191
}
172192

173193
class TestLoader implements LoaderInterface

0 commit comments

Comments
 (0)
0