Completed
Push — develop ( 21f970...705c20 )
by Freddie
03:24
created

testItSchemaAttributeMaxLengthConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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