Completed
Push — develop ( 1a3273...1f2dbe )
by Freddie
05:42
created

SchemaAttributeTest::getMinCheckConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
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 getFkConstraint
247
     *
248
     * @param mixed $constraint
249
     * @param mixed $fkTable
250
     * @param mixed $fkId
251
     * @param mixed $fkName
252
     */
253
    public function testItSchemaAttributeFkConstraints($constraint, $fkTable, $fkId, $fkName): void
254
    {
255
        $name = 'foreing';
256
        $dataType = 'integer';
257
258
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint);
259
260
        $this->assertEquals($name, $schemaAttribute->name());
261
        $this->assertEquals($dataType, $schemaAttribute->dataType());
262
        $this->assertTrue($schemaAttribute->isFk());
263
        $this->assertSame($fkTable, $schemaAttribute->fkTable());
264
        $this->assertSame($fkId, $schemaAttribute->fkId());
265
        $this->assertSame($fkName, $schemaAttribute->fkName());
266
    }
267
268
    public function testItSchemaAttributeConstraintsAsString(): void
269
    {
270
        $name = 'foo';
271
        $dataType = 'string';
272
        $constraints = 'required|min:1|minlength:8|max:100|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number';
273
274
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
275
276
        $this->assertEquals($name, $schemaAttribute->name());
277
        $this->assertEquals($dataType, $schemaAttribute->dataType());
278
        $this->assertIsArray($schemaAttribute->constraints());
279
        $this->assertSame(true, $schemaAttribute->isRequired());
280
        $this->assertSame(1, $schemaAttribute->min());
281
        $this->assertSame(8, $schemaAttribute->minLength());
282
        $this->assertSame(3, $schemaAttribute->minCheck());
283
        $this->assertSame(100, $schemaAttribute->max());
284
        $this->assertSame(10, $schemaAttribute->maxLength());
285
        $this->assertSame(4, $schemaAttribute->maxCheck());
286
        $this->assertSame('#bar', $schemaAttribute->equalTo());
287
        $this->assertSame('number', $schemaAttribute->type());
288
        $this->assertFalse($schemaAttribute->isFk());
289
        $this->assertNull($schemaAttribute->fkTable());
290
        $this->assertNull($schemaAttribute->fkId());
291
        $this->assertNull($schemaAttribute->fkName());
292
    }
293
294
    public function testItSchemaAttributeConstraintsAsArray(): void
295
    {
296
        $name = 'foo';
297
        $dataType = 'string';
298
        $constraints = [
299
            'required',
300
            'min' => 1,
301
            'minlength' => 8,
302
            'max' => 100,
303
            'maxlength' => 10,
304
            'mincheck' => 3,
305
            'maxcheck' => 4,
306
            'equalto' => '#id',
307
            'type' => 'text',
308
            'fk' => 'table,name,id',
309
        ];
310
311
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
312
313
        $this->assertEquals($name, $schemaAttribute->name());
314
        $this->assertEquals($dataType, $schemaAttribute->dataType());
315
        $this->assertIsArray($schemaAttribute->constraints());
316
        $this->assertSame(true, $schemaAttribute->isRequired());
317
        $this->assertSame(1, $schemaAttribute->min());
318
        $this->assertSame(8, $schemaAttribute->minLength());
319
        $this->assertSame(3, $schemaAttribute->minCheck());
320
        $this->assertSame(100, $schemaAttribute->max());
321
        $this->assertSame(10, $schemaAttribute->maxLength());
322
        $this->assertSame(4, $schemaAttribute->maxCheck());
323
        $this->assertSame('#id', $schemaAttribute->equalTo());
324
        $this->assertSame('text', $schemaAttribute->type());
325
        $this->assertTrue($schemaAttribute->isFk());
326
        $this->assertSame('table', $schemaAttribute->fkTable());
327
        $this->assertSame('id', $schemaAttribute->fkId());
328
        $this->assertSame('name', $schemaAttribute->fkName());
329
    }
330
331
    public function testItSchemaAttributeConstraintsAsArrayCast(): void
332
    {
333
        $name = 'foo';
334
        $dataType = 'string';
335
        $constraints = [
336
            'required',
337
            'min' => '1',
338
            'minlength' => '8',
339
            'max' => '100',
340
            'maxlength' => '10',
341
            'mincheck' => '3',
342
            'maxcheck' => '4',
343
            'equalto' => '#bar',
344
            'type' => 'number',
345
        ];
346
347
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
348
349
        $this->assertEquals($name, $schemaAttribute->name());
350
        $this->assertEquals($dataType, $schemaAttribute->dataType());
351
        $this->assertIsArray($schemaAttribute->constraints());
352
        $this->assertSame(true, $schemaAttribute->isRequired());
353
        $this->assertSame(1, $schemaAttribute->min());
354
        $this->assertSame(8, $schemaAttribute->minLength());
355
        $this->assertSame(3, $schemaAttribute->minCheck());
356
        $this->assertSame(100, $schemaAttribute->max());
357
        $this->assertSame(10, $schemaAttribute->maxLength());
358
        $this->assertSame(4, $schemaAttribute->maxCheck());
359
        $this->assertSame('#bar', $schemaAttribute->equalTo());
360
        $this->assertSame('number', $schemaAttribute->type());
361
    }
362
363
    public function getRequiredConstraint(): array
364
    {
365
        return [
366
            ['', false],
367
            ['required', true],
368
            ['required:true', true],
369
            ['required:false', false],
370
            [['required'], true],
371
            [['required' => true], true],
372
            [['required' => false], false],
373
        ];
374
    }
375
376
    public function getMinLengthConstraint(): array
377
    {
378
        return [
379
            ['', null],
380
            ['minlength:8', 8],
381
            [['minlength' => 8], 8],
382
            [['minlength' => '8'], 8],
383
        ];
384
    }
385
386
    public function getMinConstraint(): array
387
    {
388
        return [
389
            ['', null],
390
            ['min:8', 8],
391
            [['min' => 8], 8],
392
            [['min' => '8'], 8],
393
        ];
394
    }
395
396
    public function getMaxLengthConstraint(): array
397
    {
398
        return [
399
            ['', null],
400
            ['maxlength:10', 10],
401
            [['maxlength' => 10], 10],
402
            [['maxlength' => '10'], 10],
403
        ];
404
    }
405
406
    public function getMaxConstraint(): array
407
    {
408
        return [
409
            ['', null],
410
            ['max:100', 100],
411
            [['max' => 100], 100],
412
            [['max' => '100'], 100],
413
        ];
414
    }
415
416
    public function getMinCheckConstraint(): array
417
    {
418
        return [
419
            ['', null],
420
            ['mincheck:3', 3],
421
            [['mincheck' => 3], 3],
422
            [['mincheck' => '3'], 3],
423
        ];
424
    }
425
426
    public function getMaxCheckConstraint(): array
427
    {
428
        return [
429
            ['', null],
430
            ['maxcheck:4', 4],
431
            [['maxcheck' => 4], 4],
432
            [['maxcheck' => '4'], 4],
433
        ];
434
    }
435
436
    public function getCheckConstraint(): array
437
    {
438
        return [
439
            ['', null, null],
440
            ['check:3,4', 3, 4],
441
            [['check' => [
442
                'min' => 5,
443
                'max' => 6,
444
            ]], 5, 6],
445
        ];
446
    }
447
448
    public function getLengthConstraint(): array
449
    {
450
        return [
451
            ['', null, null],
452
            ['length:30,40', 30, 40],
453
            [['length' => [
454
                'min' => 5,
455
                'max' => 60,
456
            ]], 5, 60],
457
        ];
458
    }
459
460
    public function getEqualToConstraint(): array
461
    {
462
        return [
463
            ['', null],
464
            ['equalto:#foo', '#foo'],
465
            [['equalto' => '.foo'], '.foo'],
466
        ];
467
    }
468
469
    public function getTypeConstraint(): array
470
    {
471
        return [
472
            ['', null],
473
            ['type:number', 'number'],
474
            [['type' => 'text'], 'text'],
475
        ];
476
    }
477
478
    public function getFkConstraint(): array
479
    {
480
        return [
481
            ['fk:table', 'table', 'id', 'name'],
482
            ['fk:table2,username', 'table2', 'id', 'username'],
483
            ['fk:table3,description,uuid', 'table3', 'uuid', 'description'],
484
            [['fk' => 'table5'], 'table5', 'id', 'name'],
485
            [['fk' => 'table6,username'], 'table6', 'id', 'username'],
486
            [['fk' => 'table7,description,uuid'], 'table7', 'uuid', 'description'],
487
        ];
488
    }
489
}
490