|
16 | 16 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
17 | 17 | use Symfony\Component\PropertyInfo\Type; |
18 | 18 | use Symfony\Component\Serializer\Annotation\SerializedName; |
19 | | -use Symfony\Component\Serializer\Encoder\JsonEncoder; |
20 | | -use Symfony\Component\Serializer\Encoder\XmlEncoder; |
21 | 19 | use Symfony\Component\Serializer\Exception\ExtraAttributesException; |
22 | 20 | use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
23 | 21 | use Symfony\Component\Serializer\Exception\LogicException; |
@@ -106,146 +104,146 @@ public function testDenormalizeWithExtraAttributesAndNoGroupsWithMetadataFactory |
106 | 104 | ); |
107 | 105 | } |
108 | 106 |
|
109 | | - public function testDenormalizeWithNestedAttributesAndNoMetadataFactory() |
110 | | - { |
111 | | - $this->expectException(LogicException::class); |
| 107 | + public function testDenormalizeWithNestedAttributesAndNoMetadataFactory() |
| 108 | + { |
| 109 | + $this->expectException(LogicException::class); |
112 | 110 | $this->expectExceptionMessage('A class metadata factory must be provided in the constructor when setting "flatten_nested_attributes" to true.'); |
113 | | - $normalizer = new ObjectNormalizer(); |
114 | | - $normalizer->denormalize( |
115 | | - [], |
116 | | - NestedDummy::class, |
117 | | - 'any', |
118 | | - ['flatten_nested_attributes' => true] |
119 | | - ); |
120 | | - } |
121 | | - |
122 | | - public function testDenormalizeWithDuplicateNestedAttributes() |
123 | | - { |
124 | | - $this->expectException(LogicException::class); |
| 111 | + $normalizer = new ObjectNormalizer(); |
| 112 | + $normalizer->denormalize( |
| 113 | + [], |
| 114 | + NestedDummy::class, |
| 115 | + 'any', |
| 116 | + ['flatten_nested_attributes' => true] |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + public function testDenormalizeWithDuplicateNestedAttributes() |
| 121 | + { |
| 122 | + $this->expectException(LogicException::class); |
125 | 123 | $this->expectExceptionMessage('Duplicate serialized name: "one.two.three"'); |
126 | 124 | $normalizer = new AbstractObjectNormalizerWithMetadata(); |
127 | | - $normalizer->denormalize( |
128 | | - [], |
129 | | - DuplicateNestedDummy::class, |
130 | | - 'any', |
131 | | - ['flatten_nested_attributes' => true] |
132 | | - ); |
133 | | - } |
134 | | - |
135 | | - public function testDenormalizeWithNestedAttributes() |
136 | | - { |
| 125 | + $normalizer->denormalize( |
| 126 | + [], |
| 127 | + DuplicateNestedDummy::class, |
| 128 | + 'any', |
| 129 | + ['flatten_nested_attributes' => true] |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + public function testDenormalizeWithNestedAttributes() |
| 134 | + { |
137 | 135 | $normalizer = new AbstractObjectNormalizerWithMetadata(); |
138 | | - $data = [ |
139 | | - 'one' => [ |
140 | | - 'two' => [ |
141 | | - 'three' => 'foo' |
142 | | - ], |
143 | | - 'four' => 'bar' |
144 | | - ] |
145 | | - ]; |
146 | | - $test = $normalizer->denormalize( |
147 | | - $data, |
148 | | - NestedDummy::class, |
149 | | - 'any', |
150 | | - ['flatten_nested_attributes' => true] |
151 | | - ); |
152 | | - $this->assertEquals('foo', $test->foo); |
153 | | - $this->assertEquals('bar', $test->bar); |
154 | | - } |
155 | | - |
156 | | - public function testNormalizeWithNestedAttributesAndNoMetadataNameConverter() |
157 | | - { |
158 | | - $this->expectException(LogicException::class); |
| 136 | + $data = [ |
| 137 | + 'one' => [ |
| 138 | + 'two' => [ |
| 139 | + 'three' => 'foo', |
| 140 | + ], |
| 141 | + 'four' => 'bar', |
| 142 | + ], |
| 143 | + ]; |
| 144 | + $test = $normalizer->denormalize( |
| 145 | + $data, |
| 146 | + NestedDummy::class, |
| 147 | + 'any', |
| 148 | + ['flatten_nested_attributes' => true] |
| 149 | + ); |
| 150 | + $this->assertEquals('foo', $test->foo); |
| 151 | + $this->assertEquals('bar', $test->bar); |
| 152 | + } |
| 153 | + |
| 154 | + public function testNormalizeWithNestedAttributesAndNoMetadataNameConverter() |
| 155 | + { |
| 156 | + $this->expectException(LogicException::class); |
159 | 157 | $this->expectExceptionMessage('A metadata aware name converter must be provided in the constructor when setting "flatten_nested_attributes" to true.'); |
160 | | - $foobar = new NestedDummy(); |
161 | | - $foobar->foo = 'foo'; |
162 | | - $foobar->bar = 'bar'; |
163 | | - $normalizer = new ObjectNormalizer(); |
164 | | - $normalizer->normalize( |
165 | | - $foobar, |
166 | | - 'any', |
167 | | - ['flatten_nested_attributes' => true] |
168 | | - ); |
169 | | - } |
170 | | - |
171 | | - public function testNormalizeWithNestedAttributesMixingArrayTypes() |
172 | | - { |
173 | | - $this->expectException(LogicException::class); |
| 158 | + $foobar = new NestedDummy(); |
| 159 | + $foobar->foo = 'foo'; |
| 160 | + $foobar->bar = 'bar'; |
| 161 | + $normalizer = new ObjectNormalizer(); |
| 162 | + $normalizer->normalize( |
| 163 | + $foobar, |
| 164 | + 'any', |
| 165 | + ['flatten_nested_attributes' => true] |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + public function testNormalizeWithNestedAttributesMixingArrayTypes() |
| 170 | + { |
| 171 | + $this->expectException(LogicException::class); |
174 | 172 | $this->expectExceptionMessage('The element you are trying to set is already populated: "two"'); |
175 | | - $foobar = new AlreadyPopulatedNestedDummy(); |
176 | | - $foobar->foo = 'foo'; |
177 | | - $foobar->bar = 'bar'; |
178 | | - $normalizer = new ObjectNormalizer( |
179 | | - null, |
180 | | - new MetadataAwareNameConverter( |
181 | | - new ClassMetadataFactory( |
182 | | - new AnnotationLoader( |
183 | | - new AnnotationReader() |
184 | | - ) |
185 | | - ) |
186 | | - ) |
187 | | - ); |
188 | | - $normalizer->normalize( |
189 | | - $foobar, |
190 | | - 'any', |
191 | | - ['flatten_nested_attributes' => true] |
192 | | - ); |
193 | | - } |
194 | | - |
195 | | - public function testNormalizeWithNestedAttributesElementAlreadySet() |
196 | | - { |
197 | | - $this->expectException(LogicException::class); |
| 173 | + $foobar = new AlreadyPopulatedNestedDummy(); |
| 174 | + $foobar->foo = 'foo'; |
| 175 | + $foobar->bar = 'bar'; |
| 176 | + $normalizer = new ObjectNormalizer( |
| 177 | + null, |
| 178 | + new MetadataAwareNameConverter( |
| 179 | + new ClassMetadataFactory( |
| 180 | + new AnnotationLoader( |
| 181 | + new AnnotationReader() |
| 182 | + ) |
| 183 | + ) |
| 184 | + ) |
| 185 | + ); |
| 186 | + $normalizer->normalize( |
| 187 | + $foobar, |
| 188 | + 'any', |
| 189 | + ['flatten_nested_attributes' => true] |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + public function testNormalizeWithNestedAttributesElementAlreadySet() |
| 194 | + { |
| 195 | + $this->expectException(LogicException::class); |
198 | 196 | $this->expectExceptionMessage('The element you are trying to set is already populated: "three"'); |
199 | | - $foobar = new DuplicateNestedDummy(); |
200 | | - $foobar->foo = 'foo'; |
201 | | - $foobar->bar = 'bar'; |
202 | | - $normalizer = new ObjectNormalizer( |
203 | | - null, |
204 | | - new MetadataAwareNameConverter( |
205 | | - new ClassMetadataFactory( |
206 | | - new AnnotationLoader( |
207 | | - new AnnotationReader() |
208 | | - ) |
209 | | - ) |
210 | | - ) |
211 | | - ); |
212 | | - $normalizer->normalize( |
213 | | - $foobar, |
214 | | - 'any', |
215 | | - ['flatten_nested_attributes' => true] |
216 | | - ); |
217 | | - } |
218 | | - |
219 | | - public function testNormalizeWithNestedAttributes() |
220 | | - { |
221 | | - $foobar = new NestedDummy(); |
222 | | - $foobar->foo = 'foo'; |
223 | | - $foobar->bar = 'bar'; |
224 | | - $data = [ |
225 | | - 'one' => [ |
226 | | - 'two' => [ |
227 | | - 'three' => 'foo' |
228 | | - ], |
229 | | - 'four' => 'bar' |
230 | | - ] |
231 | | - ]; |
232 | | - $normalizer = new ObjectNormalizer( |
233 | | - null, |
234 | | - new MetadataAwareNameConverter( |
235 | | - new ClassMetadataFactory( |
236 | | - new AnnotationLoader( |
237 | | - new AnnotationReader() |
238 | | - ) |
239 | | - ) |
240 | | - ) |
241 | | - ); |
242 | | - $test = $normalizer->normalize( |
243 | | - $foobar, |
244 | | - 'any', |
245 | | - ['flatten_nested_attributes' => true] |
246 | | - ); |
247 | | - $this->assertEquals($data, $test); |
248 | | - } |
| 197 | + $foobar = new DuplicateNestedDummy(); |
| 198 | + $foobar->foo = 'foo'; |
| 199 | + $foobar->bar = 'bar'; |
| 200 | + $normalizer = new ObjectNormalizer( |
| 201 | + null, |
| 202 | + new MetadataAwareNameConverter( |
| 203 | + new ClassMetadataFactory( |
| 204 | + new AnnotationLoader( |
| 205 | + new AnnotationReader() |
| 206 | + ) |
| 207 | + ) |
| 208 | + ) |
| 209 | + ); |
| 210 | + $normalizer->normalize( |
| 211 | + $foobar, |
| 212 | + 'any', |
| 213 | + ['flatten_nested_attributes' => true] |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | + public function testNormalizeWithNestedAttributes() |
| 218 | + { |
| 219 | + $foobar = new NestedDummy(); |
| 220 | + $foobar->foo = 'foo'; |
| 221 | + $foobar->bar = 'bar'; |
| 222 | + $data = [ |
| 223 | + 'one' => [ |
| 224 | + 'two' => [ |
| 225 | + 'three' => 'foo', |
| 226 | + ], |
| 227 | + 'four' => 'bar', |
| 228 | + ], |
| 229 | + ]; |
| 230 | + $normalizer = new ObjectNormalizer( |
| 231 | + null, |
| 232 | + new MetadataAwareNameConverter( |
| 233 | + new ClassMetadataFactory( |
| 234 | + new AnnotationLoader( |
| 235 | + new AnnotationReader() |
| 236 | + ) |
| 237 | + ) |
| 238 | + ) |
| 239 | + ); |
| 240 | + $test = $normalizer->normalize( |
| 241 | + $foobar, |
| 242 | + 'any', |
| 243 | + ['flatten_nested_attributes' => true] |
| 244 | + ); |
| 245 | + $this->assertEquals($data, $test); |
| 246 | + } |
249 | 247 |
|
250 | 248 | public function testDenormalizeCollectionDecodedFromXmlWithOneChild() |
251 | 249 | { |
@@ -574,42 +572,41 @@ class EmptyDummy |
574 | 572 |
|
575 | 573 | class AlreadyPopulatedNestedDummy |
576 | 574 | { |
577 | | - /** |
578 | | - * @SerializedName("one.two.three") |
579 | | - */ |
580 | | - public $foo; |
581 | | - |
582 | | - /** |
583 | | - * @SerializedName("one.two") |
584 | | - */ |
585 | | - public $bar; |
586 | | -} |
| 575 | + /** |
| 576 | + * @SerializedName("one.two.three") |
| 577 | + */ |
| 578 | + public $foo; |
587 | 579 |
|
| 580 | + /** |
| 581 | + * @SerializedName("one.two") |
| 582 | + */ |
| 583 | + public $bar; |
| 584 | +} |
588 | 585 |
|
589 | 586 | class DuplicateNestedDummy |
590 | 587 | { |
591 | | - /** |
592 | | - * @SerializedName("one.two.three") |
593 | | - */ |
594 | | - public $foo; |
595 | | - |
596 | | - /** |
597 | | - * @SerializedName("one.two.three") |
598 | | - */ |
599 | | - public $bar; |
| 588 | + /** |
| 589 | + * @SerializedName("one.two.three") |
| 590 | + */ |
| 591 | + public $foo; |
| 592 | + |
| 593 | + /** |
| 594 | + * @SerializedName("one.two.three") |
| 595 | + */ |
| 596 | + public $bar; |
600 | 597 | } |
601 | 598 |
|
602 | 599 | class NestedDummy |
603 | 600 | { |
604 | | - /** |
605 | | - * @SerializedName("one.two.three") |
606 | | - */ |
607 | | - public $foo; |
608 | | - |
609 | | - /** |
610 | | - * @SerializedName("one.four") |
611 | | - */ |
612 | | - public $bar; |
| 601 | + /** |
| 602 | + * @SerializedName("one.two.three") |
| 603 | + */ |
| 604 | + public $foo; |
| 605 | + |
| 606 | + /** |
| 607 | + * @SerializedName("one.four") |
| 608 | + */ |
| 609 | + public $bar; |
613 | 610 | } |
614 | 611 |
|
615 | 612 | class AbstractObjectNormalizerWithMetadata extends AbstractObjectNormalizer |
|
0 commit comments