|
15 | 15 | use PHPUnit\Framework\MockObject\MockObject;
|
16 | 16 | use PHPUnit\Framework\TestCase;
|
17 | 17 | use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
|
| 18 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
18 | 19 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
19 | 20 | use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
|
20 | 21 | use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
@@ -286,6 +287,100 @@ public function testConstructorWithObjectDenormalizeUsingPropertyInfoExtractor()
|
286 | 287 | $this->assertEquals('bar', $obj->bar);
|
287 | 288 | }
|
288 | 289 |
|
| 290 | + public function testConstructorWithObjectDenormalizeForStdClass() |
| 291 | + { |
| 292 | + $data = [ |
| 293 | + 'foo' => ['bar' => 'baz'], |
| 294 | + 'oof' => 'rab', |
| 295 | + ]; |
| 296 | + |
| 297 | + $obj = $this->normalizer->denormalize($data, \stdClass::class, 'any'); |
| 298 | + |
| 299 | + $expected = new \stdClass(); |
| 300 | + $expected->foo = ['bar' => 'baz']; |
| 301 | + $expected->oof = 'rab'; |
| 302 | + |
| 303 | + self::assertEquals($expected, $obj); |
| 304 | + } |
| 305 | + |
| 306 | + public function testConstructorWithObjectDenormalizeForExtendingStdClass() |
| 307 | + { |
| 308 | + $data = [ |
| 309 | + 'foo' => ['bar' => 'baz'], |
| 310 | + 'oof' => 'rab', |
| 311 | + ]; |
| 312 | + |
| 313 | + $obj = $this->normalizer->denormalize($data, ObjectDummyThatExtendsStdClass::class, 'any'); |
| 314 | + |
| 315 | + $expected = new ObjectDummyThatExtendsStdClass(); |
| 316 | + $expected->foo = ['bar' => 'baz']; |
| 317 | + $expected->oof = 'rab'; |
| 318 | + |
| 319 | + self::assertEquals($expected, $obj); |
| 320 | + } |
| 321 | + |
| 322 | + public function testNormalizeWithObjectFromStdClass() |
| 323 | + { |
| 324 | + $propertyAccess = PropertyAccess::createPropertyAccessorBuilder(); |
| 325 | + $propertyAccess->disableExceptionOnInvalidPropertyPath(); |
| 326 | + $propertyAccess->disableExceptionOnInvalidIndex(); |
| 327 | + $this->normalizer = new ObjectNormalizer(null, null, $propertyAccess->getPropertyAccessor(), null, null, null, []); |
| 328 | + |
| 329 | + $o1 = new ObjectDummyThatExtendsStdClass(); |
| 330 | + $o1->foo = 'f'; |
| 331 | + $o1->bar = 'b'; |
| 332 | + |
| 333 | + $this->assertSame(['foo' => 'f', 'bar' => 'b'], $this->normalizer->normalize($o1)); |
| 334 | + |
| 335 | + $o2 = new ObjectDummyThatExtendsStdClass(); |
| 336 | + $o2->baz = 'baz'; |
| 337 | + |
| 338 | + $this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2)); |
| 339 | + } |
| 340 | + |
| 341 | + public function testNormalizeWithObjectFromStdClassAndIdenticalPropertyNameAndValue() |
| 342 | + { |
| 343 | + $propertyAccess = PropertyAccess::createPropertyAccessorBuilder(); |
| 344 | + $propertyAccess->disableExceptionOnInvalidPropertyPath(); |
| 345 | + $propertyAccess->disableExceptionOnInvalidIndex(); |
| 346 | + $this->normalizer = new ObjectNormalizer(null, null, $propertyAccess->getPropertyAccessor(), null, null, null, []); |
| 347 | + |
| 348 | + $o2 = new ObjectDummyThatExtendsStdClass(); |
| 349 | + $o2->baz = 'baz'; |
| 350 | + |
| 351 | + $this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2)); |
| 352 | + } |
| 353 | + |
| 354 | + public function testNormalizeWithObjectExtendingStdClass() |
| 355 | + { |
| 356 | + $propertyAccess = PropertyAccess::createPropertyAccessorBuilder(); |
| 357 | + $propertyAccess->disableExceptionOnInvalidPropertyPath(); |
| 358 | + $propertyAccess->disableExceptionOnInvalidIndex(); |
| 359 | + $this->normalizer = new ObjectNormalizer(null, null, $propertyAccess->getPropertyAccessor(), null, null, null, []); |
| 360 | + |
| 361 | + $o2 = new ObjectDummyWithPropertyThatExtendsStdClass(); |
| 362 | + $o2->baz = 'baz'; |
| 363 | + $o2->foo = 'bar'; |
| 364 | + |
| 365 | + $this->assertSame(['foo' => 'bar', 'baz' => 'baz',], $this->normalizer->normalize($o2)); |
| 366 | + } |
| 367 | + |
| 368 | + public function testDenormalizeObjectForExtendingStdClass() |
| 369 | + { |
| 370 | + $data = [ |
| 371 | + 'foo' => 'bar', |
| 372 | + 'oof' => 'rab', |
| 373 | + ]; |
| 374 | + |
| 375 | + $obj = $this->normalizer->denormalize($data, ObjectDummyWithPropertyThatExtendsStdClass::class, 'any'); |
| 376 | + |
| 377 | + $expected = new ObjectDummyWithPropertyThatExtendsStdClass(); |
| 378 | + $expected->foo = 'bar'; |
| 379 | + $expected->oof = 'rab'; |
| 380 | + |
| 381 | + self::assertEquals($expected, $obj); |
| 382 | + } |
| 383 | + |
289 | 384 | public function testConstructorWithObjectTypeHintDenormalize()
|
290 | 385 | {
|
291 | 386 | $data = [
|
@@ -1245,3 +1340,13 @@ class ObjectDummyWithIgnoreAttributeAndPrivateProperty
|
1245 | 1340 |
|
1246 | 1341 | private $private = 'private';
|
1247 | 1342 | }
|
| 1343 | + |
| 1344 | +class ObjectDummyThatExtendsStdClass extends \stdClass |
| 1345 | +{ |
| 1346 | +} |
| 1347 | + |
| 1348 | +class ObjectDummyWithPropertyThatExtendsStdClass extends \stdClass |
| 1349 | +{ |
| 1350 | + public string $foo; |
| 1351 | +} |
| 1352 | + |
0 commit comments