|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Chubbyphp\Tests\Parsing\Unit\Schema; |
| 6 | + |
| 7 | +use Chubbyphp\Parsing\ErrorsException; |
| 8 | +use Chubbyphp\Parsing\Schema\FloatSchema; |
| 9 | +use Chubbyphp\Parsing\Schema\IntSchema; |
| 10 | +use Chubbyphp\Parsing\Schema\ObjectConstructorSchema; |
| 11 | +use Chubbyphp\Parsing\Schema\StringSchema; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +final class ObjectConstructorDemo implements \JsonSerializable |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + public readonly string $field1, |
| 18 | + public readonly int $field2, |
| 19 | + public readonly ?float $field3 = null, |
| 20 | + ) {} |
| 21 | + |
| 22 | + public function jsonSerialize(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + 'field1' => $this->field1, |
| 26 | + 'field2' => $this->field2, |
| 27 | + 'field3' => $this->field3, |
| 28 | + ]; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +final class ObjectConstructorThrowingTypeErrorDemo |
| 33 | +{ |
| 34 | + public function __construct( |
| 35 | + public readonly string $field1, |
| 36 | + ) { |
| 37 | + throw new \TypeError('some unrelated type error'); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @covers \Chubbyphp\Parsing\Schema\ObjectConstructorSchema |
| 43 | + * |
| 44 | + * @internal |
| 45 | + */ |
| 46 | +final class ObjectConstructorSchemaTest extends TestCase |
| 47 | +{ |
| 48 | + public function testImmutability(): void |
| 49 | + { |
| 50 | + $schema = new ObjectConstructorSchema(['field1' => new StringSchema(), 'field2' => new IntSchema(), 'field3' => new FloatSchema()], ObjectConstructorDemo::class); |
| 51 | + |
| 52 | + self::assertNotSame($schema, $schema->nullable()); |
| 53 | + self::assertNotSame($schema, $schema->nullable(false)); |
| 54 | + self::assertNotSame($schema, $schema->default([])); |
| 55 | + self::assertNotSame($schema, $schema->preParse(static fn (mixed $input) => $input)); |
| 56 | + self::assertNotSame($schema, $schema->postParse(static fn (\stdClass $output) => $output)); |
| 57 | + self::assertNotSame($schema, $schema->catch(static fn (\stdClass $output, ErrorsException $e) => $output)); |
| 58 | + |
| 59 | + self::assertNotSame($schema, $schema->strict()); |
| 60 | + self::assertNotSame($schema, $schema->optional([])); |
| 61 | + } |
| 62 | + |
| 63 | + public function testConstructWithClassname(): void |
| 64 | + { |
| 65 | + try { |
| 66 | + new ObjectConstructorSchema([], 'UnknownClass'); |
| 67 | + |
| 68 | + throw new \Exception('code should not be reached'); |
| 69 | + } catch (\InvalidArgumentException $invalidArgumentException) { |
| 70 | + self::assertSame( |
| 71 | + 'Class "UnknownClass" does not exist or cannot be used for reflection', |
| 72 | + $invalidArgumentException->getMessage() |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public function testConstructWithClassNotHavingAConstructor(): void |
| 78 | + { |
| 79 | + try { |
| 80 | + new ObjectConstructorSchema([], \stdClass::class); |
| 81 | + |
| 82 | + throw new \Exception('code should not be reached'); |
| 83 | + } catch (\InvalidArgumentException $invalidArgumentException) { |
| 84 | + self::assertSame( |
| 85 | + 'Class "'.\stdClass::class.'" does have a __construct method', |
| 86 | + $invalidArgumentException->getMessage() |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public function testConstructWithMissingFieldSchema(): void |
| 92 | + { |
| 93 | + try { |
| 94 | + new ObjectConstructorSchema([], ObjectConstructorDemo::class); |
| 95 | + |
| 96 | + throw new \Exception('code should not be reached'); |
| 97 | + } catch (\InvalidArgumentException $invalidArgumentException) { |
| 98 | + self::assertSame( |
| 99 | + 'Missing fieldToSchema for "'.ObjectConstructorDemo::class.'" __construct parameters: "field1", field2"', |
| 100 | + $invalidArgumentException->getMessage() |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public function testConstructWithAdditionalFieldSchema(): void |
| 106 | + { |
| 107 | + try { |
| 108 | + new ObjectConstructorSchema([ |
| 109 | + 'field1' => new StringSchema(), |
| 110 | + 'field2' => new IntSchema(), |
| 111 | + 'field3' => new FloatSchema(), |
| 112 | + 'field4' => new FloatSchema(), |
| 113 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 114 | + |
| 115 | + throw new \Exception('code should not be reached'); |
| 116 | + } catch (\InvalidArgumentException $invalidArgumentException) { |
| 117 | + self::assertSame( |
| 118 | + 'Additional fieldToSchema for "'.ObjectConstructorDemo::class.'" __construct parameters: "field4"', |
| 119 | + $invalidArgumentException->getMessage() |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public function testSuccessWithAllParameters(): void |
| 125 | + { |
| 126 | + $input = ['field1' => 'test', 'field2' => 5, 'field3' => 3.14159]; |
| 127 | + |
| 128 | + $schema = new ObjectConstructorSchema([ |
| 129 | + 'field1' => new StringSchema(), |
| 130 | + 'field2' => new IntSchema(), |
| 131 | + 'field3' => new FloatSchema(), |
| 132 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 133 | + |
| 134 | + /** @var ObjectConstructorDemo $object */ |
| 135 | + $object = $schema->parse($input); |
| 136 | + |
| 137 | + self::assertInstanceOf(ObjectConstructorDemo::class, $object); |
| 138 | + |
| 139 | + self::assertSame($input, $object->jsonSerialize()); |
| 140 | + } |
| 141 | + |
| 142 | + public function testSuccessWithAllParametersOptionalConsidered(): void |
| 143 | + { |
| 144 | + $input = ['field1' => 'test', 'field2' => 5]; |
| 145 | + |
| 146 | + $schema = new ObjectConstructorSchema([ |
| 147 | + 'field1' => new StringSchema(), |
| 148 | + 'field2' => new IntSchema(), |
| 149 | + 'field3' => new FloatSchema(), |
| 150 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 151 | + |
| 152 | + /** @var ObjectConstructorDemo $object */ |
| 153 | + $object = $schema->parse($input); |
| 154 | + |
| 155 | + self::assertInstanceOf(ObjectConstructorDemo::class, $object); |
| 156 | + |
| 157 | + self::assertSame([...$input, 'field3' => null], $object->jsonSerialize()); |
| 158 | + } |
| 159 | + |
| 160 | + public function testSuccessWithRequiredParameters(): void |
| 161 | + { |
| 162 | + $input = ['field1' => 'test', 'field2' => 5]; |
| 163 | + |
| 164 | + $schema = new ObjectConstructorSchema([ |
| 165 | + 'field1' => new StringSchema(), |
| 166 | + 'field2' => new IntSchema(), |
| 167 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 168 | + |
| 169 | + /** @var ObjectConstructorDemo $object */ |
| 170 | + $object = $schema->parse($input); |
| 171 | + |
| 172 | + self::assertInstanceOf(ObjectConstructorDemo::class, $object); |
| 173 | + |
| 174 | + self::assertSame([...$input, 'field3' => null], $object->jsonSerialize()); |
| 175 | + } |
| 176 | + |
| 177 | + public function testFailedWithInvalidValue(): void |
| 178 | + { |
| 179 | + $input = ['field1' => 'test', 'field2' => 5, 'field3' => 'test']; |
| 180 | + |
| 181 | + $schema = new ObjectConstructorSchema([ |
| 182 | + 'field1' => new StringSchema(), |
| 183 | + 'field2' => new IntSchema(), |
| 184 | + 'field3' => new FloatSchema(), |
| 185 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 186 | + |
| 187 | + try { |
| 188 | + $schema->parse($input); |
| 189 | + |
| 190 | + throw new \Exception('code should not be reached'); |
| 191 | + } catch (ErrorsException $errorsException) { |
| 192 | + self::assertSame([ |
| 193 | + [ |
| 194 | + 'path' => 'field3', |
| 195 | + 'error' => [ |
| 196 | + 'code' => 'float.type', |
| 197 | + 'template' => 'Type should be "float", {{given}} given', |
| 198 | + 'variables' => [ |
| 199 | + 'given' => 'string', |
| 200 | + ], |
| 201 | + ], |
| 202 | + ], |
| 203 | + ], $errorsException->errors->jsonSerialize()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + public function testFailedWithInvalidValueNotCatchedByFieldSchema(): void |
| 208 | + { |
| 209 | + $input = ['field1' => 'test', 'field2' => 5, 'field3' => 'test']; |
| 210 | + |
| 211 | + $schema = new ObjectConstructorSchema([ |
| 212 | + 'field1' => new StringSchema(), |
| 213 | + 'field2' => new IntSchema(), |
| 214 | + 'field3' => new StringSchema(), |
| 215 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 216 | + |
| 217 | + try { |
| 218 | + $schema->parse($input); |
| 219 | + |
| 220 | + throw new \Exception('code should not be reached'); |
| 221 | + } catch (ErrorsException $errorsException) { |
| 222 | + self::assertSame([ |
| 223 | + [ |
| 224 | + 'path' => '', |
| 225 | + 'error' => [ |
| 226 | + 'code' => 'object.parameterType', |
| 227 | + 'template' => 'Parameter {{index}} {{name}} should be of {{type}}, {{given}} given', |
| 228 | + 'variables' => [ |
| 229 | + 'index' => '3', |
| 230 | + 'name' => '$field3', |
| 231 | + 'type' => '?float', |
| 232 | + 'given' => 'string', |
| 233 | + ], |
| 234 | + ], |
| 235 | + ], |
| 236 | + ], $errorsException->errors->jsonSerialize()); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + public function testFailedWithUnknownException(): void |
| 241 | + { |
| 242 | + $exception = new \Exception('unknown'); |
| 243 | + |
| 244 | + $input = ['field1' => 'test', 'field2' => 5, 'field3' => 3.14159]; |
| 245 | + |
| 246 | + $schema = new ObjectConstructorSchema([ |
| 247 | + 'field1' => new StringSchema(), |
| 248 | + 'field2' => new IntSchema(), |
| 249 | + 'field3' => new FloatSchema()->postParse(static fn () => throw $exception), |
| 250 | + ], ObjectConstructorDemo::class)->optional(['field3']); |
| 251 | + |
| 252 | + try { |
| 253 | + $schema->parse($input); |
| 254 | + |
| 255 | + throw new \Exception('code should not be reached'); |
| 256 | + } catch (\Exception $e) { |
| 257 | + self::assertSame($exception, $e); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + public function testFailedWithTypeErrorNotMatchingPattern(): void |
| 262 | + { |
| 263 | + $input = ['field1' => 'test']; |
| 264 | + |
| 265 | + $schema = new ObjectConstructorSchema([ |
| 266 | + 'field1' => new StringSchema(), |
| 267 | + ], ObjectConstructorThrowingTypeErrorDemo::class); |
| 268 | + |
| 269 | + try { |
| 270 | + $schema->parse($input); |
| 271 | + |
| 272 | + throw new \Exception('code should not be reached'); |
| 273 | + } catch (\TypeError $e) { |
| 274 | + self::assertSame('some unrelated type error', $e->getMessage()); |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments