Completed
Push — develop ( c90588...babbbc )
by Freddie
05:37
created

testItSchemaAttributeMaxCheckConstraints()   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 getEqualToConstraint
171
     *
172
     * @param mixed $constraint
173
     * @param mixed $expected
174
     */
175
    public function testItSchemaAttributeEqualToConstraints($constraint, $expected): void
176
    {
177
        $name = 'equalTo';
178
        $dataType = 'string';
179
180
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint);
181
182
        $this->assertEquals($name, $schemaAttribute->name());
183
        $this->assertEquals($dataType, $schemaAttribute->dataType());
184
        $this->assertSame($expected, $schemaAttribute->equalTo());
185
    }
186
187
    /**
188
     * @dataProvider getTypeConstraint
189
     *
190
     * @param mixed $constraint
191
     * @param mixed $expected
192
     */
193
    public function testItSchemaAttributeTypeConstraints($constraint, $expected): void
194
    {
195
        $name = 'equalTo';
196
        $dataType = 'string';
197
198
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint);
199
200
        $this->assertEquals($name, $schemaAttribute->name());
201
        $this->assertEquals($dataType, $schemaAttribute->dataType());
202
        $this->assertSame($expected, $schemaAttribute->type());
203
    }
204
205
    public function testItSchemaAttributeConstraintsAsString(): void
206
    {
207
        $name = 'foo';
208
        $dataType = 'string';
209
        $constraints = 'required|min:1|minlength:8|max:100|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number';
210
211
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
212
213
        $this->assertEquals($name, $schemaAttribute->name());
214
        $this->assertEquals($dataType, $schemaAttribute->dataType());
215
        $this->assertIsArray($schemaAttribute->constraints());
216
        $this->assertSame(true, $schemaAttribute->isRequired());
217
        $this->assertSame(1, $schemaAttribute->min());
218
        $this->assertSame(8, $schemaAttribute->minLength());
219
        $this->assertSame(3, $schemaAttribute->minCheck());
220
        $this->assertSame(100, $schemaAttribute->max());
221
        $this->assertSame(10, $schemaAttribute->maxLength());
222
        $this->assertSame(4, $schemaAttribute->maxCheck());
223
        $this->assertSame('#bar', $schemaAttribute->equalTo());
224
        $this->assertSame('number', $schemaAttribute->type());
225
    }
226
227
    public function testItSchemaAttributeConstraintsAsArray(): void
228
    {
229
        $name = 'foo';
230
        $dataType = 'string';
231
        $constraints = [
232
            'required',
233
            'min' => 1,
234
            'minlength' => 8,
235
            'max' => 100,
236
            'maxlength' => 10,
237
            'mincheck' => 3,
238
            'maxcheck' => 4,
239
            'equalto' => '#id',
240
            'type' => 'text',
241
        ];
242
243
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
244
245
        $this->assertEquals($name, $schemaAttribute->name());
246
        $this->assertEquals($dataType, $schemaAttribute->dataType());
247
        $this->assertIsArray($schemaAttribute->constraints());
248
        $this->assertSame(true, $schemaAttribute->isRequired());
249
        $this->assertSame(1, $schemaAttribute->min());
250
        $this->assertSame(8, $schemaAttribute->minLength());
251
        $this->assertSame(3, $schemaAttribute->minCheck());
252
        $this->assertSame(100, $schemaAttribute->max());
253
        $this->assertSame(10, $schemaAttribute->maxLength());
254
        $this->assertSame(4, $schemaAttribute->maxCheck());
255
        $this->assertSame('#id', $schemaAttribute->equalTo());
256
        $this->assertSame('text', $schemaAttribute->type());
257
    }
258
259
    public function testItSchemaAttributeConstraintsAsArrayCast(): void
260
    {
261
        $name = 'foo';
262
        $dataType = 'string';
263
        $constraints = [
264
            'required',
265
            'min' => '1',
266
            'minlength' => '8',
267
            'max' => '100',
268
            'maxlength' => '10',
269
            'mincheck' => '3',
270
            'maxcheck' => '4',
271
            'equalto' => '#bar',
272
            'type' => 'number',
273
        ];
274
275
        $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints);
276
277
        $this->assertEquals($name, $schemaAttribute->name());
278
        $this->assertEquals($dataType, $schemaAttribute->dataType());
279
        $this->assertIsArray($schemaAttribute->constraints());
280
        $this->assertSame(true, $schemaAttribute->isRequired());
281
        $this->assertSame(1, $schemaAttribute->min());
282
        $this->assertSame(8, $schemaAttribute->minLength());
283
        $this->assertSame(3, $schemaAttribute->minCheck());
284
        $this->assertSame(100, $schemaAttribute->max());
285
        $this->assertSame(10, $schemaAttribute->maxLength());
286
        $this->assertSame(4, $schemaAttribute->maxCheck());
287
        $this->assertSame('#bar', $schemaAttribute->equalTo());
288
        $this->assertSame('number', $schemaAttribute->type());
289
    }
290
291
    public function getRequiredConstraint(): array
292
    {
293
        return [
294
            ['', false],
295
            ['required', true],
296
            ['required:true', true],
297
            ['required:false', false],
298
            [['required'], true],
299
            [['required' => true], true],
300
            [['required' => false], false],
301
        ];
302
    }
303
304
    public function getMinLengthConstraint(): array
305
    {
306
        return [
307
            ['', null],
308
            ['minlength:8', 8],
309
            [['minlength' => 8], 8],
310
            [['minlength' => '8'], 8],
311
        ];
312
    }
313
314
    public function getMinConstraint(): array
315
    {
316
        return [
317
            ['', null],
318
            ['min:8', 8],
319
            [['min' => 8], 8],
320
            [['min' => '8'], 8],
321
        ];
322
    }
323
324
    public function getMaxLengthConstraint(): array
325
    {
326
        return [
327
            ['', null],
328
            ['maxlength:10', 10],
329
            [['maxlength' => 10], 10],
330
            [['maxlength' => '10'], 10],
331
        ];
332
    }
333
334
    public function getMaxConstraint(): array
335
    {
336
        return [
337
            ['', null],
338
            ['max:100', 100],
339
            [['max' => 100], 100],
340
            [['max' => '100'], 100],
341
        ];
342
    }
343
344
    public function getMinCheckConstraint(): array
345
    {
346
        return [
347
            ['', null],
348
            ['mincheck:3', 3],
349
            [['mincheck' => 3], 3],
350
            [['mincheck' => '3'], 3],
351
        ];
352
    }
353
354
    public function getMaxCheckConstraint(): array
355
    {
356
        return [
357
            ['', null],
358
            ['maxcheck:4', 4],
359
            [['maxcheck' => 4], 4],
360
            [['maxcheck' => '4'], 4],
361
        ];
362
    }
363
364
    public function getEqualToConstraint(): array
365
    {
366
        return [
367
            ['', null],
368
            ['equalto:#foo', '#foo'],
369
            [['equalto' => '.foo'], '.foo'],
370
        ];
371
    }
372
373
    public function getTypeConstraint(): array
374
    {
375
        return [
376
            ['', null],
377
            ['type:number', 'number'],
378
            [['type' => 'text'], 'text'],
379
        ];
380
    }
381
}
382