|
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; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Schema\SchemaAttribute; |
|
13
|
|
|
|
|
14
|
|
|
class SchemaAttributeTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testItSchemaAttributeWithInvalidNamePropertiesThrowException(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
|
19
|
|
|
$this->expectExceptionMessage('Name:'); |
|
20
|
|
|
|
|
21
|
|
|
new SchemaAttribute('', 'string'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testItSchemaAttributeWithInvalidDataTypePropertiesThrowException(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
|
27
|
|
|
$this->expectExceptionMessage('DataType:'); |
|
28
|
|
|
|
|
29
|
|
|
new SchemaAttribute('foo', 'bar'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @dataProvider getConstraintLogicError |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testItSchemaAttributeConstraintLogicError(string $dataType, string $constraints): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
|
38
|
|
|
$this->expectExceptionMessage('Logic:'); |
|
39
|
|
|
|
|
40
|
|
|
new SchemaAttribute('foo', $dataType, $constraints); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testItSchemaAttributeWithRequiredPropertiesSetValues(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$name = 'foo'; |
|
46
|
|
|
$dataType = 'string'; |
|
47
|
|
|
|
|
48
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
51
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @dataProvider getRequiredConstraint |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $constraint |
|
58
|
|
|
* @param mixed $expected |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testItSchemaAttributeRequiredConstraints($constraint, $expected): void |
|
61
|
|
|
{ |
|
62
|
|
|
$name = 'foo'; |
|
63
|
|
|
$dataType = 'string'; |
|
64
|
|
|
|
|
65
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
68
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
69
|
|
|
$this->assertSame($expected, $schemaAttribute->isRequired()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @dataProvider getMinLengthConstraint |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $constraint |
|
76
|
|
|
* @param mixed $expected |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testItSchemaAttributeMinLengthConstraints($constraint, $expected): void |
|
79
|
|
|
{ |
|
80
|
|
|
$name = 'foo'; |
|
81
|
|
|
$dataType = 'string'; |
|
82
|
|
|
|
|
83
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
86
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
87
|
|
|
$this->assertSame($expected, $schemaAttribute->minLength()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @dataProvider getMinConstraint |
|
92
|
|
|
* |
|
93
|
|
|
* @param mixed $constraint |
|
94
|
|
|
* @param mixed $expected |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testItSchemaAttributeMinConstraints($constraint, $expected): void |
|
97
|
|
|
{ |
|
98
|
|
|
$name = 'min'; |
|
99
|
|
|
$dataType = 'integer'; |
|
100
|
|
|
|
|
101
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
104
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
105
|
|
|
$this->assertSame($expected, $schemaAttribute->min()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @dataProvider getMaxLengthConstraint |
|
110
|
|
|
* |
|
111
|
|
|
* @param mixed $constraint |
|
112
|
|
|
* @param mixed $expected |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testItSchemaAttributeMaxLengthConstraints($constraint, $expected): void |
|
115
|
|
|
{ |
|
116
|
|
|
$name = 'minLength'; |
|
117
|
|
|
$dataType = 'string'; |
|
118
|
|
|
|
|
119
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
122
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
123
|
|
|
$this->assertSame($expected, $schemaAttribute->maxLength()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @dataProvider getMaxConstraint |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $constraint |
|
130
|
|
|
* @param mixed $expected |
|
131
|
|
|
*/ |
|
132
|
|
|
public function testItSchemaAttributeMaxConstraints($constraint, $expected): void |
|
133
|
|
|
{ |
|
134
|
|
|
$name = 'max'; |
|
135
|
|
|
$dataType = 'integer'; |
|
136
|
|
|
|
|
137
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
140
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
141
|
|
|
$this->assertSame($expected, $schemaAttribute->max()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @dataProvider getMinCheckConstraint |
|
146
|
|
|
* |
|
147
|
|
|
* @param mixed $constraint |
|
148
|
|
|
* @param mixed $expected |
|
149
|
|
|
*/ |
|
150
|
|
|
public function testItSchemaAttributeMinCheckConstraints($constraint, $expected): void |
|
151
|
|
|
{ |
|
152
|
|
|
$name = 'minCheck'; |
|
153
|
|
|
$dataType = 'integer'; |
|
154
|
|
|
|
|
155
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
158
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
159
|
|
|
$this->assertSame($expected, $schemaAttribute->minCheck()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @dataProvider getMaxCheckConstraint |
|
164
|
|
|
* |
|
165
|
|
|
* @param mixed $constraint |
|
166
|
|
|
* @param mixed $expected |
|
167
|
|
|
*/ |
|
168
|
|
|
public function testItSchemaAttributeMaxCheckConstraints($constraint, $expected): void |
|
169
|
|
|
{ |
|
170
|
|
|
$name = 'maxCheck'; |
|
171
|
|
|
$dataType = 'integer'; |
|
172
|
|
|
|
|
173
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
174
|
|
|
|
|
175
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
176
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
177
|
|
|
$this->assertSame($expected, $schemaAttribute->maxCheck()); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @dataProvider getCheckConstraint |
|
182
|
|
|
* |
|
183
|
|
|
* @param mixed $constraint |
|
184
|
|
|
* @param mixed $expectedMin |
|
185
|
|
|
* @param mixed $expectedMax |
|
186
|
|
|
*/ |
|
187
|
|
|
public function testItSchemaAttributeCheckConstraints($constraint, $expectedMin, $expectedMax): void |
|
188
|
|
|
{ |
|
189
|
|
|
$name = 'check'; |
|
190
|
|
|
$dataType = 'integer'; |
|
191
|
|
|
|
|
192
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
195
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
196
|
|
|
$this->assertSame($expectedMin, $schemaAttribute->minCheck()); |
|
197
|
|
|
$this->assertSame($expectedMax, $schemaAttribute->maxCheck()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @dataProvider getLengthConstraint |
|
202
|
|
|
* |
|
203
|
|
|
* @param mixed $constraint |
|
204
|
|
|
* @param mixed $expectedMin |
|
205
|
|
|
* @param mixed $expectedMax |
|
206
|
|
|
*/ |
|
207
|
|
|
public function testItSchemaAttributeLengthConstraints($constraint, $expectedMin, $expectedMax): void |
|
208
|
|
|
{ |
|
209
|
|
|
$name = 'length'; |
|
210
|
|
|
$dataType = 'string'; |
|
211
|
|
|
|
|
212
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
213
|
|
|
|
|
214
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
215
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
216
|
|
|
$this->assertSame($expectedMin, $schemaAttribute->minLength()); |
|
217
|
|
|
$this->assertSame($expectedMax, $schemaAttribute->maxLength()); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @dataProvider getEqualToConstraint |
|
222
|
|
|
* |
|
223
|
|
|
* @param mixed $constraint |
|
224
|
|
|
* @param mixed $expected |
|
225
|
|
|
*/ |
|
226
|
|
|
public function testItSchemaAttributeEqualToConstraints($constraint, $expected): void |
|
227
|
|
|
{ |
|
228
|
|
|
$name = 'equalTo'; |
|
229
|
|
|
$dataType = 'string'; |
|
230
|
|
|
|
|
231
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
232
|
|
|
|
|
233
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
234
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
235
|
|
|
$this->assertSame($expected, $schemaAttribute->equalTo()); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @dataProvider getTypeConstraint |
|
240
|
|
|
* |
|
241
|
|
|
* @param mixed $constraint |
|
242
|
|
|
* @param mixed $expected |
|
243
|
|
|
*/ |
|
244
|
|
|
public function testItSchemaAttributeTypeConstraints($constraint, $expected): void |
|
245
|
|
|
{ |
|
246
|
|
|
$name = 'equalTo'; |
|
247
|
|
|
$dataType = 'string'; |
|
248
|
|
|
|
|
249
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
252
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
253
|
|
|
$this->assertSame($expected, $schemaAttribute->type()); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @dataProvider getPkConstraint |
|
258
|
|
|
* |
|
259
|
|
|
* @param mixed $constraint |
|
260
|
|
|
* @param mixed $expected |
|
261
|
|
|
*/ |
|
262
|
|
|
public function testItSchemaAttributePkConstraints($constraint, $expected): void |
|
263
|
|
|
{ |
|
264
|
|
|
$name = 'foo'; |
|
265
|
|
|
$dataType = 'string'; |
|
266
|
|
|
|
|
267
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
268
|
|
|
|
|
269
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
270
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
271
|
|
|
$this->assertSame($expected, $schemaAttribute->isPk()); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @dataProvider getAiConstraint |
|
276
|
|
|
* |
|
277
|
|
|
* @param mixed $constraint |
|
278
|
|
|
* @param mixed $expected |
|
279
|
|
|
*/ |
|
280
|
|
|
public function testItSchemaAttributeAiConstraints($constraint, $expected): void |
|
281
|
|
|
{ |
|
282
|
|
|
$name = 'foo'; |
|
283
|
|
|
$dataType = 'integer'; |
|
284
|
|
|
|
|
285
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
286
|
|
|
|
|
287
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
288
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
289
|
|
|
$this->assertSame($expected, $schemaAttribute->isAi()); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @dataProvider getCaConstraint |
|
294
|
|
|
* |
|
295
|
|
|
* @param mixed $constraint |
|
296
|
|
|
* @param mixed $expected |
|
297
|
|
|
*/ |
|
298
|
|
|
public function testItSchemaAttributeCaConstraints($constraint, $expected): void |
|
299
|
|
|
{ |
|
300
|
|
|
$name = 'foo'; |
|
301
|
|
|
$dataType = 'datetime'; |
|
302
|
|
|
|
|
303
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
304
|
|
|
|
|
305
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
306
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
307
|
|
|
$this->assertSame($expected, $schemaAttribute->isCa()); |
|
308
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlame()); |
|
309
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlameAt()); |
|
310
|
|
|
$this->assertSame(false, $schemaAttribute->isBlameBy()); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @dataProvider getUaConstraint |
|
315
|
|
|
* |
|
316
|
|
|
* @param mixed $constraint |
|
317
|
|
|
* @param mixed $expected |
|
318
|
|
|
*/ |
|
319
|
|
|
public function testItSchemaAttributeUaConstraints($constraint, $expected): void |
|
320
|
|
|
{ |
|
321
|
|
|
$name = 'foo'; |
|
322
|
|
|
$dataType = 'datetime'; |
|
323
|
|
|
|
|
324
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
325
|
|
|
|
|
326
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
327
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
328
|
|
|
$this->assertSame($expected, $schemaAttribute->isUa()); |
|
329
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlame()); |
|
330
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlameAt()); |
|
331
|
|
|
$this->assertSame(false, $schemaAttribute->isBlameBy()); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @dataProvider getCbConstraint |
|
336
|
|
|
* |
|
337
|
|
|
* @param mixed $constraint |
|
338
|
|
|
* @param mixed $expected |
|
339
|
|
|
*/ |
|
340
|
|
|
public function testItSchemaAttributeCbConstraints($constraint, $expected): void |
|
341
|
|
|
{ |
|
342
|
|
|
$name = 'foo'; |
|
343
|
|
|
$dataType = 'integer'; |
|
344
|
|
|
|
|
345
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
346
|
|
|
|
|
347
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
348
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
349
|
|
|
$this->assertSame($expected, $schemaAttribute->isCb()); |
|
350
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlame()); |
|
351
|
|
|
$this->assertSame(false, $schemaAttribute->isBlameAt()); |
|
352
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlameBy()); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @dataProvider getUbConstraint |
|
357
|
|
|
* |
|
358
|
|
|
* @param mixed $constraint |
|
359
|
|
|
* @param mixed $expected |
|
360
|
|
|
*/ |
|
361
|
|
|
public function testItSchemaAttributeUbConstraints($constraint, $expected): void |
|
362
|
|
|
{ |
|
363
|
|
|
$name = 'foo'; |
|
364
|
|
|
$dataType = 'integer'; |
|
365
|
|
|
|
|
366
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
367
|
|
|
|
|
368
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
369
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
370
|
|
|
$this->assertSame($expected, $schemaAttribute->isUb()); |
|
371
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlame()); |
|
372
|
|
|
$this->assertSame(false, $schemaAttribute->isBlameAt()); |
|
373
|
|
|
$this->assertSame($expected, $schemaAttribute->isBlameBy()); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* @dataProvider getFkConstraint |
|
378
|
|
|
* |
|
379
|
|
|
* @param mixed $constraint |
|
380
|
|
|
* @param mixed $fkTable |
|
381
|
|
|
* @param mixed $fkId |
|
382
|
|
|
* @param mixed $fkName |
|
383
|
|
|
*/ |
|
384
|
|
|
public function testItSchemaAttributeFkConstraints($constraint, $fkTable, $fkId, $fkName): void |
|
385
|
|
|
{ |
|
386
|
|
|
$name = 'foreing'; |
|
387
|
|
|
$dataType = 'integer'; |
|
388
|
|
|
|
|
389
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
|
390
|
|
|
|
|
391
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
392
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
393
|
|
|
$this->assertEquals('int', $schemaAttribute->typeHint()); |
|
394
|
|
|
$this->assertTrue($schemaAttribute->isFk()); |
|
395
|
|
|
$this->assertSame($fkTable, $schemaAttribute->fkTable()); |
|
396
|
|
|
$this->assertSame($fkId, $schemaAttribute->fkId()); |
|
397
|
|
|
$this->assertSame($fkName, $schemaAttribute->fkName()); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
public function testItSchemaAttributeConstraintsAsString(): void |
|
401
|
|
|
{ |
|
402
|
|
|
$name = 'foo'; |
|
403
|
|
|
$dataType = 'string'; |
|
404
|
|
|
$constraints = 'required|minlength:8|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number'; |
|
405
|
|
|
|
|
406
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
|
407
|
|
|
|
|
408
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
409
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
410
|
|
|
$this->assertEquals('string', $schemaAttribute->typeHint()); |
|
411
|
|
|
$this->assertIsArray($schemaAttribute->constraints()); |
|
412
|
|
|
$this->assertSame(true, $schemaAttribute->isRequired()); |
|
413
|
|
|
$this->assertNull($schemaAttribute->min()); |
|
414
|
|
|
$this->assertSame(8, $schemaAttribute->minLength()); |
|
415
|
|
|
$this->assertSame(3, $schemaAttribute->minCheck()); |
|
416
|
|
|
$this->assertNull($schemaAttribute->max()); |
|
417
|
|
|
$this->assertSame(10, $schemaAttribute->maxLength()); |
|
418
|
|
|
$this->assertSame(4, $schemaAttribute->maxCheck()); |
|
419
|
|
|
$this->assertSame('#bar', $schemaAttribute->equalTo()); |
|
420
|
|
|
$this->assertSame('number', $schemaAttribute->type()); |
|
421
|
|
|
$this->assertFalse($schemaAttribute->isPk()); |
|
422
|
|
|
$this->assertFalse($schemaAttribute->isAi()); |
|
423
|
|
|
$this->assertFalse($schemaAttribute->isFk()); |
|
424
|
|
|
$this->assertNull($schemaAttribute->fkTable()); |
|
425
|
|
|
$this->assertNull($schemaAttribute->fkId()); |
|
426
|
|
|
$this->assertNull($schemaAttribute->fkName()); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
public function testItSchemaAttributeConstraintsAsArray(): void |
|
430
|
|
|
{ |
|
431
|
|
|
$name = 'foo'; |
|
432
|
|
|
$dataType = 'integer'; |
|
433
|
|
|
$constraints = [ |
|
434
|
|
|
'required', |
|
435
|
|
|
'equalto' => '#id', |
|
436
|
|
|
'type' => 'text', |
|
437
|
|
|
'pk' => true, |
|
438
|
|
|
'ai' => true, |
|
439
|
|
|
]; |
|
440
|
|
|
|
|
441
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
|
442
|
|
|
|
|
443
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
444
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
445
|
|
|
$this->assertEquals('int', $schemaAttribute->typeHint()); |
|
446
|
|
|
$this->assertIsArray($schemaAttribute->constraints()); |
|
447
|
|
|
$this->assertSame(true, $schemaAttribute->isRequired()); |
|
448
|
|
|
$this->assertNull($schemaAttribute->min()); |
|
449
|
|
|
$this->assertNull($schemaAttribute->minLength()); |
|
450
|
|
|
$this->assertNull($schemaAttribute->minCheck()); |
|
451
|
|
|
$this->assertNull($schemaAttribute->max()); |
|
452
|
|
|
$this->assertNull($schemaAttribute->maxLength()); |
|
453
|
|
|
$this->assertNull($schemaAttribute->maxCheck()); |
|
454
|
|
|
$this->assertSame('#id', $schemaAttribute->equalTo()); |
|
455
|
|
|
$this->assertSame('text', $schemaAttribute->type()); |
|
456
|
|
|
$this->assertTrue($schemaAttribute->isPk()); |
|
457
|
|
|
$this->assertTrue($schemaAttribute->isAi()); |
|
458
|
|
|
$this->assertFalse($schemaAttribute->isFk()); |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
public function testItSchemaAttributeConstraintsAsArrayCast(): void |
|
462
|
|
|
{ |
|
463
|
|
|
$name = 'foo'; |
|
464
|
|
|
$dataType = 'datetime'; |
|
465
|
|
|
$constraints = [ |
|
466
|
|
|
'required', |
|
467
|
|
|
'equalto' => '#bar', |
|
468
|
|
|
'type' => 'number', |
|
469
|
|
|
]; |
|
470
|
|
|
|
|
471
|
|
|
$schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
|
472
|
|
|
|
|
473
|
|
|
$this->assertEquals($name, $schemaAttribute->name()); |
|
474
|
|
|
$this->assertEquals($dataType, $schemaAttribute->dataType()); |
|
475
|
|
|
$this->assertEquals('\DateTime', $schemaAttribute->typeHint()); |
|
476
|
|
|
$this->assertIsArray($schemaAttribute->constraints()); |
|
477
|
|
|
$this->assertSame(true, $schemaAttribute->isRequired()); |
|
478
|
|
|
$this->assertNull($schemaAttribute->min()); |
|
479
|
|
|
$this->assertNull($schemaAttribute->minLength()); |
|
480
|
|
|
$this->assertNull($schemaAttribute->minCheck()); |
|
481
|
|
|
$this->assertNull($schemaAttribute->max()); |
|
482
|
|
|
$this->assertNull($schemaAttribute->maxLength()); |
|
483
|
|
|
$this->assertNull($schemaAttribute->maxCheck()); |
|
484
|
|
|
$this->assertSame('#bar', $schemaAttribute->equalTo()); |
|
485
|
|
|
$this->assertSame('number', $schemaAttribute->type()); |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
public function getRequiredConstraint(): array |
|
489
|
|
|
{ |
|
490
|
|
|
return [ |
|
491
|
|
|
['', false], |
|
492
|
|
|
['required', true], |
|
493
|
|
|
['required:true', true], |
|
494
|
|
|
['required:false', false], |
|
495
|
|
|
[['required'], true], |
|
496
|
|
|
[['required' => true], true], |
|
497
|
|
|
[['required' => false], false], |
|
498
|
|
|
]; |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
public function getMinLengthConstraint(): array |
|
502
|
|
|
{ |
|
503
|
|
|
return [ |
|
504
|
|
|
['', null], |
|
505
|
|
|
['minlength:8', 8], |
|
506
|
|
|
[['minlength' => 8], 8], |
|
507
|
|
|
[['minlength' => '8'], 8], |
|
508
|
|
|
]; |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
public function getMinConstraint(): array |
|
512
|
|
|
{ |
|
513
|
|
|
return [ |
|
514
|
|
|
['', null], |
|
515
|
|
|
['min:8', 8], |
|
516
|
|
|
[['min' => 8], 8], |
|
517
|
|
|
[['min' => '8'], 8], |
|
518
|
|
|
]; |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
public function getMaxLengthConstraint(): array |
|
522
|
|
|
{ |
|
523
|
|
|
return [ |
|
524
|
|
|
['', null], |
|
525
|
|
|
['maxlength:10', 10], |
|
526
|
|
|
[['maxlength' => 10], 10], |
|
527
|
|
|
[['maxlength' => '10'], 10], |
|
528
|
|
|
]; |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
public function getMaxConstraint(): array |
|
532
|
|
|
{ |
|
533
|
|
|
return [ |
|
534
|
|
|
['', null], |
|
535
|
|
|
['max:100', 100], |
|
536
|
|
|
[['max' => 100], 100], |
|
537
|
|
|
[['max' => '100'], 100], |
|
538
|
|
|
]; |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
public function getMinCheckConstraint(): array |
|
542
|
|
|
{ |
|
543
|
|
|
return [ |
|
544
|
|
|
['', null], |
|
545
|
|
|
['mincheck:3', 3], |
|
546
|
|
|
[['mincheck' => 3], 3], |
|
547
|
|
|
[['mincheck' => '3'], 3], |
|
548
|
|
|
]; |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
public function getMaxCheckConstraint(): array |
|
552
|
|
|
{ |
|
553
|
|
|
return [ |
|
554
|
|
|
['', null], |
|
555
|
|
|
['maxcheck:4', 4], |
|
556
|
|
|
[['maxcheck' => 4], 4], |
|
557
|
|
|
[['maxcheck' => '4'], 4], |
|
558
|
|
|
]; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
public function getCheckConstraint(): array |
|
562
|
|
|
{ |
|
563
|
|
|
return [ |
|
564
|
|
|
['', null, null], |
|
565
|
|
|
['check:3,4', 3, 4], |
|
566
|
|
|
[['check' => [ |
|
567
|
|
|
'min' => 5, |
|
568
|
|
|
'max' => 6, |
|
569
|
|
|
]], 5, 6], |
|
570
|
|
|
]; |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
public function getLengthConstraint(): array |
|
574
|
|
|
{ |
|
575
|
|
|
return [ |
|
576
|
|
|
['', null, null], |
|
577
|
|
|
['length:30,40', 30, 40], |
|
578
|
|
|
[['length' => [ |
|
579
|
|
|
'min' => 5, |
|
580
|
|
|
'max' => 60, |
|
581
|
|
|
]], 5, 60], |
|
582
|
|
|
]; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
public function getEqualToConstraint(): array |
|
586
|
|
|
{ |
|
587
|
|
|
return [ |
|
588
|
|
|
['', null], |
|
589
|
|
|
['equalto:#foo', '#foo'], |
|
590
|
|
|
[['equalto' => '.foo'], '.foo'], |
|
591
|
|
|
]; |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
public function getTypeConstraint(): array |
|
595
|
|
|
{ |
|
596
|
|
|
return [ |
|
597
|
|
|
['', null], |
|
598
|
|
|
['type:number', 'number'], |
|
599
|
|
|
[['type' => 'text'], 'text'], |
|
600
|
|
|
]; |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
public function getPkConstraint(): array |
|
604
|
|
|
{ |
|
605
|
|
|
return [ |
|
606
|
|
|
['', false], |
|
607
|
|
|
['required|pk', true], |
|
608
|
|
|
['required|pk:true', true], |
|
609
|
|
|
['required|pk:false', false], |
|
610
|
|
|
[['required', 'pk'], true], |
|
611
|
|
|
[['required', 'pk' => true], true], |
|
612
|
|
|
[['required', 'pk' => false], false], |
|
613
|
|
|
]; |
|
614
|
|
|
} |
|
615
|
|
|
|
|
616
|
|
|
public function getAiConstraint(): array |
|
617
|
|
|
{ |
|
618
|
|
|
return [ |
|
619
|
|
|
['', false], |
|
620
|
|
|
['required|pk|ai', true], |
|
621
|
|
|
['required|pk|ai:true', true], |
|
622
|
|
|
// ['required|pk|ai:false', false], |
|
623
|
|
|
[['required', 'pk', 'ai'], true], |
|
624
|
|
|
[['required', 'pk', 'ai' => true], true], |
|
625
|
|
|
// [['required', 'pk', 'ai' => false], false], |
|
626
|
|
|
]; |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
public function getCaConstraint(): array |
|
630
|
|
|
{ |
|
631
|
|
|
return [ |
|
632
|
|
|
['', false], |
|
633
|
|
|
['ca', true], |
|
634
|
|
|
['ca:true', true], |
|
635
|
|
|
['ca:false', false], |
|
636
|
|
|
[['ca'], true], |
|
637
|
|
|
[['ca' => true], true], |
|
638
|
|
|
[['ca' => false], false], |
|
639
|
|
|
]; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
public function getUaConstraint(): array |
|
643
|
|
|
{ |
|
644
|
|
|
return [ |
|
645
|
|
|
['', false], |
|
646
|
|
|
['ua', true], |
|
647
|
|
|
['ua:true', true], |
|
648
|
|
|
['ua:false', false], |
|
649
|
|
|
[['ua'], true], |
|
650
|
|
|
[['ua' => true], true], |
|
651
|
|
|
[['ua' => false], false], |
|
652
|
|
|
]; |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
public function getFkConstraint(): array |
|
656
|
|
|
{ |
|
657
|
|
|
return [ |
|
658
|
|
|
['fk:table', 'table', 'id', 'name'], |
|
659
|
|
|
['fk:table2,username', 'table2', 'id', 'username'], |
|
660
|
|
|
['fk:table3,description,uuid', 'table3', 'uuid', 'description'], |
|
661
|
|
|
[['fk' => 'table5'], 'table5', 'id', 'name'], |
|
662
|
|
|
[['fk' => 'table6,username'], 'table6', 'id', 'username'], |
|
663
|
|
|
[['fk' => 'table7,description,uuid'], 'table7', 'uuid', 'description'], |
|
664
|
|
|
]; |
|
665
|
|
|
} |
|
666
|
|
|
|
|
667
|
|
|
public function getCbConstraint(): array |
|
668
|
|
|
{ |
|
669
|
|
|
return [ |
|
670
|
|
|
['', false], |
|
671
|
|
|
['cb', true], |
|
672
|
|
|
['cb:true', true], |
|
673
|
|
|
['cb:false', false], |
|
674
|
|
|
[['cb'], true], |
|
675
|
|
|
[['cb' => true], true], |
|
676
|
|
|
[['cb' => false], false], |
|
677
|
|
|
]; |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
public function getUbConstraint(): array |
|
681
|
|
|
{ |
|
682
|
|
|
return [ |
|
683
|
|
|
['', false], |
|
684
|
|
|
['ub', true], |
|
685
|
|
|
['ub:true', true], |
|
686
|
|
|
['ub:false', false], |
|
687
|
|
|
[['ub'], true], |
|
688
|
|
|
[['ub' => true], true], |
|
689
|
|
|
[['ub' => false], false], |
|
690
|
|
|
]; |
|
691
|
|
|
} |
|
692
|
|
|
|
|
693
|
|
|
public function getConstraintLogicError(): array |
|
694
|
|
|
{ |
|
695
|
|
|
return [ |
|
696
|
|
|
['integer', 'pk|ai|required|min:10'], |
|
697
|
|
|
['integer', 'pk|ai|required|fk:table'], |
|
698
|
|
|
['integer', 'pk|required'], |
|
699
|
|
|
['integer', 'ai'], |
|
700
|
|
|
['integer', 'ai|pk'], |
|
701
|
|
|
['string', 'ai|pk|required'], |
|
702
|
|
|
['string', 'ca'], |
|
703
|
|
|
['integer', 'ua'], |
|
704
|
|
|
['datetime', 'ca|ua'], |
|
705
|
|
|
['integer', 'fk:table|ai|pk|required'], |
|
706
|
|
|
['integer', 'required:false|ai|pk'], |
|
707
|
|
|
['integer', 'required:false|pk'], |
|
708
|
|
|
['smallint', 'maxlength:100'], |
|
709
|
|
|
['integer', 'maxlength:100'], |
|
710
|
|
|
['bigint', 'maxlength:100'], |
|
711
|
|
|
['double', 'maxlength:100'], |
|
712
|
|
|
['float', 'maxlength:100'], |
|
713
|
|
|
['smallint', 'minlength:10'], |
|
714
|
|
|
['integer', 'minlength:10'], |
|
715
|
|
|
['bigint', 'minlength:10'], |
|
716
|
|
|
['double', 'minlength:10'], |
|
717
|
|
|
['float', 'minlength:10'], |
|
718
|
|
|
['string', 'min:100'], |
|
719
|
|
|
['text', 'max:10'], |
|
720
|
|
|
['guid', 'min:10'], |
|
721
|
|
|
['binary', 'max:10'], |
|
722
|
|
|
['blob', 'maxlength:1'], |
|
723
|
|
|
['datetimetz', 'maxlength:1'], |
|
724
|
|
|
['time', 'mincheck:1'], |
|
725
|
|
|
['datetime', 'maxcheck:1'], |
|
726
|
|
|
['datetime', 'cb'], |
|
727
|
|
|
['bool', 'ub'], |
|
728
|
|
|
['binary', 'cb'], |
|
729
|
|
|
['blob', 'ub'], |
|
730
|
|
|
['integer', 'cb|ub'], |
|
731
|
|
|
]; |
|
732
|
|
|
} |
|
733
|
|
|
} |
|
734
|
|
|
|