Skip to content

Commit 9303455

Browse files
committed
Add AbstractSchemaTest
1 parent 66591ca commit 9303455

17 files changed

Lines changed: 158 additions & 16 deletions
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

tests/Unit/Schema/ArraySchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPUnit\Framework\TestCase;
1313

1414
/**
15-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
1615
* @covers \Chubbyphp\Parsing\Schema\ArraySchema
1716
*
1817
* @internal

tests/Unit/Schema/AssocSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public function jsonSerialize(): array
2727
}
2828

2929
/**
30-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
3130
* @covers \Chubbyphp\Parsing\Schema\AssocSchema
3231
*
3332
* @internal

tests/Unit/Schema/BackedEnumSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ enum BackedSuitInt: int
3535
enum BackedEmpty: string {}
3636

3737
/**
38-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
3938
* @covers \Chubbyphp\Parsing\Schema\BackedEnumSchema
4039
*
4140
* @internal

tests/Unit/Schema/BoolSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPUnit\Framework\TestCase;
1010

1111
/**
12-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
1312
* @covers \Chubbyphp\Parsing\Schema\BoolSchema
1413
*
1514
* @internal

tests/Unit/Schema/DateTimeSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPUnit\Framework\TestCase;
1010

1111
/**
12-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
1312
* @covers \Chubbyphp\Parsing\Schema\DateTimeSchema
1413
*
1514
* @internal

tests/Unit/Schema/DiscriminatedUnionSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public function jsonSerialize(): array
2828
}
2929

3030
/**
31-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
3231
* @covers \Chubbyphp\Parsing\Schema\DiscriminatedUnionSchema
3332
*
3433
* @internal

tests/Unit/Schema/FloatSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPUnit\Framework\TestCase;
1010

1111
/**
12-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
1312
* @covers \Chubbyphp\Parsing\Schema\FloatSchema
1413
*
1514
* @internal

tests/Unit/Schema/IntSchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPUnit\Framework\TestCase;
1010

1111
/**
12-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
1312
* @covers \Chubbyphp\Parsing\Schema\IntSchema
1413
*
1514
* @internal

tests/Unit/Schema/LazySchemaTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPUnit\Framework\TestCase;
1111

1212
/**
13-
* @covers \Chubbyphp\Parsing\Schema\AbstractSchema
1413
* @covers \Chubbyphp\Parsing\Schema\LazySchema
1514
*
1615
* @internal

0 commit comments

Comments
 (0)