Skip to content

Commit 1ac0fb3

Browse files
committed
add cast tests
1 parent 7438e83 commit 1ac0fb3

File tree

8 files changed

+42
-16
lines changed

8 files changed

+42
-16
lines changed

src/Annotations/Input/InputDateFormat.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Astral\Serialize\Support\Collections\DataCollection;
99
use Astral\Serialize\Support\Context\InputValueContext;
1010
use Attribute;
11-
use DateInvalidTimeZoneException;
1211
use DateTime;
1312
use DateTimeInterface;
1413
use DateTimeZone;
@@ -29,9 +28,6 @@ public function match(mixed $value, DataCollection $collection, InputValueContex
2928
return is_string($value) || is_numeric($value);
3029
}
3130

32-
/**
33-
* @throws DateInvalidTimeZoneException
34-
*/
3531
public function resolve(mixed $value, DataCollection $collection, InputValueContext $context): string|DateTime
3632
{
3733

src/Annotations/Output/OutputDateFormat.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Astral\Serialize\Support\Collections\DataCollection;
99
use Astral\Serialize\Support\Context\OutContext;
1010
use Attribute;
11-
use DateInvalidTimeZoneException;
1211
use DateTime;
1312
use DateTimeInterface;
1413
use DateTimeZone;
@@ -33,9 +32,6 @@ public function resolve(mixed $value, DataCollection $collection, OutContext $co
3332
return $this->formatValue($value);
3433
}
3534

36-
/**
37-
* @throws DateInvalidTimeZoneException
38-
*/
3935
private function formatValue(mixed $value): ?string
4036
{
4137
$timezone = $this->timezone ? new DateTimeZone($this->timezone) : null;

src/Casts/Normalizer/DateTimeNormalizerCast.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;
66
use DateTimeInterface;
7-
use JsonException;
7+
use Throwable;
88

99
class DateTimeNormalizerCast implements NormalizerCastInterface
1010
{
@@ -18,8 +18,7 @@ public function resolve(mixed $values): mixed
1818
if ($this->match($values)) {
1919
try {
2020
return $values->format('Y-m-d H:i:s');
21-
} catch (JsonException $e) {
22-
return '';
21+
} catch (Throwable $e) {
2322
}
2423
}
2524

src/Casts/Normalizer/JsonNormalizerCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function match(mixed $values): bool
1212
return is_string($values);
1313
}
1414

15-
public function resolve(mixed $values): array
15+
public function resolve(mixed $values): mixed
1616
{
1717
if ($this->match($values)) {
1818
try {

src/OpenApi/Collections/OpenApiCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function __construct(
3131
public string $methodName,
3232
public reflectionMethod $reflectionMethod,
3333
public Tag $tag,
34-
public Summary $summary,
35-
public Route $route,
34+
public Summary|null $summary,
35+
public Route|null $route,
3636
public Headers|null $headers,
3737
public array $attributes,
3838
public RequestBody|null $requestBody,

src/Support/Config/ConfigManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private function instantiateArrayProperties(array $propertyNames): void
9494
* @return static
9595
* @throws RuntimeException
9696
*/
97-
private function addCast(object|string $castClass, ConfigCastEnum $castEnum): static
97+
public function addCast(object|string $castClass, ConfigCastEnum $castEnum): static
9898
{
9999
if (is_string($castClass) && !is_subclass_of($castClass, $castEnum->getCastInterface())) {
100100
throw new RuntimeException("Cast class must be an instance of {$castEnum->getCastInterface()}");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
4+
use Astral\Serialize\Annotations\DataCollection\InputName;
5+
use Astral\Serialize\Casts\Normalizer\JsonNormalizerCast;
6+
use Astral\Serialize\Enums\ConfigCastEnum;
7+
use Astral\Serialize\Serialize;
8+
use Astral\Serialize\Support\Config\ConfigManager;
9+
10+
beforeAll(function () {
11+
12+
13+
class AddCastTestClass extends Serialize
14+
{
15+
#[InputName('name_three_other')]
16+
public string $name_three;
17+
18+
public int $id_three;
19+
}
20+
21+
});
22+
23+
it('test add cast to config', function () {
24+
25+
$res = AddCastTestClass::from('{"name_three_other":"1223","id_three":3}');
26+
expect($res->toArray())->toHaveCount(2)
27+
->and($res->toArray()['name_three'])->toBeNull()
28+
->and($res->toArray()['id_three'])->toBeNull();
29+
30+
ConfigManager::getInstance()->addCast(JsonNormalizerCast::class, ConfigCastEnum::INPUT_NORMALIZER);
31+
$res = AddCastTestClass::from('{"name_three_other":"1223","id_three":3}');
32+
expect($res->toArray())->toHaveCount(2)
33+
->and($res->toArray()['name_three'])->toBe('1223')
34+
->and($res->toArray()['id_three'])->toBe(3);
35+
36+
});

tests/Serialize/From/NormalizerFromSerializeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class NormalizerClass extends Serialize
8080
expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}');
8181

8282
$resJson = $res->withoutResponseToJsonString();
83-
var_dump($resJson);
8483
expect($resJson)->toBe('{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}');
8584

8685
$resJson = $res->toJsonString();

0 commit comments

Comments
 (0)