|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Psl\Tests\Unit\Type; |
| 6 | + |
| 7 | +use Override; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use Psl\Str; |
| 10 | +use Psl\Type; |
| 11 | + |
| 12 | +final class JsonDecodedTypeTest extends TypeTestCase |
| 13 | +{ |
| 14 | + #[Override] |
| 15 | + public static function getType(): Type\TypeInterface |
| 16 | + { |
| 17 | + return Type\json_decoded(Type\shape([ |
| 18 | + 'name' => Type\string(), |
| 19 | + 'age' => Type\int(), |
| 20 | + ])); |
| 21 | + } |
| 22 | + |
| 23 | + #[Override] |
| 24 | + public static function getValidCoercions(): iterable |
| 25 | + { |
| 26 | + yield [ |
| 27 | + '{"name": "Alice", "age": 30}', |
| 28 | + ['name' => 'Alice', 'age' => 30], |
| 29 | + ]; |
| 30 | + yield [ |
| 31 | + '{"name": "Bob", "age": 25}', |
| 32 | + ['name' => 'Bob', 'age' => 25], |
| 33 | + ]; |
| 34 | + // Already-decoded value passes through |
| 35 | + yield [ |
| 36 | + ['name' => 'Charlie', 'age' => 40], |
| 37 | + ['name' => 'Charlie', 'age' => 40], |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + #[Override] |
| 42 | + public static function getInvalidCoercions(): iterable |
| 43 | + { |
| 44 | + yield [1]; |
| 45 | + yield [false]; |
| 46 | + yield [null]; |
| 47 | + yield ['not json']; |
| 48 | + yield ['{"name": "Alice"}']; // missing 'age' |
| 49 | + yield ['{invalid}']; |
| 50 | + } |
| 51 | + |
| 52 | + #[Override] |
| 53 | + public static function getToStringExamples(): iterable |
| 54 | + { |
| 55 | + yield [static::getType(), "json-decoded<array{'name': string, 'age': int}>"]; |
| 56 | + } |
| 57 | + |
| 58 | + public static function provideCoerceExceptionExpectations(): iterable |
| 59 | + { |
| 60 | + yield 'non-string input' => [ |
| 61 | + Type\json_decoded(Type\dict(Type\string(), Type\mixed())), |
| 62 | + 42, |
| 63 | + 'Could not coerce "int" to type "json-decoded<dict<string, mixed>>".', |
| 64 | + ]; |
| 65 | + yield 'invalid json' => [ |
| 66 | + Type\json_decoded(Type\dict(Type\string(), Type\mixed())), |
| 67 | + '{invalid}', |
| 68 | + 'Could not coerce "string" to type "json-decoded<dict<string, mixed>>" at path "coerce_input(string): dict<string, mixed>": Syntax error..', |
| 69 | + ]; |
| 70 | + yield 'decoded value does not match inner type' => [ |
| 71 | + Type\json_decoded(Type\vec(Type\int())), |
| 72 | + '{"key": "value"}', |
| 73 | + 'Could not coerce "string" to type "json-decoded<vec<int>>" at path "coerce_output(array): vec<int>.key".', |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + #[DataProvider('provideCoerceExceptionExpectations')] |
| 78 | + public function testInvalidCoercionTypeExceptions( |
| 79 | + Type\TypeInterface $type, |
| 80 | + mixed $data, |
| 81 | + string $expectedMessage, |
| 82 | + ): void { |
| 83 | + try { |
| 84 | + $type->coerce($data); |
| 85 | + static::fail(Str\format('Expected "%s" exception to be thrown.', Type\Exception\CoercionException::class)); |
| 86 | + } catch (Type\Exception\CoercionException $e) { |
| 87 | + static::assertSame($expectedMessage, $e->getMessage()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public function testCoercesJsonStringInShape(): void |
| 92 | + { |
| 93 | + $shape = Type\shape([ |
| 94 | + 'name' => Type\string(), |
| 95 | + 'metadata' => Type\json_decoded(Type\shape([ |
| 96 | + 'role' => Type\string(), |
| 97 | + 'active' => Type\bool(), |
| 98 | + ])), |
| 99 | + ]); |
| 100 | + |
| 101 | + $result = $shape->coerce([ |
| 102 | + 'name' => 'Alice', |
| 103 | + 'metadata' => '{"role": "admin", "active": true}', |
| 104 | + ]); |
| 105 | + |
| 106 | + static::assertSame( |
| 107 | + [ |
| 108 | + 'name' => 'Alice', |
| 109 | + 'metadata' => ['role' => 'admin', 'active' => true], |
| 110 | + ], |
| 111 | + $result, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + public function testPassesThroughAlreadyDecodedValueInShape(): void |
| 116 | + { |
| 117 | + $shape = Type\shape([ |
| 118 | + 'name' => Type\string(), |
| 119 | + 'metadata' => Type\json_decoded(Type\shape([ |
| 120 | + 'role' => Type\string(), |
| 121 | + ])), |
| 122 | + ]); |
| 123 | + |
| 124 | + $result = $shape->coerce([ |
| 125 | + 'name' => 'Alice', |
| 126 | + 'metadata' => ['role' => 'admin'], |
| 127 | + ]); |
| 128 | + |
| 129 | + static::assertSame( |
| 130 | + [ |
| 131 | + 'name' => 'Alice', |
| 132 | + 'metadata' => ['role' => 'admin'], |
| 133 | + ], |
| 134 | + $result, |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + public function testWithSimpleType(): void |
| 139 | + { |
| 140 | + $type = Type\json_decoded(Type\int()); |
| 141 | + |
| 142 | + static::assertSame(42, $type->coerce('42')); |
| 143 | + static::assertSame(42, $type->coerce(42)); |
| 144 | + } |
| 145 | +} |
0 commit comments