|
21 | 21 | use Symfony\Component\Serializer\SerializerInterface;
|
22 | 22 | use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
23 | 23 | use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
| 24 | +use Symfony\Component\Serializer\Tests\Fixtures\DenormalizerDecoratorSerializer; |
24 | 25 | use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
|
25 | 26 | use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
26 | 27 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
@@ -157,6 +158,49 @@ public function testConstructorWithObjectDenormalize()
|
157 | 158 | $this->assertEquals('bar', $obj->bar);
|
158 | 159 | }
|
159 | 160 |
|
| 161 | + public function testConstructorWithObjectTypeHintDenormalize() |
| 162 | + { |
| 163 | + $data = array( |
| 164 | + 'id' => 10, |
| 165 | + 'inner' => array( |
| 166 | + 'foo' => 'oof', |
| 167 | + 'bar' => 'rab', |
| 168 | + ), |
| 169 | + ); |
| 170 | + |
| 171 | + $normalizer = new ObjectNormalizer(); |
| 172 | + $serializer = new DenormalizerDecoratorSerializer($normalizer); |
| 173 | + $normalizer->setSerializer($serializer); |
| 174 | + |
| 175 | + $obj = $normalizer->denormalize($data, DummyWithConstructorObject::class); |
| 176 | + $this->assertInstanceOf(DummyWithConstructorObject::class, $obj); |
| 177 | + $this->assertEquals(10, $obj->getId()); |
| 178 | + $this->assertInstanceOf(ObjectInner::class, $obj->getInner()); |
| 179 | + $this->assertEquals('oof', $obj->getInner()->foo); |
| 180 | + $this->assertEquals('rab', $obj->getInner()->bar); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @expectedException \Symfony\Component\Serializer\Exception\RuntimeException |
| 185 | + * @expectedExceptionMessage Could not determine the class of the parameter "unknown". |
| 186 | + */ |
| 187 | + public function testConstructorWithUnknownObjectTypeHintDenormalize() |
| 188 | + { |
| 189 | + $data = array( |
| 190 | + 'id' => 10, |
| 191 | + 'unknown' => array( |
| 192 | + 'foo' => 'oof', |
| 193 | + 'bar' => 'rab', |
| 194 | + ), |
| 195 | + ); |
| 196 | + |
| 197 | + $normalizer = new ObjectNormalizer(); |
| 198 | + $serializer = new DenormalizerDecoratorSerializer($normalizer); |
| 199 | + $normalizer->setSerializer($serializer); |
| 200 | + |
| 201 | + $normalizer->denormalize($data, DummyWithConstructorInexistingObject::class); |
| 202 | + } |
| 203 | + |
160 | 204 | public function testGroupsNormalize()
|
161 | 205 | {
|
162 | 206 | $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
@@ -782,3 +826,32 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
|
782 | 826 | return false;
|
783 | 827 | }
|
784 | 828 | }
|
| 829 | + |
| 830 | +class DummyWithConstructorObject |
| 831 | +{ |
| 832 | + private $id; |
| 833 | + private $inner; |
| 834 | + |
| 835 | + public function __construct($id, ObjectInner $inner) |
| 836 | + { |
| 837 | + $this->id = $id; |
| 838 | + $this->inner = $inner; |
| 839 | + } |
| 840 | + |
| 841 | + public function getId() |
| 842 | + { |
| 843 | + return $this->id; |
| 844 | + } |
| 845 | + |
| 846 | + public function getInner() |
| 847 | + { |
| 848 | + return $this->inner; |
| 849 | + } |
| 850 | +} |
| 851 | + |
| 852 | +class DummyWithConstructorInexistingObject |
| 853 | +{ |
| 854 | + public function __construct($id, Unknown $unknown) |
| 855 | + { |
| 856 | + } |
| 857 | +} |
0 commit comments