use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
+use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
+use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
+use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
+use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
$serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
);
}
+
+ public function testNormalizerAware()
+ {
+ $normalizerAware = $this->getMockBuilder(NormalizerAwareInterface::class)->getMock();
+ $normalizerAware->expects($this->once())
+ ->method('setNormalizer')
+ ->with($this->isInstanceOf(NormalizerInterface::class));
+
+ new Serializer(array($normalizerAware));
+ }
+
+ public function testDenormalizerAware()
+ {
+ $denormalizerAware = $this->getMockBuilder(DenormalizerAwareInterface::class)->getMock();
+ $denormalizerAware->expects($this->once())
+ ->method('setDenormalizer')
+ ->with($this->isInstanceOf(DenormalizerInterface::class));
+
+ new Serializer(array($denormalizerAware));
+ }
+
+ public function testDeserializeObjectConstructorWithObjectTypeHint()
+ {
+ $jsonData = '{"bar":{"value":"baz"}}';
+
+ $serializer = new Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()));
+
+ $this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
+ }
}
class Model
return array('title' => $this->title, 'numbers' => $this->numbers);
}
}
+
+class Foo
+{
+ private $bar;
+
+ public function __construct(Bar $bar)
+ {
+ $this->bar = $bar;
+ }
+}
+
+class Bar
+{
+ private $value;
+
+ public function __construct($value)
+ {
+ $this->value = $value;
+ }
+}