1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\Schema\Tests\Unit\Validations; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Schema\Constants\Keyword; |
13
|
|
|
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException; |
14
|
|
|
use FlexPHP\Schema\Tests\TestCase; |
15
|
|
|
use FlexPHP\Schema\Validations\SchemaAttributeValidation; |
16
|
|
|
use FlexPHP\Schema\Validators\PropertyDataTypeValidator; |
17
|
|
|
use FlexPHP\Schema\Validators\PropertyTypeValidator; |
18
|
|
|
|
19
|
|
|
class SchemaAttributeValidationTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testItPropertyRequireThrownException(): void |
22
|
|
|
{ |
23
|
|
|
$this->expectException(InvalidSchemaAttributeException::class); |
24
|
|
|
|
25
|
|
|
$validation = new SchemaAttributeValidation([ |
26
|
|
|
Keyword::NAME => 'Test', |
27
|
|
|
]); |
28
|
|
|
|
29
|
|
|
$validation->validate(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testItPropertyUnknowThrownException(): void |
33
|
|
|
{ |
34
|
|
|
$this->expectException(InvalidSchemaAttributeException::class); |
35
|
|
|
$this->expectExceptionMessage('unknow'); |
36
|
|
|
|
37
|
|
|
$validation = new SchemaAttributeValidation([ |
38
|
|
|
'UnknowProperty' => 'Test', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$validation->validate(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @dataProvider propertyNameNotValid |
46
|
|
|
*/ |
47
|
|
|
public function testItPropertyNameNotValidThrownException(string $name): void |
48
|
|
|
{ |
49
|
|
|
$this->expectException(InvalidSchemaAttributeException::class); |
50
|
|
|
$this->expectExceptionMessage('Name:'); |
51
|
|
|
|
52
|
|
|
$validation = new SchemaAttributeValidation([ |
53
|
|
|
Keyword::NAME => $name, |
54
|
|
|
Keyword::DATATYPE => 'string', |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$validation->validate(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider propertyNameValid |
62
|
|
|
*/ |
63
|
|
|
public function testItPropertyNameOk(string $name): void |
64
|
|
|
{ |
65
|
|
|
$validation = new SchemaAttributeValidation([ |
66
|
|
|
Keyword::NAME => $name, |
67
|
|
|
Keyword::DATATYPE => 'string', |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$validation->validate(); |
71
|
|
|
|
72
|
|
|
$this->assertTrue(true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider propertyDataTypeNotValid |
77
|
|
|
*/ |
78
|
|
|
public function testItPropertyDataTypeNotValidThrownException(string $dataType): void |
79
|
|
|
{ |
80
|
|
|
$this->expectException(InvalidSchemaAttributeException::class); |
81
|
|
|
$this->expectExceptionMessage('DataType:'); |
82
|
|
|
|
83
|
|
|
$validation = new SchemaAttributeValidation([ |
84
|
|
|
Keyword::NAME => 'foo', |
85
|
|
|
Keyword::DATATYPE => $dataType, |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$validation->validate(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider propertyDataTypeValid |
93
|
|
|
*/ |
94
|
|
|
public function testItPropertyDataTypeOk(string $dataType): void |
95
|
|
|
{ |
96
|
|
|
$validation = new SchemaAttributeValidation([ |
97
|
|
|
Keyword::NAME => 'foo', |
98
|
|
|
Keyword::DATATYPE => $dataType, |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$validation->validate(); |
102
|
|
|
|
103
|
|
|
$this->assertTrue(true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @dataProvider propertyDataTypeNotValid |
108
|
|
|
*/ |
109
|
|
|
public function testItPropertyTypeNotValidThrownException(string $type): void |
110
|
|
|
{ |
111
|
|
|
$this->expectException(InvalidSchemaAttributeException::class); |
112
|
|
|
$this->expectExceptionMessage('Type:'); |
113
|
|
|
|
114
|
|
|
$validation = new SchemaAttributeValidation([ |
115
|
|
|
Keyword::NAME => 'foo', |
116
|
|
|
Keyword::DATATYPE => 'string', |
117
|
|
|
Keyword::TYPE => $type, |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$validation->validate(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @dataProvider propertyTypeValid |
125
|
|
|
*/ |
126
|
|
|
public function testItPropertyTypeOk(string $type): void |
127
|
|
|
{ |
128
|
|
|
$validation = new SchemaAttributeValidation([ |
129
|
|
|
Keyword::NAME => 'foo', |
130
|
|
|
Keyword::DATATYPE => 'string', |
131
|
|
|
Keyword::TYPE => $type, |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
$validation->validate(); |
135
|
|
|
|
136
|
|
|
$this->assertTrue(true); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @dataProvider propertyConstraintsNotValid |
141
|
|
|
*/ |
142
|
|
|
public function testItPropertyConstraintsNotValidThrownException( |
143
|
|
|
array $constraints, |
144
|
|
|
string $datetype = 'string' |
145
|
|
|
): void { |
146
|
|
|
$this->expectException(InvalidSchemaAttributeException::class); |
147
|
|
|
$this->expectExceptionMessage('Constraints:'); |
148
|
|
|
|
149
|
|
|
$validation = new SchemaAttributeValidation([ |
150
|
|
|
Keyword::NAME => 'foo', |
151
|
|
|
Keyword::DATATYPE => $datetype, |
152
|
|
|
Keyword::CONSTRAINTS => $constraints, |
153
|
|
|
]); |
154
|
|
|
|
155
|
|
|
$validation->validate(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @dataProvider propertyConstraintsValid |
160
|
|
|
*/ |
161
|
|
|
public function testItPropertyConstraintsOk( |
162
|
|
|
array $constraints, |
163
|
|
|
string $datetype = 'string' |
164
|
|
|
): void { |
165
|
|
|
$validation = new SchemaAttributeValidation([ |
166
|
|
|
Keyword::NAME => 'foo', |
167
|
|
|
Keyword::DATATYPE => $datetype, |
168
|
|
|
Keyword::CONSTRAINTS => $constraints, |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
$validation->validate(); |
172
|
|
|
|
173
|
|
|
$this->assertTrue(true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function propertyNameNotValid(): array |
177
|
|
|
{ |
178
|
|
|
return [ |
179
|
|
|
[''], |
180
|
|
|
[' '], |
181
|
|
|
['_'], |
182
|
|
|
['_name'], |
183
|
|
|
['name_'], |
184
|
|
|
['1Name'], |
185
|
|
|
['$Name'], |
186
|
|
|
['Na$me'], |
187
|
|
|
['Name$'], |
188
|
|
|
[\str_repeat('N', 65)], |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function propertyNameValid(): array |
193
|
|
|
{ |
194
|
|
|
return [ |
195
|
|
|
['n'], |
196
|
|
|
['N'], |
197
|
|
|
['Name'], |
198
|
|
|
['N123'], |
199
|
|
|
['Name_Test'], |
200
|
|
|
['name_test'], |
201
|
|
|
[\str_repeat('N', 64)], |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function propertyDataTypeNotValid(): array |
206
|
|
|
{ |
207
|
|
|
return [ |
208
|
|
|
['unknow'], |
209
|
|
|
['barchar'], |
210
|
|
|
['interger'], |
211
|
|
|
['int'], |
212
|
|
|
]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function propertyDataTypeValid(): array |
216
|
|
|
{ |
217
|
|
|
return \array_map(function ($dataType) { |
218
|
|
|
return [$dataType]; |
219
|
|
|
}, PropertyDataTypeValidator::ALLOWED_DATATYPES); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function propertyTypeNotValid(): array |
223
|
|
|
{ |
224
|
|
|
return [ |
225
|
|
|
['unknow'], |
226
|
|
|
['textt'], |
227
|
|
|
['text area'], |
228
|
|
|
['int'], |
229
|
|
|
[null], |
230
|
|
|
[[]], |
231
|
|
|
[1], |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function propertyTypeValid(): array |
236
|
|
|
{ |
237
|
|
|
return \array_map(function ($dataType) { |
238
|
|
|
return [$dataType]; |
239
|
|
|
}, PropertyTypeValidator::ALLOWED_TYPES); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function propertyConstraintsNotValid(): array |
243
|
|
|
{ |
244
|
|
|
return [ |
245
|
|
|
[['']], |
246
|
|
|
[['required']], |
247
|
|
|
[['_REQUIRED']], |
248
|
|
|
[['REQUIRED']], |
249
|
|
|
[['Required']], |
250
|
|
|
[['required' => null]], |
251
|
|
|
[['required' => '']], |
252
|
|
|
[[1]], |
253
|
|
|
[['minlength' => null]], |
254
|
|
|
[['maxlength' => []]], |
255
|
|
|
[['mincheck' => -1]], |
256
|
|
|
[['maxcheck' => 0]], |
257
|
|
|
[['min' => null]], |
258
|
|
|
[['min' => '']], |
259
|
|
|
[['min' => 'notvalid']], |
260
|
|
|
[['min' => -1]], |
261
|
|
|
[['max' => null]], |
262
|
|
|
[['max' => '']], |
263
|
|
|
[['max' => 'notvalid']], |
264
|
|
|
[['max' => 0]], |
265
|
|
|
[['max' => -1]], |
266
|
|
|
[['equalto' => null]], |
267
|
|
|
[['type' => 'unknow']], |
268
|
|
|
[['check' => [ |
269
|
|
|
'min' => \rand(5, 10), |
270
|
|
|
]]], |
271
|
|
|
[['check' => [ |
272
|
|
|
'min' => \rand(5, 10), |
273
|
|
|
'max' => \rand(0, 4), |
274
|
|
|
]]], |
275
|
|
|
[['length' => [ |
276
|
|
|
'max' => \rand(0, 5), |
277
|
|
|
]]], |
278
|
|
|
[['length' => [ |
279
|
|
|
'min' => null, |
280
|
|
|
'max' => \rand(0, 5), |
281
|
|
|
]]], |
282
|
|
|
[['length' => [ |
283
|
|
|
'min' => \rand(5, 10), |
284
|
|
|
'max' => \rand(0, 4), |
285
|
|
|
]]], |
286
|
|
|
[['length' => [ |
287
|
|
|
'min' => \rand(0, 5), |
288
|
|
|
'max' => null, |
289
|
|
|
]]], |
290
|
|
|
[['length' => [ |
291
|
|
|
'min' => \rand(5, 10), |
292
|
|
|
]]], |
293
|
|
|
[['pk' => null]], |
294
|
|
|
[['pk' => '']], |
295
|
|
|
[['pk' => 'notvalid']], |
296
|
|
|
[['fk' => null]], |
297
|
|
|
[['fk' => '']], |
298
|
|
|
[['fk' => 'table.name']], |
299
|
|
|
[['fk' => 'table,name.id']], |
300
|
|
|
[['fk' => 'table,name,id.dot']], |
301
|
|
|
[['ai' => null]], |
302
|
|
|
[['ai' => '']], |
303
|
|
|
[['ai' => 'notvalid']], |
304
|
|
|
[['ca' => null]], |
305
|
|
|
[['ca' => '']], |
306
|
|
|
[['ua' => null]], |
307
|
|
|
[['ua' => '']], |
308
|
|
|
[['ua' => 'notvalid']], |
309
|
|
|
[['cb' => null]], |
310
|
|
|
[['cb' => '']], |
311
|
|
|
[['cb' => 'notvalid']], |
312
|
|
|
[['ub' => null]], |
313
|
|
|
[['ub' => '']], |
314
|
|
|
[['ub' => 'notvalid']], |
315
|
|
|
[['filter' => null]], |
316
|
|
|
[['filter' => '']], |
317
|
|
|
[['filter' => 'notvalid']], |
318
|
|
|
[['format' => null]], |
319
|
|
|
[['format' => '']], |
320
|
|
|
[['format' => 'notvalid']], |
321
|
|
|
[['trim' => null]], |
322
|
|
|
[['trim' => '']], |
323
|
|
|
[['trim' => 'notvalid']], |
324
|
|
|
[['fchars' => null]], |
325
|
|
|
[['fchars' => '']], |
326
|
|
|
[['fchars' => 'notvalid']], |
327
|
|
|
[['link']], |
328
|
|
|
[['show' => false]], |
329
|
|
|
[['show' => true]], |
330
|
|
|
[['show' => 'notvalid']], |
331
|
|
|
// [['show' => 'a,i']], |
332
|
|
|
[['hide' => false]], |
333
|
|
|
[['hide' => true]], |
334
|
|
|
[['hide' => 'notvalid']], |
335
|
|
|
// [['hide' => 'a,i']], |
336
|
|
|
]; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function propertyConstraintsValid(): array |
340
|
|
|
{ |
341
|
|
|
return [ |
342
|
|
|
[[]], |
343
|
|
|
[['required' => true]], |
344
|
|
|
[['required' => false]], |
345
|
|
|
[['required' => 'true']], |
346
|
|
|
[['required' => 'false']], |
347
|
|
|
[['minlength' => 0]], |
348
|
|
|
[['minlength' => \rand(0, 9)]], |
349
|
|
|
[['maxlength' => \rand(1, 9)]], |
350
|
|
|
[['mincheck' => 0]], |
351
|
|
|
[['mincheck' => \rand(1, 9)]], |
352
|
|
|
[['maxcheck' => \rand(1, 9)]], |
353
|
|
|
[['min' => 0]], |
354
|
|
|
[['min' => \rand(1, 9)]], |
355
|
|
|
[['max' => \rand(1, 9)]], |
356
|
|
|
[['equalto' => 'foo']], |
357
|
|
|
[['type' => 'text']], |
358
|
|
|
[['check' => [ |
359
|
|
|
'min' => \rand(0, 4), |
360
|
|
|
'max' => \rand(5, 10), |
361
|
|
|
]]], |
362
|
|
|
[['length' => [ |
363
|
|
|
'min' => \rand(0, 4), |
364
|
|
|
'max' => \rand(5, 10), |
365
|
|
|
]]], |
366
|
|
|
[['pk' => true]], |
367
|
|
|
[['pk' => false]], |
368
|
|
|
[['pk' => 'true']], |
369
|
|
|
[['pk' => 'false']], |
370
|
|
|
[['fk' => 'table']], |
371
|
|
|
[['fk' => 'table,name']], |
372
|
|
|
[['fk' => 'table,name,id']], |
373
|
|
|
[['ai' => true]], |
374
|
|
|
[['ai' => false]], |
375
|
|
|
[['ai' => 'true']], |
376
|
|
|
[['ai' => 'false']], |
377
|
|
|
[['ca' => true]], |
378
|
|
|
[['ca' => false]], |
379
|
|
|
[['ca' => 'true']], |
380
|
|
|
[['ca' => 'false']], |
381
|
|
|
[['ua' => true]], |
382
|
|
|
[['ua' => false]], |
383
|
|
|
[['ua' => 'true']], |
384
|
|
|
[['ua' => 'false']], |
385
|
|
|
[['cb' => true]], |
386
|
|
|
[['cb' => false]], |
387
|
|
|
[['cb' => 'true']], |
388
|
|
|
[['cb' => 'false']], |
389
|
|
|
[['ub' => true]], |
390
|
|
|
[['ub' => false]], |
391
|
|
|
[['ub' => 'true']], |
392
|
|
|
[['ub' => 'false']], |
393
|
|
|
[['filter' => 'eq']], |
394
|
|
|
[['filter' => 'ne']], |
395
|
|
|
[['filter' => 'gt']], |
396
|
|
|
[['filter' => 'ge']], |
397
|
|
|
[['filter' => 'lt']], |
398
|
|
|
[['filter' => 'le']], |
399
|
|
|
[['filter' => 'nl']], |
400
|
|
|
[['filter' => 'nn']], |
401
|
|
|
[['filter' => 'in']], |
402
|
|
|
[['filter' => 'ni']], |
403
|
|
|
[['filter' => 'ss']], |
404
|
|
|
[['filter' => 'se']], |
405
|
|
|
[['filter' => 'sc']], |
406
|
|
|
[['filter' => 'sx']], |
407
|
|
|
[['filter' => 'bw']], |
408
|
|
|
[['format' => 'money']], |
409
|
|
|
[['format' => 'datetime']], |
410
|
|
|
[['format' => 'timeago']], |
411
|
|
|
[['trim' => true]], |
412
|
|
|
[['trim' => false]], |
413
|
|
|
[['trim' => 'true']], |
414
|
|
|
[['trim' => 'false']], |
415
|
|
|
[['fchars' => 0]], |
416
|
|
|
[['fchars' => 2]], |
417
|
|
|
[['fchars' => '2']], |
418
|
|
|
[['link' => true]], |
419
|
|
|
[['link' => false]], |
420
|
|
|
[['show' => 'a']], |
421
|
|
|
[['show' => 'i']], |
422
|
|
|
[['show' => 'c']], |
423
|
|
|
[['show' => 'r']], |
424
|
|
|
[['show' => 'u']], |
425
|
|
|
[['show' => 'd']], |
426
|
|
|
[['show' => 'i,c,r,u,d']], |
427
|
|
|
[['hide' => 'a']], |
428
|
|
|
[['hide' => 'i']], |
429
|
|
|
[['hide' => 'c']], |
430
|
|
|
[['hide' => 'r']], |
431
|
|
|
[['hide' => 'u']], |
432
|
|
|
[['hide' => 'd']], |
433
|
|
|
[['hide' => 'i,c,r,u,d']], |
434
|
|
|
]; |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|