|
3 | 3 | namespace Nuxtifyts\PhpDto\Tests\Unit\Serializers;
|
4 | 4 |
|
5 | 5 | use DateTimeImmutable;
|
| 6 | +use Nuxtifyts\PhpDto\Attributes\PropertyAttributes\ArrayOfScalarTypes; |
6 | 7 | use Nuxtifyts\PhpDto\Contexts\PropertyContext;
|
7 | 8 | use Nuxtifyts\PhpDto\Contexts\TypeContext;
|
| 9 | +use Nuxtifyts\PhpDto\Data; |
8 | 10 | use Nuxtifyts\PhpDto\Enums\Property\Type;
|
| 11 | +use Nuxtifyts\PhpDto\Exceptions\SerializeException; |
9 | 12 | use Nuxtifyts\PhpDto\Serializers\ArraySerializer;
|
10 | 13 | use Nuxtifyts\PhpDto\Tests\Dummies\ArrayOfMixedAttributesData;
|
11 | 14 | use Nuxtifyts\PhpDto\Tests\Dummies\Enums\YesNoBackedEnum;
|
@@ -37,6 +40,39 @@ public function supports_array_types(): void
|
37 | 40 | );
|
38 | 41 | }
|
39 | 42 |
|
| 43 | + /** |
| 44 | + * @throws Throwable |
| 45 | + */ |
| 46 | + #[Test] |
| 47 | + public function is_able_to_handle_null_if_property_is_nullable(): void |
| 48 | + { |
| 49 | + $object = new readonly class (null) extends Data { |
| 50 | + /** |
| 51 | + * @param ?list<int> $arrayOfIntegers |
| 52 | + */ |
| 53 | + public function __construct( |
| 54 | + #[ArrayOfScalarTypes(Type::INT)] |
| 55 | + public ?array $arrayOfIntegers |
| 56 | + ) { |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + $reflectionClass = new ReflectionClass($object); |
| 61 | + $property = $reflectionClass->getProperty('arrayOfIntegers'); |
| 62 | + |
| 63 | + $arraySerializer = new ArraySerializer(); |
| 64 | + |
| 65 | + self::assertEquals( |
| 66 | + ['arrayOfIntegers' => null], |
| 67 | + $arraySerializer->serialize(PropertyContext::getInstance($property), $object) |
| 68 | + ); |
| 69 | + |
| 70 | + self::assertEquals( |
| 71 | + null, |
| 72 | + $arraySerializer->deserialize(PropertyContext::getInstance($property), ['arrayOfIntegers' => null]) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
40 | 76 | /**
|
41 | 77 | * @param array<string, mixed> $expectedSerializedValue
|
42 | 78 | *
|
|
0 commit comments