From 25ce2188bbc3445c62f058d877e86505552a019b Mon Sep 17 00:00:00 2001 From: Romain Canon Date: Thu, 3 Nov 2022 20:01:29 +0100 Subject: [PATCH] test: add test to check mapping with missing value for nullable node --- .../Mapping/Other/FlexibleMappingTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Integration/Mapping/Other/FlexibleMappingTest.php b/tests/Integration/Mapping/Other/FlexibleMappingTest.php index 01a8011..0753e60 100644 --- a/tests/Integration/Mapping/Other/FlexibleMappingTest.php +++ b/tests/Integration/Mapping/Other/FlexibleMappingTest.php @@ -149,6 +149,28 @@ final class FlexibleMappingTest extends IntegrationTest self::assertSame($source, $result); } + public function test_missing_value_for_nullable_property_fills_it_with_null(): void + { + $class = new class () { + public string $foo; + + /** @noRector \Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector */ + public ?string $bar; + }; + + try { + $result = (new MapperBuilder())->flexible()->mapper()->map( + get_class($class), + ['foo' => 'foo'] + ); + } catch (MappingError $error) { + $this->mappingFail($error); + } + + self::assertSame('foo', $result->foo); + self::assertSame(null, $result->bar); + } + public function test_value_that_cannot_be_cast_throws_exception(): void { try {