Completed
Push — develop ( 705c20...1dfcbc )
by Freddie
03:12
created

testItSchemaAttributeConstraintLogicError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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
    }
310
311
    /**
312
     * @dataProvider getUaConstraint
313
     *
314
     * @param mixed $constraint
315
     * @param mixed $expected
316
     */
317
    public function testItSchemaAttributeUaConstraints($constraint, $expected): void
318
    {
319
        $name = 'foo';
320
        $dataType = 'datetime';
321
322
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint);
323
324
        $this->assertEquals($name, $schemaAttribute->name());
325
        $this->assertEquals($dataType, $schemaAttribute->dataType());
326
        $this->assertSame($expected, $schemaAttribute->isUa());
327
        $this->assertSame($expected, $schemaAttribute->isBlame());
328
    }
329
330
    /**
331
     * @dataProvider getFkConstraint
332
     *
333
     * @param mixed $constraint
334
     * @param mixed $fkTable
335
     * @param mixed $fkId
336
     * @param mixed $fkName
337
     */
338
    public function testItSchemaAttributeFkConstraints($constraint, $fkTable, $fkId, $fkName): void
339
    {
340
        $name = 'foreing';
341
        $dataType = 'integer';
342
343
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint);
344
345
        $this->assertEquals($name, $schemaAttribute->name());
346
        $this->assertEquals($dataType, $schemaAttribute->dataType());
347
        $this->assertEquals('int', $schemaAttribute->typeHint());
348
        $this->assertTrue($schemaAttribute->isFk());
349
        $this->assertSame($fkTable, $schemaAttribute->fkTable());
350
        $this->assertSame($fkId, $schemaAttribute->fkId());
351
        $this->assertSame($fkName, $schemaAttribute->fkName());
352
    }
353
354
    public function testItSchemaAttributeConstraintsAsString(): void
355
    {
356
        $name = 'foo';
357
        $dataType = 'string';
358
        $constraints = 'required|minlength:8|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number';
359
360
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
361
362
        $this->assertEquals($name, $schemaAttribute->name());
363
        $this->assertEquals($dataType, $schemaAttribute->dataType());
364
        $this->assertEquals('string', $schemaAttribute->typeHint());
365
        $this->assertIsArray($schemaAttribute->constraints());
366
        $this->assertSame(true, $schemaAttribute->isRequired());
367
        $this->assertNull($schemaAttribute->min());
368
        $this->assertSame(8, $schemaAttribute->minLength());
369
        $this->assertSame(3, $schemaAttribute->minCheck());
370
        $this->assertNull($schemaAttribute->max());
371
        $this->assertSame(10, $schemaAttribute->maxLength());
372
        $this->assertSame(4, $schemaAttribute->maxCheck());
373
        $this->assertSame('#bar', $schemaAttribute->equalTo());
374
        $this->assertSame('number', $schemaAttribute->type());
375
        $this->assertFalse($schemaAttribute->isPk());
376
        $this->assertFalse($schemaAttribute->isAi());
377
        $this->assertFalse($schemaAttribute->isFk());
378
        $this->assertNull($schemaAttribute->fkTable());
379
        $this->assertNull($schemaAttribute->fkId());
380
        $this->assertNull($schemaAttribute->fkName());
381
    }
382
383
    public function testItSchemaAttributeConstraintsAsArray(): void
384
    {
385
        $name = 'foo';
386
        $dataType = 'integer';
387
        $constraints = [
388
            'required',
389
            'min' => 1,
390
            'max' => 100,
391
            'mincheck' => 3,
392
            'maxcheck' => 4,
393
            'equalto' => '#id',
394
            'type' => 'text',
395
            'pk' => true,
396
            'ai' => true,
397
        ];
398
399
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
400
401
        $this->assertEquals($name, $schemaAttribute->name());
402
        $this->assertEquals($dataType, $schemaAttribute->dataType());
403
        $this->assertEquals('int', $schemaAttribute->typeHint());
404
        $this->assertIsArray($schemaAttribute->constraints());
405
        $this->assertSame(true, $schemaAttribute->isRequired());
406
        $this->assertSame(1, $schemaAttribute->min());
407
        $this->assertNull($schemaAttribute->minLength());
408
        $this->assertSame(3, $schemaAttribute->minCheck());
409
        $this->assertSame(100, $schemaAttribute->max());
410
        $this->assertNull($schemaAttribute->maxLength());
411
        $this->assertSame(4, $schemaAttribute->maxCheck());
412
        $this->assertSame('#id', $schemaAttribute->equalTo());
413
        $this->assertSame('text', $schemaAttribute->type());
414
        $this->assertTrue($schemaAttribute->isPk());
415
        $this->assertTrue($schemaAttribute->isAi());
416
        $this->assertFalse($schemaAttribute->isFk());
417
    }
418
419
    public function testItSchemaAttributeConstraintsAsArrayCast(): void
420
    {
421
        $name = 'foo';
422
        $dataType = 'datetime';
423
        $constraints = [
424
            'required',
425
            'equalto' => '#bar',
426
            'type' => 'number',
427
        ];
428
429
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
430
431
        $this->assertEquals($name, $schemaAttribute->name());
432
        $this->assertEquals($dataType, $schemaAttribute->dataType());
433
        $this->assertEquals('\DateTime', $schemaAttribute->typeHint());
434
        $this->assertIsArray($schemaAttribute->constraints());
435
        $this->assertSame(true, $schemaAttribute->isRequired());
436
        $this->assertNull($schemaAttribute->min());
437
        $this->assertNull($schemaAttribute->minLength());
438
        $this->assertNull($schemaAttribute->minCheck());
439
        $this->assertNull($schemaAttribute->max());
440
        $this->assertNull($schemaAttribute->maxLength());
441
        $this->assertNull($schemaAttribute->maxCheck());
442
        $this->assertSame('#bar', $schemaAttribute->equalTo());
443
        $this->assertSame('number', $schemaAttribute->type());
444
    }
445
446
    public function getRequiredConstraint(): array
447
    {
448
        return [
449
            ['', false],
450
            ['required', true],
451
            ['required:true', true],
452
            ['required:false', false],
453
            [['required'], true],
454
            [['required' => true], true],
455
            [['required' => false], false],
456
        ];
457
    }
458
459
    public function getMinLengthConstraint(): array
460
    {
461
        return [
462
            ['', null],
463
            ['minlength:8', 8],
464
            [['minlength' => 8], 8],
465
            [['minlength' => '8'], 8],
466
        ];
467
    }
468
469
    public function getMinConstraint(): array
470
    {
471
        return [
472
            ['', null],
473
            ['min:8', 8],
474
            [['min' => 8], 8],
475
            [['min' => '8'], 8],
476
        ];
477
    }
478
479
    public function getMaxLengthConstraint(): array
480
    {
481
        return [
482
            ['', null],
483
            ['maxlength:10', 10],
484
            [['maxlength' => 10], 10],
485
            [['maxlength' => '10'], 10],
486
        ];
487
    }
488
489
    public function getMaxConstraint(): array
490
    {
491
        return [
492
            ['', null],
493
            ['max:100', 100],
494
            [['max' => 100], 100],
495
            [['max' => '100'], 100],
496
        ];
497
    }
498
499
    public function getMinCheckConstraint(): array
500
    {
501
        return [
502
            ['', null],
503
            ['mincheck:3', 3],
504
            [['mincheck' => 3], 3],
505
            [['mincheck' => '3'], 3],
506
        ];
507
    }
508
509
    public function getMaxCheckConstraint(): array
510
    {
511
        return [
512
            ['', null],
513
            ['maxcheck:4', 4],
514
            [['maxcheck' => 4], 4],
515
            [['maxcheck' => '4'], 4],
516
        ];
517
    }
518
519
    public function getCheckConstraint(): array
520
    {
521
        return [
522
            ['', null, null],
523
            ['check:3,4', 3, 4],
524
            [['check' => [
525
                'min' => 5,
526
                'max' => 6,
527
            ]], 5, 6],
528
        ];
529
    }
530
531
    public function getLengthConstraint(): array
532
    {
533
        return [
534
            ['', null, null],
535
            ['length:30,40', 30, 40],
536
            [['length' => [
537
                'min' => 5,
538
                'max' => 60,
539
            ]], 5, 60],
540
        ];
541
    }
542
543
    public function getEqualToConstraint(): array
544
    {
545
        return [
546
            ['', null],
547
            ['equalto:#foo', '#foo'],
548
            [['equalto' => '.foo'], '.foo'],
549
        ];
550
    }
551
552
    public function getTypeConstraint(): array
553
    {
554
        return [
555
            ['', null],
556
            ['type:number', 'number'],
557
            [['type' => 'text'], 'text'],
558
        ];
559
    }
560
561
    public function getPkConstraint(): array
562
    {
563
        return [
564
            ['', false],
565
            ['required|pk', true],
566
            ['required|pk:true', true],
567
            ['required|pk:false', false],
568
            [['required', 'pk'], true],
569
            [['required', 'pk' => true], true],
570
            [['required', 'pk' => false], false],
571
        ];
572
    }
573
574
    public function getAiConstraint(): array
575
    {
576
        return [
577
            ['', false],
578
            ['required|pk|ai', true],
579
            ['required|pk|ai:true', true],
580
            ['required|pk|ai:false', false],
581
            [['required', 'pk', 'ai'], true],
582
            [['required', 'pk', 'ai' => true], true],
583
            [['required', 'pk', 'ai' => false], false],
584
        ];
585
    }
586
587
    public function getCaConstraint(): array
588
    {
589
        return [
590
            ['', false],
591
            ['ca', true],
592
            ['ca:true', true],
593
            ['ca:false', false],
594
            [['ca'], true],
595
            [['ca' => true], true],
596
            [['ca' => false], false],
597
        ];
598
    }
599
600
    public function getUaConstraint(): array
601
    {
602
        return [
603
            ['', false],
604
            ['ua', true],
605
            ['ua:true', true],
606
            ['ua:false', false],
607
            [['ua'], true],
608
            [['ua' => true], true],
609
            [['ua' => false], false],
610
        ];
611
    }
612
613
    public function getFkConstraint(): array
614
    {
615
        return [
616
            ['fk:table', 'table', 'id', 'name'],
617
            ['fk:table2,username', 'table2', 'id', 'username'],
618
            ['fk:table3,description,uuid', 'table3', 'uuid', 'description'],
619
            [['fk' => 'table5'], 'table5', 'id', 'name'],
620
            [['fk' => 'table6,username'], 'table6', 'id', 'username'],
621
            [['fk' => 'table7,description,uuid'], 'table7', 'uuid', 'description'],
622
        ];
623
    }
624
625
    public function getConstraintLogicError(): array
626
    {
627
        return [
628
            ['integer', 'ai'],
629
            ['integer', 'ai|pk'],
630
            ['string', 'ai|pk|required'],
631
            ['string', 'ca'],
632
            ['integer', 'ua'],
633
            ['datetime', 'ca|ua'],
634
            ['integer', 'fk:table|ai|pk|required'],
635
            ['integer', 'required:false|ai|pk'],
636
            ['integer', 'required:false|pk'],
637
            ['smallint', 'maxlength:100'],
638
            ['integer', 'maxlength:100'],
639
            ['bigint', 'maxlength:100'],
640
            ['double', 'maxlength:100'],
641
            ['float', 'maxlength:100'],
642
            ['smallint', 'minlength:10'],
643
            ['integer', 'minlength:10'],
644
            ['bigint', 'minlength:10'],
645
            ['double', 'minlength:10'],
646
            ['float', 'minlength:10'],
647
            ['string', 'min:100'],
648
            ['text', 'max:10'],
649
            ['guid', 'min:10'],
650
            ['binary', 'max:10'],
651
            ['blob', 'maxlength:1'],
652
            ['datetimetz', 'maxlength:1'],
653
            ['time', 'mincheck:1'],
654
            ['datetime', 'maxcheck:1'],
655
        ];
656
    }
657
}
658