Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,11 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$context = parent::createChildContext($parentContext, $attribute, $format);
$context['cache_key'] = $this->getCacheKey($format, $context);
if ($context['cache_key'] ?? false) {
$context['cache_key'] .= '-'.$attribute;
} else {
$context['cache_key'] = $this->getCacheKey($format, $context);
}

return $context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,131 @@ public function testDenormalizeUntypedStringObject()
$this->assertEquals(new DummyWithStringObject(new DummyString()), $actual);
$this->assertEquals('', $actual->value->value);
}

public function testProvidingContextCacheKeyGeneratesSameChildContextCacheKey()
{
$foobar = new Dummy();
$foobar->foo = new EmptyDummy();
$foobar->bar = 'bar';
$foobar->baz = 'baz';
$data = [
'foo' => [],
'bar' => 'bar',
'baz' => 'baz',
];

$normalizer = new class() extends AbstractObjectNormalizerDummy {
public $childContextCacheKey;

protected function extractAttributes(object $object, string $format = null, array $context = []): array
{
return array_keys((array) $object);
}

protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
{
return $object->{$attribute};
}

protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$childContext = parent::createChildContext($parentContext, $attribute, $format);
$this->childContextCacheKey = $childContext['cache_key'];

return $childContext;
}
};

$serializer = new Serializer([$normalizer]);

$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
$firstChildContextCacheKey = $normalizer->childContextCacheKey;

$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/2']);
$secondChildContextCacheKey = $normalizer->childContextCacheKey;

$this->assertSame($firstChildContextCacheKey, $secondChildContextCacheKey);
}

public function testChildContextKeepsOriginalContextCacheKey()
{
$foobar = new Dummy();
$foobar->foo = new EmptyDummy();
$foobar->bar = 'bar';
$foobar->baz = 'baz';
$data = [
'foo' => [],
'bar' => 'bar',
'baz' => 'baz',
];

$normalizer = new class() extends AbstractObjectNormalizerDummy {
public $childContextCacheKey;

protected function extractAttributes(object $object, string $format = null, array $context = []): array
{
return array_keys((array) $object);
}

protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
{
return $object->{$attribute};
}

protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$childContext = parent::createChildContext($parentContext, $attribute, $format);
$this->childContextCacheKey = $childContext['cache_key'];

return $childContext;
}
};

$serializer = new Serializer([$normalizer]);
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);

$this->assertSame('hardcoded-foo', $normalizer->childContextCacheKey);
}

public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse()
{
$foobar = new Dummy();
$foobar->foo = new EmptyDummy();
$foobar->bar = 'bar';
$foobar->baz = 'baz';
$data = [
'foo' => [],
'bar' => 'bar',
'baz' => 'baz',
];

$normalizer = new class() extends AbstractObjectNormalizerDummy {
public $childContextCacheKey;

protected function extractAttributes(object $object, string $format = null, array $context = []): array
{
return array_keys((array) $object);
}

protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
{
return $object->{$attribute};
}

protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$childContext = parent::createChildContext($parentContext, $attribute, $format);
$this->childContextCacheKey = $childContext['cache_key'];

return $childContext;
}
};

$serializer = new Serializer([$normalizer]);
$serializer->normalize($foobar, null, ['cache_key' => false]);

$this->assertFalse($normalizer->childContextCacheKey);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down