Skip to content

Commit 6e49ae5

Browse files
committed
Added more multiple data types tests
1 parent ca1a9ac commit 6e49ae5

File tree

5 files changed

+115
-23
lines changed

5 files changed

+115
-23
lines changed

src/Serializers/BackedEnumSerializer.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BackedEnum;
99
use Nuxtifyts\PhpDto\Exceptions\DeserializeException;
1010
use Nuxtifyts\PhpDto\Exceptions\SerializeException;
11+
use Exception;
1112

1213
class BackedEnumSerializer extends Serializer
1314
{
@@ -29,7 +30,7 @@ public function serialize(PropertyContext $property, object $object): array
2930
$value = $property->getValue($object);
3031

3132
return [
32-
$property->propertyName => match(true) {
33+
$property->propertyName => match (true) {
3334
$value instanceof BackedEnum => $value->value,
3435
$value === null && $property->isNullable => null,
3536
default => throw new SerializeException('Value is not a BackedEnum')
@@ -50,19 +51,25 @@ public function deserialize(PropertyContext $property, ArrayAccess|array $data):
5051

5152
if ($value !== null) {
5253
foreach ($property->getFilteredTypeContexts(...self::supportedTypes()) as $typeContext) {
53-
if (!$typeContext->reflection?->implementsInterface(BackedEnum::class)) {
54-
continue;
55-
}
54+
try {
55+
if (!$typeContext->reflection?->implementsInterface(BackedEnum::class)) {
56+
continue;
57+
}
5658

57-
$enumValue = call_user_func(
58-
// @phpstan-ignore-next-line
59-
[$typeContext->reflection->getName(), 'tryFrom'],
60-
$value
61-
);
59+
$enumValue = call_user_func(
60+
// @phpstan-ignore-next-line
61+
[$typeContext->reflection->getName(), 'tryFrom'],
62+
$value
63+
);
6264

63-
if ($enumValue instanceof BackedEnum) {
64-
return $enumValue;
65+
if ($enumValue instanceof BackedEnum) {
66+
return $enumValue;
67+
}
68+
// @codeCoverageIgnoreStart
69+
} catch (Exception) {
70+
continue;
6571
}
72+
// @codeCoverageIgnoreEnd
6673
}
6774
}
6875

src/Serializers/DataSerializer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function serialize(PropertyContext $property, object $object): array
3030
$value = $property->getValue($object);
3131

3232
return [
33-
$property->propertyName => match(true) {
33+
$property->propertyName => match (true) {
3434
$value instanceof BaseDataContract => $value->jsonSerialize(),
3535
$value === null && $property->isNullable => null,
3636
default => throw new SerializeException('Value is not an instance of BaseDataContract')
@@ -46,8 +46,8 @@ public function deserialize(PropertyContext $property, ArrayAccess|array $data):
4646
$value = $data[$property->propertyName] ?? null;
4747

4848
if (is_array($value)) {
49-
try {
50-
foreach ($property->getFilteredTypeContexts(...self::supportedTypes()) as $typeContext) {
49+
foreach ($property->getFilteredTypeContexts(...self::supportedTypes()) as $typeContext) {
50+
try {
5151
if (!$typeContext->reflection?->implementsInterface(BaseDataContract::class)) {
5252
continue;
5353
}
@@ -63,10 +63,12 @@ public function deserialize(PropertyContext $property, ArrayAccess|array $data):
6363
}
6464

6565
return $deserializedValue;
66+
// @codeCoverageIgnoreStart
67+
} catch (Exception) {
68+
continue;
6669
}
67-
// @codeCoverageIgnoreStart
68-
} catch (Exception) {}
69-
// @codeCoverageIgnoreEnd
70+
// @codeCoverageIgnoreEnd
71+
}
7072
}
7173

7274
return is_null($value) && $property->isNullable

src/Serializers/DateTimeSerializer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function serialize(PropertyContext $property, object $object): array
3232

3333
// TODO: move nullable value outside because it's repetitive
3434
return [
35-
$property->propertyName => match(true) {
35+
$property->propertyName => match (true) {
3636
$value instanceof DateTimeInterface => $value->format(DateTimeInterface::ATOM),
3737
$value === null && $property->isNullable => null,
3838
default => throw new SerializeException('Value is not an instance of DateTimeInterface')
@@ -48,8 +48,8 @@ public function deserialize(PropertyContext $property, ArrayAccess|array $data):
4848
$value = $data[$property->propertyName] ?? null;
4949

5050
if (is_string($value)) {
51-
try {
52-
foreach ($property->getFilteredTypeContexts(...self::supportedTypes()) as $typeContext) {
51+
foreach ($property->getFilteredTypeContexts(...self::supportedTypes()) as $typeContext) {
52+
try {
5353
if (!$typeContext->reflection?->implementsInterface(DateTimeInterface::class)) {
5454
continue;
5555
}
@@ -65,10 +65,12 @@ public function deserialize(PropertyContext $property, ArrayAccess|array $data):
6565
}
6666

6767
return $deserializedValue;
68+
// @codeCoverageIgnoreStart
69+
} catch (Exception) {
70+
continue;
6871
}
69-
// @codeCoverageIgnoreStart
70-
} catch (Exception) {}
71-
// @codeCoverageIgnoreEnd
72+
// @codeCoverageIgnoreEnd
73+
}
7274
}
7375

7476
return is_null($value) && $property->isNullable
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Dummies;
4+
5+
use Nuxtifyts\PhpDto\Data;
6+
use Nuxtifyts\PhpDto\Tests\Dummies\Enums\YesNoBackedEnum;
7+
8+
final readonly class UnionMultipleComplexData extends Data
9+
{
10+
public function __construct(
11+
public YesNoBackedEnum|bool $yesOrNo,
12+
public AddressData|CountryData|null $location
13+
) {
14+
}
15+
}

tests/Unit/Concerns/BaseDataTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Nuxtifyts\PhpDto\Tests\Dummies\Enums\YesNoBackedEnum;
1515
use Nuxtifyts\PhpDto\Tests\Dummies\InvitationData;
1616
use Nuxtifyts\PhpDto\Tests\Dummies\RefundableItemData;
17+
use Nuxtifyts\PhpDto\Tests\Dummies\UnionMultipleComplexData;
1718
use Nuxtifyts\PhpDto\Tests\Dummies\UnionMultipleTypeData;
1819
use Nuxtifyts\PhpDto\Tests\Dummies\YesOrNoData;
1920
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
@@ -38,6 +39,7 @@
3839
#[UsesClass(UnionMultipleTypeData::class)]
3940
#[UsesClass(AddressData::class)]
4041
#[UsesClass(CountryData::class)]
42+
#[UsesClass(UnionMultipleComplexData::class)]
4143
final class BaseDataTest extends UnitCase
4244
{
4345
/**
@@ -318,6 +320,70 @@ public static function will_perform_serialization_and_deserialization_data_provi
318320
'coordinates' => null
319321
],
320322
'expectedSerializedData' => $data
323+
],
324+
'Union multiple type data' => [
325+
'dtoClass' => UnionMultipleComplexData::class,
326+
'data' => $data = [
327+
'yesOrNo' => YesNoBackedEnum::YES->value,
328+
'location' => [
329+
'code' => 'country code 3',
330+
'name' => 'country name 3'
331+
]
332+
],
333+
'expectedProperties' => [
334+
'yesOrNo' => YesNoBackedEnum::YES,
335+
'location' => new CountryData('country code 3', 'country name 3')
336+
],
337+
'expectedSerializedData' => $data
338+
],
339+
'Union multiple type data 2' => [
340+
'dtoClass' => UnionMultipleComplexData::class,
341+
'data' => $data = [
342+
'yesOrNo' => false,
343+
'location' => [
344+
'street' => 'street 3',
345+
'city' => 'city 3',
346+
'state' => 'state 3',
347+
'zip' => 'zip 3',
348+
'country' => [
349+
'name' => 'country name 3',
350+
'code' => 'country code 3'
351+
],
352+
'coordinates' => [
353+
'latitude' => 42.42,
354+
'longitude' => 24.24
355+
]
356+
]
357+
],
358+
'expectedProperties' => [
359+
'yesOrNo' => false,
360+
'location' => new AddressData(
361+
'street 3',
362+
'city 3',
363+
'state 3',
364+
'zip 3',
365+
new CountryData('country code 3', 'country name 3'),
366+
new CoordinatesData(42.42, 24.24)
367+
)
368+
],
369+
'expectedSerializedData' => [
370+
...$data,
371+
'location' => [
372+
'street' => 'street 3',
373+
'city' => 'city 3',
374+
'state' => 'state 3',
375+
'zip' => 'zip 3',
376+
'country' => [
377+
'name' => 'country name 3',
378+
'code' => 'country code 3'
379+
],
380+
'coordinates' => [
381+
'latitude' => 42.42,
382+
'longitude' => 24.24,
383+
'radius' => null
384+
]
385+
]
386+
]
321387
]
322388
];
323389
}

0 commit comments

Comments
 (0)