Passed
Push — develop ( babbbc...8dad66 )
by Freddie
05:46
created

testItSchemaAttributeMaxConstraints()   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
    public function testItSchemaAttributeConstraintsAsString(): void
246
    {
247
        $name = 'foo';
248
        $dataType = 'string';
249
        $constraints = 'required|min:1|minlength:8|max:100|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number';
250
251
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
252
253
        $this->assertEquals($name, $schemaAttribute->name());
254
        $this->assertEquals($dataType, $schemaAttribute->dataType());
255
        $this->assertIsArray($schemaAttribute->constraints());
256
        $this->assertSame(true, $schemaAttribute->isRequired());
257
        $this->assertSame(1, $schemaAttribute->min());
258
        $this->assertSame(8, $schemaAttribute->minLength());
259
        $this->assertSame(3, $schemaAttribute->minCheck());
260
        $this->assertSame(100, $schemaAttribute->max());
261
        $this->assertSame(10, $schemaAttribute->maxLength());
262
        $this->assertSame(4, $schemaAttribute->maxCheck());
263
        $this->assertSame('#bar', $schemaAttribute->equalTo());
264
        $this->assertSame('number', $schemaAttribute->type());
265
    }
266
267
    public function testItSchemaAttributeConstraintsAsArray(): void
268
    {
269
        $name = 'foo';
270
        $dataType = 'string';
271
        $constraints = [
272
            'required',
273
            'min' => 1,
274
            'minlength' => 8,
275
            'max' => 100,
276
            'maxlength' => 10,
277
            'mincheck' => 3,
278
            'maxcheck' => 4,
279
            'equalto' => '#id',
280
            'type' => 'text',
281
        ];
282
283
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
284
285
        $this->assertEquals($name, $schemaAttribute->name());
286
        $this->assertEquals($dataType, $schemaAttribute->dataType());
287
        $this->assertIsArray($schemaAttribute->constraints());
288
        $this->assertSame(true, $schemaAttribute->isRequired());
289
        $this->assertSame(1, $schemaAttribute->min());
290
        $this->assertSame(8, $schemaAttribute->minLength());
291
        $this->assertSame(3, $schemaAttribute->minCheck());
292
        $this->assertSame(100, $schemaAttribute->max());
293
        $this->assertSame(10, $schemaAttribute->maxLength());
294
        $this->assertSame(4, $schemaAttribute->maxCheck());
295
        $this->assertSame('#id', $schemaAttribute->equalTo());
296
        $this->assertSame('text', $schemaAttribute->type());
297
    }
298
299
    public function testItSchemaAttributeConstraintsAsArrayCast(): void
300
    {
301
        $name = 'foo';
302
        $dataType = 'string';
303
        $constraints = [
304
            'required',
305
            'min' => '1',
306
            'minlength' => '8',
307
            'max' => '100',
308
            'maxlength' => '10',
309
            'mincheck' => '3',
310
            'maxcheck' => '4',
311
            'equalto' => '#bar',
312
            'type' => 'number',
313
        ];
314
315
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
316
317
        $this->assertEquals($name, $schemaAttribute->name());
318
        $this->assertEquals($dataType, $schemaAttribute->dataType());
319
        $this->assertIsArray($schemaAttribute->constraints());
320
        $this->assertSame(true, $schemaAttribute->isRequired());
321
        $this->assertSame(1, $schemaAttribute->min());
322
        $this->assertSame(8, $schemaAttribute->minLength());
323
        $this->assertSame(3, $schemaAttribute->minCheck());
324
        $this->assertSame(100, $schemaAttribute->max());
325
        $this->assertSame(10, $schemaAttribute->maxLength());
326
        $this->assertSame(4, $schemaAttribute->maxCheck());
327
        $this->assertSame('#bar', $schemaAttribute->equalTo());
328
        $this->assertSame('number', $schemaAttribute->type());
329
    }
330
331
    public function getRequiredConstraint(): array
332
    {
333
        return [
334
            ['', false],
335
            ['required', true],
336
            ['required:true', true],
337
            ['required:false', false],
338
            [['required'], true],
339
            [['required' => true], true],
340
            [['required' => false], false],
341
        ];
342
    }
343
344
    public function getMinLengthConstraint(): array
345
    {
346
        return [
347
            ['', null],
348
            ['minlength:8', 8],
349
            [['minlength' => 8], 8],
350
            [['minlength' => '8'], 8],
351
        ];
352
    }
353
354
    public function getMinConstraint(): array
355
    {
356
        return [
357
            ['', null],
358
            ['min:8', 8],
359
            [['min' => 8], 8],
360
            [['min' => '8'], 8],
361
        ];
362
    }
363
364
    public function getMaxLengthConstraint(): array
365
    {
366
        return [
367
            ['', null],
368
            ['maxlength:10', 10],
369
            [['maxlength' => 10], 10],
370
            [['maxlength' => '10'], 10],
371
        ];
372
    }
373
374
    public function getMaxConstraint(): array
375
    {
376
        return [
377
            ['', null],
378
            ['max:100', 100],
379
            [['max' => 100], 100],
380
            [['max' => '100'], 100],
381
        ];
382
    }
383
384
    public function getMinCheckConstraint(): array
385
    {
386
        return [
387
            ['', null],
388
            ['mincheck:3', 3],
389
            [['mincheck' => 3], 3],
390
            [['mincheck' => '3'], 3],
391
        ];
392
    }
393
394
    public function getMaxCheckConstraint(): array
395
    {
396
        return [
397
            ['', null],
398
            ['maxcheck:4', 4],
399
            [['maxcheck' => 4], 4],
400
            [['maxcheck' => '4'], 4],
401
        ];
402
    }
403
404
    public function getCheckConstraint(): array
405
    {
406
        return [
407
            ['', null, null],
408
            ['check:3,4', 3, 4],
409
            [['check' => [
410
                'min' => 5,
411
                'max' => 6,
412
            ]], 5, 6],
413
        ];
414
    }
415
416
    public function getLengthConstraint(): array
417
    {
418
        return [
419
            ['', null, null],
420
            ['length:30,40', 30, 40],
421
            [['length' => [
422
                'min' => 5,
423
                'max' => 60,
424
            ]], 5, 60],
425
        ];
426
    }
427
428
    public function getEqualToConstraint(): array
429
    {
430
        return [
431
            ['', null],
432
            ['equalto:#foo', '#foo'],
433
            [['equalto' => '.foo'], '.foo'],
434
        ];
435
    }
436
437
    public function getTypeConstraint(): array
438
    {
439
        return [
440
            ['', null],
441
            ['type:number', 'number'],
442
            [['type' => 'text'], 'text'],
443
        ];
444
    }
445
}
446