|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of event-engine/php-json-schema. |
| 4 | + * (c) 2018-2019 prooph software GmbH <contact@prooph.de> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace EventEngineTest\JsonSchema; |
| 13 | + |
| 14 | +use EventEngine\JsonSchema\Exception\JsonValidationError; |
| 15 | +use EventEngine\JsonSchema\Exception\JustinRainbowJsonValidationError; |
| 16 | +use EventEngine\JsonSchema\JustinRainbowJsonSchema; |
| 17 | + |
| 18 | +class JustinRainbowJsonSchemaTest extends BasicTestCase |
| 19 | +{ |
| 20 | + private function schema(): array |
| 21 | + { |
| 22 | + $schema = <<<'JSON' |
| 23 | +{ |
| 24 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 25 | + "type": "object", |
| 26 | + "properties": { |
| 27 | + "name": { |
| 28 | + "type": "string", |
| 29 | + "minLength": 3 |
| 30 | + }, |
| 31 | + "hasValue": {"type": "boolean"}, |
| 32 | + "age": {"type": "number"}, |
| 33 | + "subObject": { |
| 34 | + "type": "object", |
| 35 | + "properties": { |
| 36 | + "p1": { |
| 37 | + "type": "string", |
| 38 | + "minLength": 3 |
| 39 | + }, |
| 40 | + "p2": {"type": "boolean"} |
| 41 | + }, |
| 42 | + "required": ["p1", "p2"], |
| 43 | + "additionalProperties": false |
| 44 | + }, |
| 45 | + "type": { |
| 46 | + "enum": ["Foo", "Bar", "Baz"] |
| 47 | + } |
| 48 | + }, |
| 49 | + "required": ["name", "hasValue", "age", "type"], |
| 50 | + "additionalProperties": false |
| 51 | +} |
| 52 | +JSON; |
| 53 | + return json_decode($schema, true); |
| 54 | + } |
| 55 | + |
| 56 | + private function validData(): array |
| 57 | + { |
| 58 | + return [ |
| 59 | + 'name' => 'Tester', |
| 60 | + 'hasValue' => true, |
| 61 | + 'age' => 40, |
| 62 | + 'type' => 'Bar', |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @test |
| 68 | + */ |
| 69 | + public function it_validates_json_schema(): void |
| 70 | + { |
| 71 | + $data = $this->validData(); |
| 72 | + |
| 73 | + $cut = new JustinRainbowJsonSchema(); |
| 74 | + $cut->assert('myObject', $data, $this->schema()); |
| 75 | + $this->assertTrue(true); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @test |
| 80 | + */ |
| 81 | + public function it_throws_json_validation_error_exception(): void |
| 82 | + { |
| 83 | + $data = $this->validData(); |
| 84 | + $data['unknown'] = 'set'; |
| 85 | + |
| 86 | + $expectedMessage = <<<'Msg' |
| 87 | +Validation of "myObject" failed: |
| 88 | +[additionalProp] The property unknown is not defined and the definition does not allow additional properties |
| 89 | +Msg; |
| 90 | + |
| 91 | + $cut = new JustinRainbowJsonSchema(); |
| 92 | + try { |
| 93 | + $cut->assert('myObject', $data, $this->schema()); |
| 94 | + } catch (JsonValidationError $e) { |
| 95 | + $this->assertSame(400, $e->getCode()); |
| 96 | + $this->assertStringStartsWith($expectedMessage, $e->getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @test |
| 102 | + */ |
| 103 | + public function it_throws_justin_rainbow_json_validation_error_exception(): void |
| 104 | + { |
| 105 | + $data = $this->validData(); |
| 106 | + $data['subObject']['unknown'] = 'set'; |
| 107 | + |
| 108 | + $expectedMessage = <<<'Msg' |
| 109 | +Validation of "myObject" failed: |
| 110 | +field "subObject.p1" [required] The property p1 is required |
| 111 | +field "subObject.p2" [required] The property p2 is required |
| 112 | +field "subObject" [additionalProp] The property unknown is not defined and the definition does not allow additional properties |
| 113 | +Msg; |
| 114 | + |
| 115 | + |
| 116 | + $cut = new JustinRainbowJsonSchema(); |
| 117 | + try { |
| 118 | + $cut->assert('myObject', $data, $this->schema()); |
| 119 | + } catch (JustinRainbowJsonValidationError $e) { |
| 120 | + $this->assertSame(400, $e->getCode()); |
| 121 | + $this->assertCount(3, $e->errors()); |
| 122 | + $this->assertStringStartsWith($expectedMessage, $e->getMessage()); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments