Skip to content

Commit 8906a2a

Browse files
committed
Merge branch '2.0'
2 parents f962337 + 2fee7e0 commit 8906a2a

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/Hal/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function supportsNormalization($data, $format = null)
4444
public function normalize($object, $format = null, array $context = [])
4545
{
4646
$context['cache_key'] = $this->getHalCacheKey($format, $context);
47+
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
48+
$context = $this->initContext($resourceClass, $context);
4749

4850
$rawData = parent::normalize($object, $format, $context);
4951
if (!is_array($rawData)) {

tests/Fixtures/TestBundle/Doctrine/Generator/Uuid.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Doctrine\Generator;
1515

16-
class Uuid
16+
class Uuid implements \JsonSerializable
1717
{
1818
private $id;
1919

@@ -26,4 +26,9 @@ public function __toString()
2626
{
2727
return $this->id;
2828
}
29+
30+
public function jsonSerialize()
31+
{
32+
return (string) $this->id;
33+
}
2934
}

tests/Fixtures/TestBundle/Entity/CustomGeneratedIdentifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ class CustomGeneratedIdentifier
3737

3838
public function getId()
3939
{
40-
return (string) $this->id;
40+
return $this->id;
4141
}
4242
}

tests/Hal/Serializer/ItemNormalizerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function testNormalize()
9696

9797
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
9898
$resourceClassResolverProphecy->getResourceClass($dummy, null, true)->willReturn(Dummy::class)->shouldBeCalled();
99+
$resourceClassResolverProphecy->getResourceClass($dummy, Dummy::class, true)->willReturn(Dummy::class)->shouldBeCalled();
99100

100101
$serializerProphecy = $this->prophesize(SerializerInterface::class);
101102
$serializerProphecy->willImplement(NormalizerInterface::class);
@@ -119,4 +120,47 @@ public function testNormalize()
119120
];
120121
$this->assertEquals($expected, $normalizer->normalize($dummy));
121122
}
123+
124+
public function testNormalizeWithoutCache()
125+
{
126+
$dummy = new Dummy();
127+
$dummy->setName('hello');
128+
129+
$propertyNameCollection = new PropertyNameCollection(['name']);
130+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
131+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
132+
133+
$propertyMetadataFactory = new PropertyMetadata(null, null, true);
134+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
135+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadataFactory)->shouldBeCalled();
136+
137+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
138+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1988')->shouldBeCalled();
139+
140+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
141+
$resourceClassResolverProphecy->getResourceClass($dummy, null, true)->willReturn(Dummy::class)->shouldBeCalled();
142+
$resourceClassResolverProphecy->getResourceClass($dummy, Dummy::class, true)->willReturn(Dummy::class)->shouldBeCalled();
143+
144+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
145+
$serializerProphecy->willImplement(NormalizerInterface::class);
146+
$serializerProphecy->normalize('hello', null, Argument::type('array'))->willReturn('hello')->shouldBeCalled();
147+
148+
$normalizer = new ItemNormalizer(
149+
$propertyNameCollectionFactoryProphecy->reveal(),
150+
$propertyMetadataFactoryProphecy->reveal(),
151+
$iriConverterProphecy->reveal(),
152+
$resourceClassResolverProphecy->reveal()
153+
);
154+
$normalizer->setSerializer($serializerProphecy->reveal());
155+
156+
$expected = [
157+
'_links' => [
158+
'self' => [
159+
'href' => '/dummies/1988',
160+
],
161+
],
162+
'name' => 'hello',
163+
];
164+
$this->assertEquals($expected, $normalizer->normalize($dummy, null, ['not_serializable' => function () {}]));
165+
}
122166
}

0 commit comments

Comments
 (0)