|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Chubbyphp\Tests\Parsing\Unit\Schema; |
| 6 | + |
| 7 | +use Chubbyphp\Parsing\Error; |
| 8 | +use Chubbyphp\Parsing\ErrorsException; |
| 9 | +use Chubbyphp\Parsing\Schema\AbstractSchema; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * @covers \Chubbyphp\Parsing\Schema\AbstractSchema |
| 14 | + * |
| 15 | + * @internal |
| 16 | + */ |
| 17 | +final class AbstractSchemaTest extends TestCase |
| 18 | +{ |
| 19 | + public function testNullable(): void |
| 20 | + { |
| 21 | + $schema = new class extends AbstractSchema { |
| 22 | + public function parse(mixed $input): mixed |
| 23 | + { |
| 24 | + if (null === $input && $this->nullable) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + if (null === $input) { |
| 29 | + throw new ErrorsException(new Error('type', 'Type error', [])); |
| 30 | + } |
| 31 | + |
| 32 | + return $input; |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + self::assertNotSame($schema, $schema->nullable()); |
| 37 | + self::assertNull($schema->nullable()->parse(null)); |
| 38 | + self::assertNull($schema->nullable(true)->parse(null)); |
| 39 | + |
| 40 | + try { |
| 41 | + $schema->nullable(false)->parse(null); |
| 42 | + |
| 43 | + throw new \Exception('code should not be reached'); |
| 44 | + } catch (ErrorsException) { |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function testDefault(): void |
| 49 | + { |
| 50 | + $schema = new class extends AbstractSchema { |
| 51 | + public function parse(mixed $input): mixed |
| 52 | + { |
| 53 | + return $this->dispatchPreParses($input); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + self::assertNotSame($schema, $schema->default('default')); |
| 58 | + self::assertSame('default', $schema->default('default')->parse(null)); |
| 59 | + self::assertSame('value', $schema->default('default')->parse('value')); |
| 60 | + } |
| 61 | + |
| 62 | + public function testPreParse(): void |
| 63 | + { |
| 64 | + $schema = new class extends AbstractSchema { |
| 65 | + public function parse(mixed $input): mixed |
| 66 | + { |
| 67 | + return $this->dispatchPreParses($input); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + self::assertNotSame($schema, $schema->preParse(static fn ($i) => $i)); |
| 72 | + self::assertSame('a12', $schema |
| 73 | + ->preParse(static fn ($i) => $i.'1') |
| 74 | + ->preParse(static fn ($i) => $i.'2') |
| 75 | + ->parse('a')); |
| 76 | + } |
| 77 | + |
| 78 | + public function testPostParse(): void |
| 79 | + { |
| 80 | + $schema = new class extends AbstractSchema { |
| 81 | + public function parse(mixed $input): mixed |
| 82 | + { |
| 83 | + return $this->dispatchPostParses($input); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + self::assertNotSame($schema, $schema->postParse(static fn ($o) => $o)); |
| 88 | + self::assertSame('a12', $schema |
| 89 | + ->postParse(static fn ($o) => $o.'1') |
| 90 | + ->postParse(static fn ($o) => $o.'2') |
| 91 | + ->parse('a')); |
| 92 | + } |
| 93 | + |
| 94 | + public function testSafeParse(): void |
| 95 | + { |
| 96 | + $successSchema = new class extends AbstractSchema { |
| 97 | + public function parse(mixed $input): mixed |
| 98 | + { |
| 99 | + return $input; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + $result = $successSchema->safeParse('value'); |
| 104 | + self::assertTrue($result->success); |
| 105 | + self::assertSame('value', $result->data); |
| 106 | + |
| 107 | + $failSchema = new class extends AbstractSchema { |
| 108 | + public function parse(mixed $input): mixed |
| 109 | + { |
| 110 | + throw new ErrorsException(new Error('error', 'Error', [])); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + $result = $failSchema->safeParse('value'); |
| 115 | + self::assertFalse($result->success); |
| 116 | + self::assertInstanceOf(ErrorsException::class, $result->exception); |
| 117 | + } |
| 118 | + |
| 119 | + public function testCatch(): void |
| 120 | + { |
| 121 | + $schema = new class extends AbstractSchema { |
| 122 | + public function parse(mixed $input): mixed |
| 123 | + { |
| 124 | + $e = new ErrorsException(new Error('error', 'Error', [])); |
| 125 | + if (null !== $this->catch) { |
| 126 | + return ($this->catch)($input, $e); |
| 127 | + } |
| 128 | + |
| 129 | + throw $e; |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + self::assertNotSame($schema, $schema->catch(static fn ($i, $e) => $i)); |
| 134 | + self::assertSame('caught', $schema->catch(static fn ($i, $e) => 'caught')->parse('value')); |
| 135 | + } |
| 136 | + |
| 137 | + public function testGetDataType(): void |
| 138 | + { |
| 139 | + $schema = new class extends AbstractSchema { |
| 140 | + public function parse(mixed $input): mixed |
| 141 | + { |
| 142 | + throw new ErrorsException(new Error('type', '{{given}}', ['given' => $this->getDataType($input)])); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + try { |
| 147 | + $schema->parse('string'); |
| 148 | + } catch (ErrorsException $e) { |
| 149 | + self::assertSame('string', $e->errors->jsonSerialize()[0]['error']['variables']['given']); |
| 150 | + } |
| 151 | + |
| 152 | + try { |
| 153 | + $schema->parse(new \stdClass()); |
| 154 | + } catch (ErrorsException $e) { |
| 155 | + self::assertSame(\stdClass::class, $e->errors->jsonSerialize()[0]['error']['variables']['given']); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments