1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Rule\Json; |
8
|
|
|
use Yiisoft\Validator\Rule\JsonHandler; |
9
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
10
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
14
|
|
|
|
15
|
|
|
final class JsonTest extends RuleTestCase |
16
|
|
|
{ |
17
|
|
|
use DifferentRuleInHandlerTestTrait; |
18
|
|
|
use RuleWithOptionsTestTrait; |
19
|
|
|
use SkipOnErrorTestTrait; |
20
|
|
|
use WhenTestTrait; |
21
|
|
|
|
22
|
|
|
public function testGetName(): void |
23
|
|
|
{ |
24
|
|
|
$rule = new Json(); |
25
|
|
|
$this->assertSame('json', $rule->getName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function dataOptions(): array |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
[ |
32
|
|
|
new Json(), |
33
|
|
|
[ |
34
|
|
|
'incorrectInputMessage' => [ |
35
|
|
|
'template' => 'The value must have a string type.', |
36
|
|
|
'parameters' => [], |
37
|
|
|
], |
38
|
|
|
'message' => [ |
39
|
|
|
'template' => 'The value is not JSON.', |
40
|
|
|
'parameters' => [], |
41
|
|
|
], |
42
|
|
|
'skipOnEmpty' => false, |
43
|
|
|
'skipOnError' => false, |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function dataValidationPassed(): array |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
// JSON test from http://www.json.org/JSON_checker/test/pass1.json |
53
|
|
|
[ |
54
|
|
|
<<<'JSON_WRAP' |
55
|
|
|
[ |
56
|
|
|
"JSON Test Pattern pass1", |
57
|
|
|
{"object with 1 member":["array with 1 element"]}, |
58
|
|
|
{}, |
59
|
|
|
[], |
60
|
|
|
-42, |
61
|
|
|
true, |
62
|
|
|
false, |
63
|
|
|
null, |
64
|
|
|
{ |
65
|
|
|
"integer": 1234567890, |
66
|
|
|
"real": -9876.543210, |
67
|
|
|
"e": 0.123456789e-12, |
68
|
|
|
"E": 1.234567890E+34, |
69
|
|
|
"": 23456789012E66, |
70
|
|
|
"zero": 0, |
71
|
|
|
"one": 1, |
72
|
|
|
"space": " ", |
73
|
|
|
"quote": "\"", |
74
|
|
|
"backslash": "\\", |
75
|
|
|
"controls": "\b\f\n\r\t", |
76
|
|
|
"slash": "/ & \/", |
77
|
|
|
"alpha": "abcdefghijklmnopqrstuvwyz", |
78
|
|
|
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", |
79
|
|
|
"digit": "0123456789", |
80
|
|
|
"0123456789": "digit", |
81
|
|
|
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?", |
82
|
|
|
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", |
83
|
|
|
"true": true, |
84
|
|
|
"false": false, |
85
|
|
|
"null": null, |
86
|
|
|
"array":[ ], |
87
|
|
|
"object":{ }, |
88
|
|
|
"address": "50 St. James Street", |
89
|
|
|
"url": "http://www.JSON.org/", |
90
|
|
|
"comment": "// /* <!-- --", |
91
|
|
|
"# -- --> */": " ", |
92
|
|
|
" s p a c e d " :[1,2 , 3 |
93
|
|
|
|
94
|
|
|
, |
95
|
|
|
|
96
|
|
|
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], |
97
|
|
|
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", |
98
|
|
|
"quotes": "" \u0022 %22 0x22 034 "", |
99
|
|
|
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" |
100
|
|
|
: "A key can be any string" |
101
|
|
|
}, |
102
|
|
|
0.5 ,98.6 |
103
|
|
|
, |
104
|
|
|
99.44 |
105
|
|
|
, |
106
|
|
|
|
107
|
|
|
1066, |
108
|
|
|
1e1, |
109
|
|
|
0.1e1, |
110
|
|
|
1e-1, |
111
|
|
|
1e00,2e+00,2e-00 |
112
|
|
|
,"rosebud"] |
113
|
|
|
JSON_WRAP |
114
|
|
|
, |
115
|
|
|
[new Json()], |
116
|
|
|
], |
117
|
|
|
// JSON test from http://www.json.org/JSON_checker/test/pass2.json |
118
|
|
|
['[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]', [new Json()]], |
119
|
|
|
// JSON test from http://www.json.org/JSON_checker/test/pass3.json |
120
|
|
|
[ |
121
|
|
|
<<<'JSON_WRAP' |
122
|
|
|
{ |
123
|
|
|
"JSON Test Pattern pass3": { |
124
|
|
|
"The outermost value": "must be an object or array.", |
125
|
|
|
"In this test": "It is an object." |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
JSON_WRAP |
129
|
|
|
, |
130
|
|
|
[new Json()], |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function dataValidationFailed(): array |
136
|
|
|
{ |
137
|
|
|
$incorrectInputErrors = ['' => ['The value must have a string type.']]; |
138
|
|
|
$errors = ['' => ['The value is not JSON.']]; |
139
|
|
|
|
140
|
|
|
return [ |
141
|
|
|
'incorrect input, array' => [['json'], [new Json()], $incorrectInputErrors], |
142
|
|
|
'incorrect input, integer' => [10, [new Json()], $incorrectInputErrors], |
143
|
|
|
'incorrect input, null' => [null, [new Json()], $incorrectInputErrors], |
144
|
|
|
'custom incorrect input message' => [ |
145
|
|
|
['json'], |
146
|
|
|
[new Json(incorrectInputMessage: 'Custom incorrect input message.')], |
147
|
|
|
['' => ['Custom incorrect input message.']], |
148
|
|
|
], |
149
|
|
|
'custom incorrect input message with parameters' => [ |
150
|
|
|
['json'], |
151
|
|
|
[new Json(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
152
|
|
|
['' => ['Attribute - , type - array.']], |
153
|
|
|
], |
154
|
|
|
'custom incorrect input message with parameters, attribute set' => [ |
155
|
|
|
['data' => ['json']], |
156
|
|
|
['data' => new Json(incorrectInputMessage: 'Attribute - {attribute}, type - {type}.')], |
157
|
|
|
['data' => ['Attribute - data, type - array.']], |
158
|
|
|
], |
159
|
|
|
|
160
|
|
|
['{"name": "tester"', [new Json()], $errors], |
161
|
|
|
['{"name": tester}', [new Json()], $errors], |
162
|
|
|
|
163
|
|
|
'custom message' => ['bad json', [new Json(message: 'Custom message.')], ['' => ['Custom message.']]], |
164
|
|
|
'custom message with parameters' => [ |
165
|
|
|
'bad json', |
166
|
|
|
[new Json(message: 'Attribute - {attribute}, value - {value}.')], |
167
|
|
|
['' => ['Attribute - , value - bad json.']], |
168
|
|
|
], |
169
|
|
|
'custom message with parameters, attribute set' => [ |
170
|
|
|
['data' => 'bad json'], |
171
|
|
|
['data' => new Json(message: 'Attribute - {attribute}, value - {value}.')], |
172
|
|
|
['data' => ['Attribute - data, value - bad json.']], |
173
|
|
|
], |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testSkipOnError(): void |
178
|
|
|
{ |
179
|
|
|
$this->testSkipOnErrorInternal(new Json(), new Json(skipOnError: true)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testWhen(): void |
183
|
|
|
{ |
184
|
|
|
$when = static fn (mixed $value): bool => $value !== null; |
185
|
|
|
$this->testWhenInternal(new Json(), new Json(when: $when)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
189
|
|
|
{ |
190
|
|
|
return [Json::class, JsonHandler::class]; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|