|
23 | 23 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
24 | 24 | use Symfony\Component\Serializer\SerializerAwareInterface;
|
25 | 25 | use Symfony\Component\Serializer\SerializerInterface;
|
| 26 | +use Symfony\Component\Serializer\Tests\Fixtures\DeepObjectPopulateChildDummy; |
| 27 | +use Symfony\Component\Serializer\Tests\Fixtures\DeepObjectPopulateParentDummy; |
26 | 28 |
|
27 | 29 | class AbstractObjectNormalizerTest extends TestCase
|
28 | 30 | {
|
@@ -171,6 +173,48 @@ public function testSkipNullValues()
|
171 | 173 | $result = $normalizer->normalize($dummy, null, [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
|
172 | 174 | $this->assertSame(['bar' => 'present'], $result);
|
173 | 175 | }
|
| 176 | + |
| 177 | + public function testDeepObjectToPopulate() |
| 178 | + { |
| 179 | + $child = new DeepObjectPopulateChildDummy(); |
| 180 | + $child->bar = 'bar-old'; |
| 181 | + $child->foo = 'foo-old'; |
| 182 | + |
| 183 | + $parent = new DeepObjectPopulateParentDummy(); |
| 184 | + $parent->setChild($child); |
| 185 | + |
| 186 | + $context = [ |
| 187 | + AbstractObjectNormalizer::OBJECT_TO_POPULATE => $parent, |
| 188 | + AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE => true, |
| 189 | + ]; |
| 190 | + |
| 191 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 192 | + $normalizer = new ObjectNormalizer($classMetadataFactory, null, null, new PhpDocExtractor()); |
| 193 | + |
| 194 | + $newChild = new DeepObjectPopulateChildDummy(); |
| 195 | + $newChild->bar = 'bar-new'; |
| 196 | + $newChild->foo = 'foo-old'; |
| 197 | + |
| 198 | + $serializer = $this->getMockBuilder(__NAMESPACE__.'\ObjectSerializerDenormalizer')->getMock(); |
| 199 | + $serializer |
| 200 | + ->method('supportsDenormalization') |
| 201 | + ->with($this->arrayHasKey('bar'), |
| 202 | + $this->equalTo(DeepObjectPopulateChildDummy::class), |
| 203 | + $this->isNull(), |
| 204 | + $this->contains($child)) |
| 205 | + ->willReturn(true); |
| 206 | + $serializer->method('denormalize')->willReturn($newChild); |
| 207 | + |
| 208 | + $normalizer->setSerializer($serializer); |
| 209 | + $normalizer->denormalize([ |
| 210 | + 'child' => [ |
| 211 | + 'bar' => 'bar-new', |
| 212 | + ], |
| 213 | + ], 'Symfony\Component\Serializer\Tests\Fixtures\DeepObjectPopulateParentDummy', null, $context); |
| 214 | + |
| 215 | + $this->assertSame('bar-new', $parent->getChild()->bar); |
| 216 | + $this->assertSame('foo-old', $parent->getChild()->foo); |
| 217 | + } |
174 | 218 | }
|
175 | 219 |
|
176 | 220 | class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
|
@@ -348,3 +392,7 @@ public function setSerializer(SerializerInterface $serializer)
|
348 | 392 | $this->serializer = $serializer;
|
349 | 393 | }
|
350 | 394 | }
|
| 395 | + |
| 396 | +abstract class ObjectSerializerDenormalizer implements SerializerInterface, DenormalizerInterface |
| 397 | +{ |
| 398 | +} |
0 commit comments