Skip to content

Commit 25eb6a5

Browse files
committed
Bring more test coverage for array serializer
1 parent 394e8d8 commit 25eb6a5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/Unit/Serializers/ArraySerializerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Nuxtifyts\PhpDto\Tests\Unit\Serializers;
44

55
use DateTimeImmutable;
6+
use Nuxtifyts\PhpDto\Attributes\PropertyAttributes\ArrayOfScalarTypes;
67
use Nuxtifyts\PhpDto\Contexts\PropertyContext;
78
use Nuxtifyts\PhpDto\Contexts\TypeContext;
9+
use Nuxtifyts\PhpDto\Data;
810
use Nuxtifyts\PhpDto\Enums\Property\Type;
11+
use Nuxtifyts\PhpDto\Exceptions\SerializeException;
912
use Nuxtifyts\PhpDto\Serializers\ArraySerializer;
1013
use Nuxtifyts\PhpDto\Tests\Dummies\ArrayOfMixedAttributesData;
1114
use Nuxtifyts\PhpDto\Tests\Dummies\Enums\YesNoBackedEnum;
@@ -37,6 +40,39 @@ public function supports_array_types(): void
3740
);
3841
}
3942

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+
4076
/**
4177
* @param array<string, mixed> $expectedSerializedValue
4278
*

0 commit comments

Comments
 (0)