|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Swaggest\JsonSchema\Tests\PHPUnit; |
| 4 | + |
| 5 | +use Swaggest\JsonSchema\Context; |
| 6 | +use Swaggest\JsonSchema\InvalidValue; |
| 7 | +use Swaggest\JsonSchema\Schema; |
| 8 | + |
| 9 | +class TolerateStringsTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + public function testWithTolerateStrings() |
| 12 | + { |
| 13 | + $json_schema = json_decode( |
| 14 | + json_encode( |
| 15 | + [ |
| 16 | + 'items' => [ |
| 17 | + ['type' => 'string'], |
| 18 | + ['type' => 'integer'], |
| 19 | + ['type' => 'number'], |
| 20 | + ['type' => 'boolean'], |
| 21 | + [ |
| 22 | + 'type' => 'integer', |
| 23 | + 'enum' => [1, 2, 3] |
| 24 | + ] |
| 25 | + ] |
| 26 | + ] |
| 27 | + ) |
| 28 | + ); |
| 29 | + |
| 30 | + $data = [ |
| 31 | + 'text', |
| 32 | + '10', |
| 33 | + '123.45', |
| 34 | + 'true', |
| 35 | + '2' |
| 36 | + ]; |
| 37 | + |
| 38 | + $options = new Context(); |
| 39 | + $options->tolerateStrings = true; |
| 40 | + |
| 41 | + $schema = Schema::import($json_schema); |
| 42 | + |
| 43 | + $this->assertSame($data, $schema->in($data, $options)); |
| 44 | + } |
| 45 | + |
| 46 | + public function testWithTolerateStringsBadNumber() |
| 47 | + { |
| 48 | + $json_schema = json_decode( |
| 49 | + json_encode( |
| 50 | + [ |
| 51 | + 'items' => [ |
| 52 | + ['type' => 'string'], |
| 53 | + ['type' => 'integer'], |
| 54 | + ['type' => 'number'], |
| 55 | + ['type' => 'boolean'] |
| 56 | + ] |
| 57 | + ] |
| 58 | + ) |
| 59 | + ); |
| 60 | + |
| 61 | + $data = [ |
| 62 | + 'text', |
| 63 | + '10', |
| 64 | + 'bad', |
| 65 | + 'true' |
| 66 | + ]; |
| 67 | + |
| 68 | + $options = new Context(); |
| 69 | + $options->tolerateStrings = true; |
| 70 | + |
| 71 | + $schema = Schema::import($json_schema); |
| 72 | + |
| 73 | + $this->setExpectedException(get_class(new InvalidValue()), 'Number expected, "bad" received at #->items:2'); |
| 74 | + $schema->in($data, $options); |
| 75 | + } |
| 76 | + |
| 77 | + public function testWithTolerateStringsBadInteger() |
| 78 | + { |
| 79 | + $json_schema = json_decode( |
| 80 | + json_encode( |
| 81 | + [ |
| 82 | + 'items' => [ |
| 83 | + ['type' => 'string'], |
| 84 | + ['type' => 'integer'], |
| 85 | + ['type' => 'number'], |
| 86 | + ['type' => 'boolean'] |
| 87 | + ] |
| 88 | + ] |
| 89 | + ) |
| 90 | + ); |
| 91 | + |
| 92 | + $data = [ |
| 93 | + 'text', |
| 94 | + 'bad', |
| 95 | + '123.45', |
| 96 | + 'true' |
| 97 | + ]; |
| 98 | + |
| 99 | + $options = new Context(); |
| 100 | + $options->tolerateStrings = true; |
| 101 | + |
| 102 | + $schema = Schema::import($json_schema); |
| 103 | + |
| 104 | + $this->setExpectedException(get_class(new InvalidValue()), 'Integer expected, "bad" received at #->items:1'); |
| 105 | + $schema->in($data, $options); |
| 106 | + } |
| 107 | + |
| 108 | + public function testWithTolerateStringsBadBoolean() |
| 109 | + { |
| 110 | + $json_schema = json_decode( |
| 111 | + json_encode( |
| 112 | + [ |
| 113 | + 'items' => [ |
| 114 | + ['type' => 'string'], |
| 115 | + ['type' => 'integer'], |
| 116 | + ['type' => 'number'], |
| 117 | + ['type' => 'boolean'] |
| 118 | + ] |
| 119 | + ] |
| 120 | + ) |
| 121 | + ); |
| 122 | + |
| 123 | + $data = [ |
| 124 | + 'text', |
| 125 | + '10', |
| 126 | + '123.45', |
| 127 | + 'bad' |
| 128 | + ]; |
| 129 | + |
| 130 | + $options = new Context(); |
| 131 | + $options->tolerateStrings = true; |
| 132 | + |
| 133 | + $schema = Schema::import($json_schema); |
| 134 | + |
| 135 | + $this->setExpectedException(get_class(new InvalidValue()), 'Boolean expected, "bad" received at #->items:3'); |
| 136 | + $schema->in($data, $options); |
| 137 | + } |
| 138 | + |
| 139 | + public function testWithTolerateStringsBadEnum() |
| 140 | + { |
| 141 | + $json_schema = json_decode( |
| 142 | + json_encode( |
| 143 | + [ |
| 144 | + 'items' => [ |
| 145 | + ['type' => 'string'], |
| 146 | + ['type' => 'integer'], |
| 147 | + ['type' => 'number'], |
| 148 | + ['type' => 'boolean'], |
| 149 | + [ |
| 150 | + 'type' => 'integer', |
| 151 | + 'enum' => [1, 2, 3] |
| 152 | + ] |
| 153 | + ] |
| 154 | + ] |
| 155 | + ) |
| 156 | + ); |
| 157 | + |
| 158 | + $data = [ |
| 159 | + 'text', |
| 160 | + '10', |
| 161 | + '123.45', |
| 162 | + 'true', |
| 163 | + '5' |
| 164 | + ]; |
| 165 | + |
| 166 | + $options = new Context(); |
| 167 | + $options->tolerateStrings = true; |
| 168 | + |
| 169 | + $schema = Schema::import($json_schema); |
| 170 | + |
| 171 | + $this->setExpectedException(get_class(new InvalidValue()), 'Enum failed, enum: [1,2,3], data: 5 at #->items:4'); |
| 172 | + $schema->in($data, $options); |
| 173 | + } |
| 174 | +} |
0 commit comments