|
6 | 6 |
|
7 | 7 | namespace Tests\Feature; |
8 | 8 |
|
| 9 | +use Carsdotcom\JsonSchemaValidation\Exceptions\JsonSchemaValidationException; |
9 | 10 | use Carsdotcom\JsonSchemaValidation\SchemaValidatorService; |
10 | 11 | use Illuminate\Support\Facades\Config; |
11 | 12 | use Opis\JsonSchema\Exceptions\UnresolvedReferenceException; |
@@ -87,4 +88,38 @@ public function testRegisterRawSchema(): void |
87 | 88 | self::assertStringStartsWith(Config::get('json-schema.base_url'), $absoluteRaw); |
88 | 89 | self::assertTrue($validator->validate($vins, $absoluteRaw)); |
89 | 90 | } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dataProvider provideValidateEncodedStringOrThrow |
| 94 | + */ |
| 95 | + public function testValidateEncodedStringOrThrow(string $encodedData, mixed $schema, bool $expectedSuccess): void |
| 96 | + { |
| 97 | + $validator = new SchemaValidatorService(); |
| 98 | + |
| 99 | + try { |
| 100 | + self::assertTrue($validator->validateEncodedStringOrThrow($encodedData, $schema)); |
| 101 | + if (!$expectedSuccess) { |
| 102 | + self::fail("Should have thrown JsonSchemaValidationException"); |
| 103 | + } |
| 104 | + } catch (JsonSchemaValidationException $e) { |
| 105 | + if ($expectedSuccess) { |
| 106 | + self::assertTrue(false, "Expected success, instead got " . $e->errorsAsMultilineString()); |
| 107 | + } else { |
| 108 | + self::addToAssertionCount(1); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public function provideValidateEncodedStringOrThrow(): array |
| 114 | + { |
| 115 | + return [ |
| 116 | + 'primitive, string schema' => ['420', '{"type": "number", "minimum": 69}', true], |
| 117 | + 'primitive, string schema fails' => ['42', '{"type": "number", "minimum": 69}', false], |
| 118 | + 'empty object is still an object' => ['{}', '{"type": "object"}', true], |
| 119 | + 'empty object is not an array' => ['{}', '{"type": "array"}', false], |
| 120 | + 'typical complex object, success' => ['{"a":1}', '{"type":"object","properties":{"a":{"type":"number"}}, "required":["a"]}', true], |
| 121 | + 'typical complex object, failure' => ['{"b":1}', '{"type":"object","properties":{"a":{"type":"number"}}, "required":["a"]}', false], |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
90 | 125 | } |
0 commit comments