10000 Add tests · symfony/symfony@e99a90b · GitHub
[go: up one dir, main page]

Skip to content

Commit e99a90b

Browse files
committed
Add tests
1 parent 7bd4ac5 commit e99a90b

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
281281
* @param array $context
282282
* @param \ReflectionClass $reflectionClass
283283
* @param array|bool $allowedAttributes
284+
* @param string|null $format
284285
*
285286
* @return object
286287
*
287288
* @throws RuntimeException
288289
*/
289-
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
290+
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
290291
{
291292
if (
292293
isset($context[static::OBJECT_TO_POPULATE]) &&
@@ -320,12 +321,13 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
320321
}
321322
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
322323
$parameterData = $data[$key];
323-
if (null !== $constructogrParameter->getClass()) {
324+
if (null !== $constructorParameter->getClass()) {
324325
$parameterData = $this->serializer->denormalize($parameterData, $constructorParameter->getClass()->getName(), null, $context);
325326
}
326327

327328
// Don't run set for a parameter passed to the constructor
328329
$params[] = $parameterData;
330+
unset($data[$key]);
329331
} elseif ($constructorParameter->isDefaultValueAvailable()) {
330332
$params[] = $constructorParameter->getDefaultValue();
331333
} else {

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
175175
$normalizedData = $this->prepareForDenormalization($data);
176176

177177
$reflectionClass = new \ReflectionClass($class);
178-
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
178+
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
179179

180180
foreach ($normalizedData as $attribute => $value) {
181181
if ($this->nameConverter) {

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ public function testConstructorWithObjectDenormalize()
157157
$this->assertEquals('bar', $obj->bar);
158158
}
159159

160+
public function testConstructorWithObjectTypeHintDenormalize()
161+
{
162+
$data = [
163+
'id' => 10,
164+
'inner' => [
165+
'foo' => 'oof',
166+
'bar' => 'rab',
167+
],
168+
];
169+
170+
$obj = $this->normalizer->denormalize($data, DummyWithConstructorObject::class);
171+
$this->assertInstanceOf(DummyWithConstructorObject::class, $obj);
172+
$this->assertEquals(10, $obj->getId);
173+
$this->assertInstanceOf(ObjectInner::class, $obj->getInner());
174+
$this->assertEquals('foo', $obj->getInner()->foo);
175+
$this->assertEquals('bar', $obj->getInner()->bar);
176+
}
177+
160178
public function testGroupsNormalize()
161179
{
162180
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
@@ -782,3 +800,25 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
782800
return false;
783801
}
784802
}
803+
804+
class DummyWithConstructorObject
805+
{
806+
private $id;
807+
private $inner;
808+
809+
public function __construct($id, ObjectInner $inner)
810+
{
811+
$this->id = $id;
812+
$this->inner = $inner;
813+
}
814+
815+
public function getId()
816+
{
817+
return $this->id;
818+
}
819+
820+
public function getInner()
821+
{
822+
return $this->inner;
823+
}
824+
}

0 commit comments

Comments
 (0)
0