Completed
Push — develop ( 1f2dbe...562875 )
by Freddie
03:44
created

testItSchemaAttributeEqualToConstraints()   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 getFkConstraint
283
     *
284
     * @param mixed $constraint
285
     * @param mixed $fkTable
286
     * @param mixed $fkId
287
     * @param mixed $fkName
288
     */
289
    public function testItSchemaAttributeFkConstraints($constraint, $fkTable, $fkId, $fkName): void
290
    {
291
        $name = 'foreing';
292
        $dataType = 'integer';
293
294
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint);
295
296
        $this->assertEquals($name, $schemaAttribute->name());
297
        $this->assertEquals($dataType, $schemaAttribute->dataType());
298
        $this->assertTrue($schemaAttribute->isFk());
299
        $this->assertSame($fkTable, $schemaAttribute->fkTable());
300
        $this->assertSame($fkId, $schemaAttribute->fkId());
301
        $this->assertSame($fkName, $schemaAttribute->fkName());
302
    }
303
304
    public function testItSchemaAttributeConstraintsAsString(): void
305
    {
306
        $name = 'foo';
307
        $dataType = 'string';
308
        $constraints = 'required|min:1|minlength:8|max:100|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number';
309
310
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
311
312
        $this->assertEquals($name, $schemaAttribute->name());
313
        $this->assertEquals($dataType, $schemaAttribute->dataType());
314
        $this->assertIsArray($schemaAttribute->constraints());
315
        $this->assertSame(true, $schemaAttribute->isRequired());
316
        $this->assertSame(1, $schemaAttribute->min());
317
        $this->assertSame(8, $schemaAttribute->minLength());
318
        $this->assertSame(3, $schemaAttribute->minCheck());
319
        $this->assertSame(100, $schemaAttribute->max());
320
        $this->assertSame(10, $schemaAttribute->maxLength());
321
        $this->assertSame(4, $schemaAttribute->maxCheck());
322
        $this->assertSame('#bar', $schemaAttribute->equalTo());
323
        $this->assertSame('number', $schemaAttribute->type());
324
        $this->assertFalse($schemaAttribute->isPk());
325
        $this->assertFalse($schemaAttribute->isAi());
326
        $this->assertFalse($schemaAttribute->isFk());
327
        $this->assertNull($schemaAttribute->fkTable());
328
        $this->assertNull($schemaAttribute->fkId());
329
        $this->assertNull($schemaAttribute->fkName());
330
    }
331
332
    public function testItSchemaAttributeConstraintsAsArray(): void
333
    {
334
        $name = 'foo';
335
        $dataType = 'string';
336
        $constraints = [
337
            'required',
338
            'min' => 1,
339
            'minlength' => 8,
340
            'max' => 100,
341
            'maxlength' => 10,
342
            'mincheck' => 3,
343
            'maxcheck' => 4,
344
            'equalto' => '#id',
345
            'type' => 'text',
346
            'pk' => true,
347
            'ai' => true,
348
            'fk' => 'table,name,id',
349
        ];
350
351
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
352
353
        $this->assertEquals($name, $schemaAttribute->name());
354
        $this->assertEquals($dataType, $schemaAttribute->dataType());
355
        $this->assertIsArray($schemaAttribute->constraints());
356
        $this->assertSame(true, $schemaAttribute->isRequired());
357
        $this->assertSame(1, $schemaAttribute->min());
358
        $this->assertSame(8, $schemaAttribute->minLength());
359
        $this->assertSame(3, $schemaAttribute->minCheck());
360
        $this->assertSame(100, $schemaAttribute->max());
361
        $this->assertSame(10, $schemaAttribute->maxLength());
362
        $this->assertSame(4, $schemaAttribute->maxCheck());
363
        $this->assertSame('#id', $schemaAttribute->equalTo());
364
        $this->assertSame('text', $schemaAttribute->type());
365
        $this->assertTrue($schemaAttribute->isPk());
366
        $this->assertTrue($schemaAttribute->isAi());
367
        $this->assertTrue($schemaAttribute->isFk());
368
        $this->assertSame('table', $schemaAttribute->fkTable());
369
        $this->assertSame('id', $schemaAttribute->fkId());
370
        $this->assertSame('name', $schemaAttribute->fkName());
371
    }
372
373
    public function testItSchemaAttributeConstraintsAsArrayCast(): void
374
    {
375
        $name = 'foo';
376
        $dataType = 'string';
377
        $constraints = [
378
            'required',
379
            'min' => '1',
380
            'minlength' => '8',
381
            'max' => '100',
382
            'maxlength' => '10',
383
            'mincheck' => '3',
384
            'maxcheck' => '4',
385
            'equalto' => '#bar',
386
            'type' => 'number',
387
        ];
388
389
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
390
391
        $this->assertEquals($name, $schemaAttribute->name());
392
        $this->assertEquals($dataType, $schemaAttribute->dataType());
393
        $this->assertIsArray($schemaAttribute->constraints());
394
        $this->assertSame(true, $schemaAttribute->isRequired());
395
        $this->assertSame(1, $schemaAttribute->min());
396
        $this->assertSame(8, $schemaAttribute->minLength());
397
        $this->assertSame(3, $schemaAttribute->minCheck());
398
        $this->assertSame(100, $schemaAttribute->max());
399
        $this->assertSame(10, $schemaAttribute->maxLength());
400
        $this->assertSame(4, $schemaAttribute->maxCheck());
401
        $this->assertSame('#bar', $schemaAttribute->equalTo());
402
        $this->assertSame('number', $schemaAttribute->type());
403
    }
404
405
    public function getRequiredConstraint(): array
406
    {
407
        return [
408
            ['', false],
409
            ['required', true],
410
            ['required:true', true],
411
            ['required:false', false],
412
            [['required'], true],
413
            [['required' => true], true],
414
            [['required' => false], false],
415
        ];
416
    }
417
418
    public function getMinLengthConstraint(): array
419
    {
420
        return [
421
            ['', null],
422
            ['minlength:8', 8],
423
            [['minlength' => 8], 8],
424
            [['minlength' => '8'], 8],
425
        ];
426
    }
427
428
    public function getMinConstraint(): array
429
    {
430
        return [
431
            ['', null],
432
            ['min:8', 8],
433
            [['min' => 8], 8],
434
            [['min' => '8'], 8],
435
        ];
436
    }
437
438
    public function getMaxLengthConstraint(): array
439
    {
440
        return [
441
            ['', null],
442
            ['maxlength:10', 10],
443
            [['maxlength' => 10], 10],
444
            [['maxlength' => '10'], 10],
445
        ];
446
    }
447
448
    public function getMaxConstraint(): array
449
    {
450
        return [
451
            ['', null],
452
            ['max:100', 100],
453
            [['max' => 100], 100],
454
            [['max' => '100'], 100],
455
        ];
456
    }
457
458
    public function getMinCheckConstraint(): array
459
    {
460
        return [
461
            ['', null],
462
            ['mincheck:3', 3],
463
            [['mincheck' => 3], 3],
464
            [['mincheck' => '3'], 3],
465
        ];
466
    }
467
468
    public function getMaxCheckConstraint(): array
469
    {
470
        return [
471
            ['', null],
472
            ['maxcheck:4', 4],
473
            [['maxcheck' => 4], 4],
474
            [['maxcheck' => '4'], 4],
475
        ];
476
    }
477
478
    public function getCheckConstraint(): array
479
    {
480
        return [
481
            ['', null, null],
482
            ['check:3,4', 3, 4],
483
            [['check' => [
484
                'min' => 5,
485
                'max' => 6,
486
            ]], 5, 6],
487
        ];
488
    }
489
490
    public function getLengthConstraint(): array
491
    {
492
        return [
493
            ['', null, null],
494
            ['length:30,40', 30, 40],
495
            [['length' => [
496
                'min' => 5,
497
                'max' => 60,
498
            ]], 5, 60],
499
        ];
500
    }
501
502
    public function getEqualToConstraint(): array
503
    {
504
        return [
505
            ['', null],
506
            ['equalto:#foo', '#foo'],
507
            [['equalto' => '.foo'], '.foo'],
508
        ];
509
    }
510
511
    public function getTypeConstraint(): array
512
    {
513
        return [
514
            ['', null],
515
            ['type:number', 'number'],
516
            [['type' => 'text'], 'text'],
517
        ];
518
    }
519
520
    public function getPkConstraint(): array
521
    {
522
        return [
523
            ['', false],
524
            ['pk', true],
525
            ['pk:true', true],
526
            ['pk:false', false],
527
            [['pk'], true],
528
            [['pk' => true], true],
529
            [['pk' => false], false],
530
        ];
531
    }
532
533
    public function getAiConstraint(): array
534
    {
535
        return [
536
            ['', false],
537
            ['ai', true],
538
            ['ai:true', true],
539
            ['ai:false', false],
540
            [['ai'], true],
541
            [['ai' => true], true],
542
            [['ai' => false], false],
543
        ];
544
    }
545
546
    public function getFkConstraint(): array
547
    {
548
        return [
549
            ['fk:table', 'table', 'id', 'name'],
550
            ['fk:table2,username', 'table2', 'id', 'username'],
551
            ['fk:table3,description,uuid', 'table3', 'uuid', 'description'],
552
            [['fk' => 'table5'], 'table5', 'id', 'name'],
553
            [['fk' => 'table6,username'], 'table6', 'id', 'username'],
554
            [['fk' => 'table7,description,uuid'], 'table7', 'uuid', 'description'],
555
        ];
556
    }
557
}
558