Skip to content

Commit

Permalink
Merge 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jan 8, 2024
2 parents a98ef1a + 2aed3c1 commit af8726a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<server name="KERNEL_DIR" value="tests/Fixtures/app/" />
<server name="KERNEL_CLASS" value="AppKernel" />
<env name="APP_ENV" value="test" />
<env name="SYMFONY_PHPUNIT_REQUIRE" value="nikic/php-parser:^4.16"/>
</php>

<testsuites>
Expand Down
9 changes: 5 additions & 4 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,10 @@ protected function getFactoryOptions(array $context): array
$options['serializer_groups'] = (array) $context[self::GROUPS];
}

$operationCacheKey = ($context['resource_class'] ?? '').($context['operation_name'] ?? '').($context['api_normalize'] ?? '');
if ($operationCacheKey && isset($this->localFactoryOptionsCache[$operationCacheKey])) {
return $options + $this->localFactoryOptionsCache[$operationCacheKey];
$operationCacheKey = ($context['resource_class'] ?? '').($context['operation_name'] ?? '').($context['root_operation_name'] ?? '');
$suffix = ($context['api_normalize'] ?? '') ? 'n' : '';
if ($operationCacheKey && isset($this->localFactoryOptionsCache[$operationCacheKey.$suffix])) {
return $options + $this->localFactoryOptionsCache[$operationCacheKey.$suffix];
}

// This is a hot spot
Expand All @@ -622,7 +623,7 @@ protected function getFactoryOptions(array $context): array
}
}

return $options + $this->localFactoryOptionsCache[$operationCacheKey] = $options;
return $options + $this->localFactoryOptionsCache[$operationCacheKey.$suffix] = $options;
}

/**
Expand Down
78 changes: 78 additions & 0 deletions src/Serializer/Tests/AbstractItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,84 @@ public function testDenormalizeObjectWithNullDisabledTypeEnforcement(): void
$this->assertInstanceOf(DtoWithNullValue::class, $actual);
$this->assertEquals(new DtoWithNullValue(), $actual);
}

public function testCacheKey(): void
{
$relatedDummy = new RelatedDummy();

$dummy = new Dummy();
$dummy->setName('foo');
$dummy->setAlias('ignored');
$dummy->setRelatedDummy($relatedDummy);
$dummy->relatedDummies->add(new RelatedDummy());

$relatedDummies = new ArrayCollection([$relatedDummy]);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['name', 'alias', 'relatedDummy', 'relatedDummies']));

$relatedDummyType = new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class);
$relatedDummiesType = new Type(Type::BUILTIN_TYPE_OBJECT, false, ArrayCollection::class, true, new Type(Type::BUILTIN_TYPE_INT), $relatedDummyType);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'alias', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_STRING)])->withDescription('')->withReadable(true));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummyType])->withDescription('')->withReadable(true)->withWritable(false)->withReadableLink(false));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$relatedDummiesType])->withReadable(true)->withWritable(false)->withReadableLink(false));

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResource($dummy, Argument::cetera())->willReturn('/dummies/1');
$iriConverterProphecy->getIriFromResource($relatedDummy, Argument::cetera())->willReturn('/dummies/2');

$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
$propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo');
$propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy);
$propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn($relatedDummies);

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
$resourceClassResolverProphecy->getResourceClass($dummy, null)->willReturn(Dummy::class);
$resourceClassResolverProphecy->getResourceClass($relatedDummy, RelatedDummy::class)->willReturn(RelatedDummy::class);
$resourceClassResolverProphecy->getResourceClass($relatedDummies, RelatedDummy::class)->willReturn(RelatedDummy::class);
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
$resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize('foo', null, Argument::type('array'))->willReturn('foo');
$serializerProphecy->normalize(['/dummies/2'], null, Argument::type('array'))->willReturn(['/dummies/2']);

$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$propertyAccessorProphecy->reveal(),
null,
null,
[],
null,
null,
]);
$normalizer->setSerializer($serializerProphecy->reveal());

$expected = [
'name' => 'foo',
'relatedDummy' => '/dummies/2',
'relatedDummies' => ['/dummies/2'],
];
$this->assertSame($expected, $normalizer->normalize($dummy, null, [
'resources' => [],
'groups' => ['group'],
'ignored_attributes' => ['alias'],
'operation_name' => 'operation_name',
'root_operation_name' => 'root_operation_name',
]));

$operationCacheKey = (new \ReflectionClass($normalizer))->getProperty('localFactoryOptionsCache')->getValue($normalizer);
$this->assertEquals(array_keys($operationCacheKey), [sprintf('%s%s%s%s', Dummy::class, 'operation_name', 'root_operation_name', 'n')]);
$this->assertEquals(current($operationCacheKey), ['serializer_groups' => ['group']]);
}
}

class ObjectWithBasicProperties
Expand Down

0 comments on commit af8726a

Please sign in to comment.