Add tests
This commit is contained in:
parent
7bd4ac5937
commit
e99a90b9e9
@ -281,12 +281,13 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
* @param array $context
|
||||
* @param \ReflectionClass $reflectionClass
|
||||
* @param array|bool $allowedAttributes
|
||||
* @param string|null $format
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
||||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null)
|
||||
{
|
||||
if (
|
||||
isset($context[static::OBJECT_TO_POPULATE]) &&
|
||||
@ -320,12 +321,13 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
||||
}
|
||||
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
||||
$parameterData = $data[$key];
|
||||
if (null !== $constructogrParameter->getClass()) {
|
||||
if (null !== $constructorParameter->getClass()) {
|
||||
$parameterData = $this->serializer->denormalize($parameterData, $constructorParameter->getClass()->getName(), null, $context);
|
||||
}
|
||||
|
||||
// Don't run set for a parameter passed to the constructor
|
||||
$params[] = $parameterData;
|
||||
unset($data[$key]);
|
||||
} elseif ($constructorParameter->isDefaultValueAvailable()) {
|
||||
$params[] = $constructorParameter->getDefaultValue();
|
||||
} else {
|
||||
|
@ -175,7 +175,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
|
||||
$normalizedData = $this->prepareForDenormalization($data);
|
||||
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
|
||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
|
||||
|
||||
foreach ($normalizedData as $attribute => $value) {
|
||||
if ($this->nameConverter) {
|
||||
|
@ -157,6 +157,24 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
}
|
||||
|
||||
public function testConstructorWithObjectTypeHintDenormalize()
|
||||
{
|
||||
$data = [
|
||||
'id' => 10,
|
||||
'inner' => [
|
||||
'foo' => 'oof',
|
||||
'bar' => 'rab',
|
||||
],
|
||||
];
|
||||
|
||||
$obj = $this->normalizer->denormalize($data, DummyWithConstructorObject::class);
|
||||
$this->assertInstanceOf(DummyWithConstructorObject::class, $obj);
|
||||
$this->assertEquals(10, $obj->getId);
|
||||
$this->assertInstanceOf(ObjectInner::class, $obj->getInner());
|
||||
$this->assertEquals('foo', $obj->getInner()->foo);
|
||||
$this->assertEquals('bar', $obj->getInner()->bar);
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
@ -782,3 +800,25 @@ class FormatAndContextAwareNormalizer extends ObjectNormalizer
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class DummyWithConstructorObject
|
||||
{
|
||||
private $id;
|
||||
private $inner;
|
||||
|
||||
public function __construct($id, ObjectInner $inner)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->inner = $inner;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getInner()
|
||||
{
|
||||
return $this->inner;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user