1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FlexPHP\Schema\Tests\Domain\Validations; |
4
|
|
|
|
5
|
|
|
use FlexPHP\Schema\Constants\Keyword; |
6
|
|
|
use FlexPHP\Schema\Exception\AttributeValidationException; |
7
|
|
|
use FlexPHP\Schema\Validations\AttributeValidation; |
8
|
|
|
use FlexPHP\Schema\Validators\PropertyDataTypeValidator; |
9
|
|
|
use FlexPHP\Schema\Validators\PropertyTypeValidator; |
10
|
|
|
use FlexPHP\Schema\Tests\TestCase; |
11
|
|
|
|
12
|
|
|
class AttributeValidationTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testItPropertyUnknowThrownException(): void |
15
|
|
|
{ |
16
|
|
|
$this->expectException(AttributeValidationException::class); |
17
|
|
|
$this->expectExceptionMessage('unknow'); |
18
|
|
|
|
19
|
|
|
$validation = new AttributeValidation([ |
20
|
|
|
'UnknowProperty' => 'Test', |
21
|
|
|
]); |
22
|
|
|
|
23
|
|
|
$validation->validate(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider propertyNameNotValid |
28
|
|
|
*/ |
29
|
|
|
public function testItPropertyNameNotValidThrownException($name): void |
30
|
|
|
{ |
31
|
|
|
$this->expectException(AttributeValidationException::class); |
32
|
|
|
$this->expectExceptionMessage('Name:'); |
33
|
|
|
|
34
|
|
|
$validation = new AttributeValidation([ |
35
|
|
|
Keyword::NAME => $name, |
36
|
|
|
Keyword::DATATYPE => 'string', |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$validation->validate(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider propertyNameValid |
44
|
|
|
*/ |
45
|
|
|
public function testItPropertyNameOk($name): void |
46
|
|
|
{ |
47
|
|
|
$validation = new AttributeValidation([ |
48
|
|
|
Keyword::NAME => $name, |
49
|
|
|
Keyword::DATATYPE => 'string', |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$validation->validate(); |
53
|
|
|
|
54
|
|
|
$this->assertTrue(true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider propertyDataTypeNotValid |
59
|
|
|
*/ |
60
|
|
|
public function testItPropertyDataTypeNotValidThrownException($dataType): void |
61
|
|
|
{ |
62
|
|
|
$this->expectException(AttributeValidationException::class); |
63
|
|
|
$this->expectExceptionMessage('DataType:'); |
64
|
|
|
|
65
|
|
|
$validation = new AttributeValidation([ |
66
|
|
|
Keyword::NAME => 'foo', |
67
|
|
|
Keyword::DATATYPE => $dataType, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$validation->validate(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider propertyDataTypeValid |
75
|
|
|
*/ |
76
|
|
|
public function testItPropertyDataTypeOk($dataType): void |
77
|
|
|
{ |
78
|
|
|
$validation = new AttributeValidation([ |
79
|
|
|
Keyword::NAME => 'foo', |
80
|
|
|
Keyword::DATATYPE => $dataType, |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$validation->validate(); |
84
|
|
|
|
85
|
|
|
$this->assertTrue(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @dataProvider propertyDataTypeNotValid |
90
|
|
|
*/ |
91
|
|
|
public function testItPropertyTypeNotValidThrownException($type): void |
92
|
|
|
{ |
93
|
|
|
$this->expectException(AttributeValidationException::class); |
94
|
|
|
$this->expectExceptionMessage('Type:'); |
95
|
|
|
|
96
|
|
|
$validation = new AttributeValidation([ |
97
|
|
|
Keyword::NAME => 'foo', |
98
|
|
|
Keyword::DATATYPE => 'string', |
99
|
|
|
Keyword::TYPE => $type, |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
$validation->validate(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @dataProvider propertyTypeValid |
107
|
|
|
*/ |
108
|
|
|
public function testItPropertyTypeOk($type): void |
109
|
|
|
{ |
110
|
|
|
$validation = new AttributeValidation([ |
111
|
|
|
Keyword::NAME => 'foo', |
112
|
|
|
Keyword::DATATYPE => 'string', |
113
|
|
|
Keyword::TYPE => $type, |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
$validation->validate(); |
117
|
|
|
|
118
|
|
|
$this->assertTrue(true); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @dataProvider propertyConstraintsNotValid |
123
|
|
|
*/ |
124
|
|
|
public function testItPropertyConstraintsNotValidThrownException($constraints): void |
125
|
|
|
{ |
126
|
|
|
$this->expectException(AttributeValidationException::class); |
127
|
|
|
$this->expectExceptionMessage('Constraints:'); |
128
|
|
|
|
129
|
|
|
$validation = new AttributeValidation([ |
130
|
|
|
Keyword::NAME => 'foo', |
131
|
|
|
Keyword::DATATYPE => 'string', |
132
|
|
|
Keyword::CONSTRAINTS => $constraints, |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
$validation->validate(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @dataProvider propertyConstraintsValid |
140
|
|
|
*/ |
141
|
|
|
public function testItPropertyConstraintsOk($constraints): void |
142
|
|
|
{ |
143
|
|
|
$validation = new AttributeValidation([ |
144
|
|
|
Keyword::NAME => 'foo', |
145
|
|
|
Keyword::DATATYPE => 'string', |
146
|
|
|
Keyword::CONSTRAINTS => $constraints, |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
$validation->validate(); |
150
|
|
|
|
151
|
|
|
$this->assertTrue(true); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function propertyNameNotValid(): array |
155
|
|
|
{ |
156
|
|
|
return [ |
157
|
|
|
['#Name'], |
158
|
|
|
['1Name'], |
159
|
|
|
['Name$'], |
160
|
|
|
[str_repeat('N', 65)], |
161
|
|
|
[''], |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function propertyNameValid(): array |
166
|
|
|
{ |
167
|
|
|
return [ |
168
|
|
|
['Name'], |
169
|
|
|
['N123'], |
170
|
|
|
['Name_Test'], |
171
|
|
|
['name_test'], |
172
|
|
|
['_name'], |
173
|
|
|
[str_repeat('N', 64)], |
174
|
|
|
['N'], |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function propertyDataTypeNotValid(): array |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
['unknow'], |
182
|
|
|
['bool'], |
183
|
|
|
['barchar'], |
184
|
|
|
['interger'], |
185
|
|
|
['int'], |
186
|
|
|
[null], |
187
|
|
|
[[]], |
188
|
|
|
[1], |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function propertyDataTypeValid(): array |
193
|
|
|
{ |
194
|
|
|
return array_map(function ($dataType) { |
195
|
|
|
return [$dataType]; |
196
|
|
|
}, PropertyDataTypeValidator::ALLOWED_DATATYPES); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function propertyTypeNotValid(): array |
200
|
|
|
{ |
201
|
|
|
return [ |
202
|
|
|
['unknow'], |
203
|
|
|
['textt'], |
204
|
|
|
['text area'], |
205
|
|
|
['int'], |
206
|
|
|
[null], |
207
|
|
|
[[]], |
208
|
|
|
[1], |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function propertyTypeValid(): array |
213
|
|
|
{ |
214
|
|
|
return array_map(function ($dataType) { |
215
|
|
|
return [$dataType]; |
216
|
|
|
}, PropertyTypeValidator::ALLOWED_TYPES); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function propertyConstraintsNotValid(): array |
220
|
|
|
{ |
221
|
|
|
return [ |
222
|
|
|
['_REQUIRED'], |
223
|
|
|
['REQUIRED'], |
224
|
|
|
['Required'], |
225
|
|
|
[1], |
226
|
|
|
[['minlength' => null]], |
227
|
|
|
[['maxlength' => []]], |
228
|
|
|
[['mincheck' => -1]], |
229
|
|
|
[['maxcheck' => 0]], |
230
|
|
|
[['min' => '']], |
231
|
|
|
[['max' => 'null']], |
232
|
|
|
[['equalto' => null]], |
233
|
|
|
[['type' => 'unknow']], |
234
|
|
|
[['check' => [ |
235
|
|
|
'min' => rand(5, 10), |
236
|
|
|
]]], |
237
|
|
|
[['check' => [ |
238
|
|
|
'min' => rand(5, 10), |
239
|
|
|
'max' => rand(0, 4), |
240
|
|
|
]]], |
241
|
|
|
[['length' => [ |
242
|
|
|
'max' => rand(0, 5), |
243
|
|
|
]]], |
244
|
|
|
[['length' => [ |
245
|
|
|
'min' => rand(5, 10), |
246
|
|
|
'max' => rand(0, 5), |
247
|
|
|
]]], |
248
|
|
|
[['length' => [ |
249
|
|
|
'min' => rand(5, 10), |
250
|
|
|
], 'type' => 'text']], |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function propertyConstraintsValid(): array |
255
|
|
|
{ |
256
|
|
|
return [ |
257
|
|
|
[null], |
258
|
|
|
[''], |
259
|
|
|
[[]], |
260
|
|
|
['required'], |
261
|
|
|
['required|min:8'], // Using | |
262
|
|
|
["['required']"], // Array syntax |
263
|
|
|
["['required','min'=>8]"], // Array syntax multiple |
264
|
|
|
['["required"]'], // JSON simple |
265
|
|
|
['{"required":true}'], // JSON complex |
266
|
|
|
['{"required":true,"min":8}'], // JSON complex multiple |
267
|
|
|
[['required']], |
268
|
|
|
[['required' => true]], |
269
|
|
|
[['required' => false]], |
270
|
|
|
[['minlength' => 0]], |
271
|
|
|
[['minlength' => rand(0, 9)]], |
272
|
|
|
[['maxlength' => rand(1, 9)]], |
273
|
|
|
[['mincheck' => 0]], |
274
|
|
|
[['mincheck' => rand(1, 9)]], |
275
|
|
|
[['maxcheck' => rand(1, 9)]], |
276
|
|
|
[['min' => 0]], |
277
|
|
|
[['min' => rand(1, 9)]], |
278
|
|
|
[['max' => rand(1, 9)]], |
279
|
|
|
[['equalto' => 'foo']], |
280
|
|
|
[['type' => 'text']], |
281
|
|
|
[['check' => [ |
282
|
|
|
'min' => rand(0, 4), |
283
|
|
|
'max' => rand(5, 10), |
284
|
|
|
]]], |
285
|
|
|
[['length' => [ |
286
|
|
|
'min' => rand(0, 4), |
287
|
|
|
'max' => rand(5, 10), |
288
|
|
|
]]], |
289
|
|
|
]; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|