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

Skip to content

Commit dbcec69

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

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
@@ -90,17 +90,17 @@ public function getMetadataFor($value)
9090
return $this->loadedClasses[$class];
9191
}
9292

93+
if (!class_exists($class) && !interface_exists($class)) {
94+
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
95+
}
96+
9397
if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
9498
// Include constraints from the parent class
9599
$this->mergeConstraints($metadata);
96100

97101
return $this->loadedClasses[$class] = $metadata;
98102
}
99103

100-
if (!class_exists($class) && !interface_exists($class)) {
101-
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
102-
}
103-
104104
$metadata = new ClassMetadata($class);
105105

106106
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
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\Validator\Tests\Mapping\Factory;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1516
use Symfony\Component\Validator\Constraints\Callback;
17+
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
1618
use Symfony\Component\Validator\Mapping\ClassMetadata;
1719
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
1820
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
@@ -192,6 +194,24 @@ public function testGroupsFromParent()
192194
$this->assertContains('EntityStaticCar', $groups);
193195
$this->assertContains('EntityStaticVehicle', $groups);
194196
}
197+
198+
public function testNonClassNameStringValues()
199+
{
200+
$testedValue = 'error@example.com';
201+
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
202+
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
203+
$factory = new LazyLoadingMetadataFactory($loader, $cache);
204+
$cache
205+
->method('read')
206+
->with($testedValue)
207+
->willThrowException(new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters {}()/\@:', $testedValue)));
208+
$this->expectException(NoSuchMetadataException::class);
209+
try {
210+
$factory->getMetadataFor($testedValue);
211+
} catch (InvalidArgumentException $exception) {
212+
$this->fail(sprintf('Unexpected %s thrown', InvalidArgumentException::class));
213+
}
214+
}
195215
}
196216

197217
class TestLoader implements LoaderInterface

0 commit comments

Comments
 (0)
0