forked from jsonrainbow/json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoerciveTest.php
More file actions
247 lines (217 loc) · 10.2 KB
/
CoerciveTest.php
File metadata and controls
247 lines (217 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema\Tests\Constraints;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\TypeCheck\LooseTypeCheck;
use JsonSchema\Validator;
class CoerciveTest extends VeryBaseTestCase
{
protected $factory = null;
public function setUp()
{
$this->factory = new Factory();
$this->factory->setConfig(Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE_TYPES);
}
public function dataCoerceCases()
{
// check type conversions
$types = array(
// toType
'string' => array(
// fromType fromValue toValue valid Test Number
array('string', '"ABC"', 'ABC', true), // #0
array('integer', '45', '45', true), // #1
array('boolean', 'true', 'true', true), // #2
array('boolean', 'false', 'false', true), // #3
array('NULL', 'null', '', true), // #4
array('array', '[45]', '45', true), // #5
array('object', '{"a":"b"}', null, false), // #6
array('array', '[{"a":"b"}]', null, false), // #7
),
'integer' => array(
array('string', '"45"', 45, true), // #8
array('integer', '45', 45, true), // #9
array('boolean', 'true', 1, true), // #10
array('boolean', 'false', 0, true), // #11
array('NULL', 'null', 0, true), // #12
array('array', '["-45"]', -45, true), // #13
array('object', '{"a":"b"}', null, false), // #14
array('array', '["ABC"]', null, false), // #15
),
'boolean' => array(
array('string', '"true"', true, true), // #16
array('integer', '1', true, true), // #17
array('boolean', 'true', true, true), // #18
array('NULL', 'null', false, true), // #19
array('array', '["true"]', true, true), // #20
array('object', '{"a":"b"}', null, false), // #21
array('string', '""', null, false), // #22
array('string', '"ABC"', null, false), // #23
array('integer', '2', null, false), // #24
),
'NULL' => array(
array('string', '""', null, true), // #25
array('integer', '0', null, true), // #26
array('boolean', 'false', null, true), // #27
array('NULL', 'null', null, true), // #28
array('array', '[0]', null, true), // #29
array('object', '{"a":"b"}', null, false), // #30
array('string', '"null"', null, false), // #31
array('integer', '-1', null, false), // #32
),
'array' => array(
array('string', '"ABC"', array('ABC'), true), // #33
array('integer', '45', array(45), true), // #34
array('boolean', 'true', array(true), true), // #35
array('NULL', 'null', array(null), true), // #36
array('array', '["ABC"]', array('ABC'), true), // #37
array('object', '{"a":"b"}', null, false), // #38
),
);
// #39 check post-coercion validation (to array)
$tests[] = array(
'{"properties":{"propertyOne":{"type":"array","items":[{"type":"number"}]}}}',
'{"propertyOne":"ABC"}',
'string', null, null, false
);
// #40 check multiple types (first valid)
$tests[] = array(
'{"properties":{"propertyOne":{"type":["number", "string"]}}}',
'{"propertyOne":42}',
'integer', 'integer', 42, true
);
// #41 check multiple types (last valid)
$tests[] = array(
'{"properties":{"propertyOne":{"type":["number", "string"]}}}',
'{"propertyOne":"42"}',
'string', 'string', '42', true
);
// #42 check the meaning of life
$tests[] = array(
'{"properties":{"propertyOne":{"type":"any"}}}',
'{"propertyOne":"42"}',
'string', 'string', '42', true
);
// #43 check turple coercion
$tests[] = array(
'{"properties":{"propertyOne":{"type":"array","items":[{"type":"number"},{"type":"string"}]}}}',
'{"propertyOne":["42", 42]}',
'array', 'array', array(42, '42'), true
);
// #44 check early coercion
$tests[] = array(
'{"properties":{"propertyOne":{"type":["object", "number", "string"]}}}',
'{"propertyOne":"42"}',
'string', 'integer', 42, true, Constraint::CHECK_MODE_EARLY_COERCE
);
// #45 check multiple types (none valid)
$tests[] = array(
'{"properties":{"propertyOne":{"type":["number", "boolean"]}}}',
'{"propertyOne":"42"}',
'string', 'integer', 42, true
);
// #46 check coercion with "const"
$tests[] = array(
'{"properties":{"propertyOne":{"type":"string","const":"42"}}}',
'{"propertyOne":42}',
'integer', 'string', '42', true
);
// #47 check coercion with "const"
$tests[] = array(
'{"properties":{"propertyOne":{"type":"number","const":42}}}',
'{"propertyOne":"42"}',
'string', 'integer', 42, true
);
// #48 check boolean coercion with "const"
$tests[] = array(
'{"properties":{"propertyOne":{"type":"boolean","const":false}}}',
'{"propertyOne":"false"}',
'string', 'boolean', false, true
);
// #49 check boolean coercion with "const"
$tests[] = array(
'{"properties":{"propertyOne":{"type":"boolean","const":true}}}',
'{"propertyOne":"true"}',
'string', 'boolean', true, true
);
// #50 check boolean coercion with "const"
$tests[] = array(
'{"properties":{"propertyOne":{"type":"boolean","const":true}}}',
'{"propertyOne":1}',
'integer', 'boolean', true, true
);
// #51 check boolean coercion with "const"
$tests[] = array(
'{"properties":{"propertyOne":{"type":"boolean","const":false}}}',
'{"propertyOne":"false"}',
'string', 'boolean', false, true
);
foreach ($types as $toType => $testCases) {
foreach ($testCases as $testCase) {
$tests[] = array(
sprintf('{"properties":{"propertyOne":{"type":"%s"}}}', strtolower($toType)),
sprintf('{"propertyOne":%s}', $testCase[1]),
$testCase[0],
$toType,
$testCase[2],
$testCase[3]
);
}
}
return $tests;
}
/** @dataProvider dataCoerceCases **/
public function testCoerceCases($schema, $data, $startType, $endType, $endValue, $valid, $extraFlags = 0, $assoc = false)
{
$validator = new Validator($this->factory);
$schema = json_decode($schema);
$data = json_decode($data, $assoc);
// check initial type
$type = gettype(LooseTypeCheck::propertyGet($data, 'propertyOne'));
if ($assoc && $type == 'array' && $startType == 'object') {
$type = 'object';
}
$this->assertEquals($startType, $type, "Incorrect type '$type': expected '$startType'");
$validator->validate($data, $schema, $this->factory->getConfig() | $extraFlags);
// check validity
if ($valid) {
$prettyPrint = defined('\JSON_PRETTY_PRINT') ? constant('\JSON_PRETTY_PRINT') : 0;
$this->assertTrue(
$validator->isValid(),
'Validation failed: ' . json_encode($validator->getErrors(), $prettyPrint)
);
// check end type
$type = gettype(LooseTypeCheck::propertyGet($data, 'propertyOne'));
$this->assertEquals($endType, $type, "Incorrect type '$type': expected '$endType'");
// check end value
$value = LooseTypeCheck::propertyGet($data, 'propertyOne');
$this->assertSame($value, $endValue, sprintf(
"Incorrect value '%s': expected '%s'",
is_scalar($value) ? $value : gettype($value),
is_scalar($endValue) ? $endValue : gettype($endValue)
));
} else {
$this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed');
$this->assertCount(1, $validator->getErrors());
}
}
/** @dataProvider dataCoerceCases **/
public function testCoerceCasesUsingAssoc($schema, $data, $startType, $endType, $endValue, $valid, $early = false)
{
$this->testCoerceCases($schema, $data, $startType, $endType, $endValue, $valid, $early, true);
}
public function testCoerceAPI()
{
$input = json_decode('{"propertyOne": "10"}');
$schema = json_decode('{"properties":{"propertyOne":{"type":"number"}}}');
$v = new Validator();
$v->coerce($input, $schema);
$this->assertEquals('{"propertyOne":10}', json_encode($input));
}
}