[Serializer] fixed fix encoding of cache keys with anonymous classes

This commit is contained in:
Michael Zangerle 2020-08-25 11:11:10 +02:00 committed by Nicolas Grekas
parent 77418e712e
commit 3a4675359d
2 changed files with 17 additions and 2 deletions

View File

@ -44,8 +44,7 @@ class CacheClassMetadataFactory implements ClassMetadataFactoryInterface
public function getMetadataFor($value)
{
$class = $this->getClass($value);
// Key cannot contain backslashes according to PSR-6
$key = strtr($class, '\\', '_');
$key = rawurlencode(strtr($class, '\\', '_'));
$item = $this->cacheItemPool->getItem($key);
if ($item->isHit()) {

View File

@ -63,4 +63,20 @@ class CacheMetadataFactoryTest extends TestCase
$factory->getMetadataFor('Not\Exist');
}
public function testAnonymousClass()
{
$anonymousObject = new class() {
};
$metadata = new ClassMetadata(\get_class($anonymousObject));
$decorated = $this->getMockBuilder(ClassMetadataFactoryInterface::class)->getMock();
$decorated
->expects($this->once())
->method('getMetadataFor')
->willReturn($metadata);
$factory = new CacheClassMetadataFactory($decorated, new ArrayAdapter());
$this->assertEquals($metadata, $factory->getMetadataFor($anonymousObject));
}
}