Skip to content

Commit f5afd6f

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 f5afd6f

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

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

+4-4
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

+45-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

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

14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1416
use Symfony\Component\Validator\Constraints\Callback;
17+
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
1518
use Symfony\Component\Validator\Mapping\ClassMetadata;
1619
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
1720
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
1821
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
1922

20-
class LazyLoadingMetadataFactoryTest extends \PHPUnit_Framework_TestCase
23+
class LazyLoadingMetadataFactoryTest extends TestCase
2124
{
2225
const CLASS_NAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
2326
const PARENT_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
@@ -166,7 +169,48 @@ public function testMetadataCacheWithRuntimeConstraint()
166169
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
167170
$metadata->addConstraint(new Callback(function () {}));
168171

172+
$this->assertCount(3, $metadata->getConstraints());
173+
169174
$metadata = $factory->getMetadataFor(self::CLASS_NAME);
175+
176+
$this->assertCount(6, $metadata->getConstraints());
177+
}
178+
179+
public function testGroupsFromParent()
180+
{
181+
$reader = new \Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader();
182+
$factory = new LazyLoadingMetadataFactory($reader);
183+
$metadata = $factory->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\EntityStaticCarTurbo');
184+
$groups = array();
185+
186+
foreach ($metadata->getPropertyMetadata('wheels') as $propertyMetadata) {
187+
$constraints = $propertyMetadata->getConstraints();
188+
$groups = array_replace($groups, $constraints[0]->groups);
189+
}
190+
191+
$this->assertCount(4, $groups);
192+
$this->assertContains('Default', $groups);
193+
$this->assertContains('EntityStaticCarTurbo', $groups);
194+
$this->assertContains('EntityStaticCar', $groups);
195+
$this->assertContains('EntityStaticVehicle', $groups);
196+
}
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+
}
170214
}
171215
}
172216

0 commit comments

Comments
 (0)