Passed
Push — develop ( 4eb6c9...b59003 )
by Freddie
05:08
created

propertyDataTypeValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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\Unit\Validations;
11
12
use FlexPHP\Schema\Constants\Keyword;
13
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
14
use FlexPHP\Schema\Tests\TestCase;
15
use FlexPHP\Schema\Validations\SchemaAttributeValidation;
16
use FlexPHP\Schema\Validators\PropertyDataTypeValidator;
17
use FlexPHP\Schema\Validators\PropertyTypeValidator;
18
19
class SchemaAttributeValidationTest extends TestCase
20
{
21
    public function testItPropertyRequireThrownException(): void
22
    {
23
        $this->expectException(InvalidSchemaAttributeException::class);
24
25
        $validation = new SchemaAttributeValidation([
26
            Keyword::NAME => 'Test',
27
        ]);
28
29
        $validation->validate();
30
    }
31
32
    public function testItPropertyUnknowThrownException(): void
33
    {
34
        $this->expectException(InvalidSchemaAttributeException::class);
35
        $this->expectExceptionMessage('unknow');
36
37
        $validation = new SchemaAttributeValidation([
38
            'UnknowProperty' => 'Test',
39
        ]);
40
41
        $validation->validate();
42
    }
43
44
    /**
45
     * @dataProvider propertyNameNotValid
46
     */
47
    public function testItPropertyNameNotValidThrownException(string $name): void
48
    {
49
        $this->expectException(InvalidSchemaAttributeException::class);
50
        $this->expectExceptionMessage('Name:');
51
52
        $validation = new SchemaAttributeValidation([
53
            Keyword::NAME => $name,
54
            Keyword::DATATYPE => 'string',
55
        ]);
56
57
        $validation->validate();
58
    }
59
60
    /**
61
     * @dataProvider propertyNameValid
62
     */
63
    public function testItPropertyNameOk(string $name): void
64
    {
65
        $validation = new SchemaAttributeValidation([
66
            Keyword::NAME => $name,
67
            Keyword::DATATYPE => 'string',
68
        ]);
69
70
        $validation->validate();
71
72
        $this->assertTrue(true);
73
    }
74
75
    /**
76
     * @dataProvider propertyDataTypeNotValid
77
     */
78
    public function testItPropertyDataTypeNotValidThrownException(string $dataType): void
79
    {
80
        $this->expectException(InvalidSchemaAttributeException::class);
81
        $this->expectExceptionMessage('DataType:');
82
83
        $validation = new SchemaAttributeValidation([
84
            Keyword::NAME => 'foo',
85
            Keyword::DATATYPE => $dataType,
86
        ]);
87
88
        $validation->validate();
89
    }
90
91
    /**
92
     * @dataProvider propertyDataTypeValid
93
     */
94
    public function testItPropertyDataTypeOk(string $dataType): void
95
    {
96
        $validation = new SchemaAttributeValidation([
97
            Keyword::NAME => 'foo',
98
            Keyword::DATATYPE => $dataType,
99
        ]);
100
101
        $validation->validate();
102
103
        $this->assertTrue(true);
104
    }
105
106
    /**
107
     * @dataProvider propertyDataTypeNotValid
108
     */
109
    public function testItPropertyTypeNotValidThrownException(string $type): void
110
    {
111
        $this->expectException(InvalidSchemaAttributeException::class);
112
        $this->expectExceptionMessage('Type:');
113
114
        $validation = new SchemaAttributeValidation([
115
            Keyword::NAME => 'foo',
116
            Keyword::DATATYPE => 'string',
117
            Keyword::TYPE => $type,
118
        ]);
119
120
        $validation->validate();
121
    }
122
123
    /**
124
     * @dataProvider propertyTypeValid
125
     */
126
    public function testItPropertyTypeOk(string $type): void
127
    {
128
        $validation = new SchemaAttributeValidation([
129
            Keyword::NAME => 'foo',
130
            Keyword::DATATYPE => 'string',
131
            Keyword::TYPE => $type,
132
        ]);
133
134
        $validation->validate();
135
136
        $this->assertTrue(true);
137
    }
138
139
    /**
140
     * @dataProvider propertyConstraintsNotValid
141
     */
142
    public function testItPropertyConstraintsNotValidThrownException(array $constraints): void
143
    {
144
        $this->expectException(InvalidSchemaAttributeException::class);
145
        $this->expectExceptionMessage('Constraints:');
146
147
        $validation = new SchemaAttributeValidation([
148
            Keyword::NAME => 'foo',
149
            Keyword::DATATYPE => 'string',
150
            Keyword::CONSTRAINTS => $constraints,
151
        ]);
152
153
        $validation->validate();
154
    }
155
156
    /**
157
     * @dataProvider propertyConstraintsValid
158
     */
159
    public function testItPropertyConstraintsOk(array $constraints): void
160
    {
161
        $validation = new SchemaAttributeValidation([
162
            Keyword::NAME => 'foo',
163
            Keyword::DATATYPE => 'string',
164
            Keyword::CONSTRAINTS => $constraints,
165
        ]);
166
167
        $validation->validate();
168
169
        $this->assertTrue(true);
170
    }
171
172
    public function propertyNameNotValid(): array
173
    {
174
        return [
175
            [''],
176
            [' '],
177
            ['_'],
178
            ['_name'],
179
            ['name_'],
180
            ['1Name'],
181
            ['$Name'],
182
            ['Na$me'],
183
            ['Name$'],
184
            [\str_repeat('N', 65)],
185
        ];
186
    }
187
188
    public function propertyNameValid(): array
189
    {
190
        return [
191
            ['n'],
192
            ['N'],
193
            ['Name'],
194
            ['N123'],
195
            ['Name_Test'],
196
            ['name_test'],
197
            [\str_repeat('N', 64)],
198
        ];
199
    }
200
201
    public function propertyDataTypeNotValid(): array
202
    {
203
        return [
204
            ['unknow'],
205
            ['barchar'],
206
            ['interger'],
207
            ['int'],
208
        ];
209
    }
210
211
    public function propertyDataTypeValid(): array
212
    {
213
        return \array_map(function ($dataType) {
214
            return [$dataType];
215
        }, PropertyDataTypeValidator::ALLOWED_DATATYPES);
216
    }
217
218
    public function propertyTypeNotValid(): array
219
    {
220
        return [
221
            ['unknow'],
222
            ['textt'],
223
            ['text area'],
224
            ['int'],
225
            [null],
226
            [[]],
227
            [1],
228
        ];
229
    }
230
231
    public function propertyTypeValid(): array
232
    {
233
        return \array_map(function ($dataType) {
234
            return [$dataType];
235
        }, PropertyTypeValidator::ALLOWED_TYPES);
236
    }
237
238
    public function propertyConstraintsNotValid(): array
239
    {
240
        return [
241
            [['']],
242
            [['required']],
243
            [['_REQUIRED']],
244
            [['REQUIRED']],
245
            [['Required']],
246
            [['required' => null]],
247
            [['required' => '']],
248
            [[1]],
249
            [['minlength' => null]],
250
            [['maxlength' => []]],
251
            [['mincheck' => -1]],
252
            [['maxcheck' => 0]],
253
            [['min' => '']],
254
            [['max' => 'null']],
255
            [['equalto' => null]],
256
            [['type' => 'unknow']],
257
            [['check' => [
258
                'min' => \rand(5, 10),
259
            ]]],
260
            [['check' => [
261
                'min' => \rand(5, 10),
262
                'max' => \rand(0, 4),
263
            ]]],
264
            [['length' => [
265
                'max' => \rand(0, 5),
266
            ]]],
267
            [['length' => [
268
                'min' => null,
269
                'max' => \rand(0, 5),
270
            ]]],
271
            [['length' => [
272
                'min' => \rand(5, 10),
273
                'max' => \rand(0, 4),
274
            ]]],
275
            [['length' => [
276
                'min' => \rand(0, 5),
277
                'max' => null,
278
            ]]],
279
            [['length' => [
280
                'min' => \rand(5, 10),
281
            ]]],
282
            [['pk' => null]],
283
            [['pk' => '']],
284
            [['fk' => null]],
285
            [['fk' => '']],
286
            [['fk' => 'table.name']],
287
            [['fk' => 'table,name.id']],
288
            [['fk' => 'table,name,id.dot']],
289
            [['ai' => null]],
290
            [['ai' => '']],
291
            [['ca' => null]],
292
            [['ca' => '']],
293
            [['ua' => null]],
294
            [['ua' => '']],
295
            [['cb' => null]],
296
            [['cb' => '']],
297
            [['ub' => null]],
298
            [['ub' => '']],
299
        ];
300
    }
301
302
    public function propertyConstraintsValid(): array
303
    {
304
        return [
305
            [[]],
306
            [['required' => true]],
307
            [['required' => false]],
308
            [['required' => 'true']],
309
            [['required' => 'false']],
310
            [['minlength' => 0]],
311
            [['minlength' => \rand(0, 9)]],
312
            [['maxlength' => \rand(1, 9)]],
313
            [['mincheck' => 0]],
314
            [['mincheck' => \rand(1, 9)]],
315
            [['maxcheck' => \rand(1, 9)]],
316
            [['min' => 0]],
317
            [['min' => \rand(1, 9)]],
318
            [['max' => \rand(1, 9)]],
319
            [['equalto' => 'foo']],
320
            [['type' => 'text']],
321
            [['check' => [
322
                'min' => \rand(0, 4),
323
                'max' => \rand(5, 10),
324
            ]]],
325
            [['length' => [
326
                'min' => \rand(0, 4),
327
                'max' => \rand(5, 10),
328
            ]]],
329
            [['pk' => true]],
330
            [['pk' => false]],
331
            [['pk' => 'true']],
332
            [['pk' => 'false']],
333
            [['fk' => 'table']],
334
            [['fk' => 'table,name']],
335
            [['fk' => 'table,name,id']],
336
            [['ai' => true]],
337
            [['ai' => false]],
338
            [['ai' => 'true']],
339
            [['ai' => 'false']],
340
            [['ca' => true]],
341
            [['ca' => false]],
342
            [['ca' => 'true']],
343
            [['ca' => 'false']],
344
            [['ua' => true]],
345
            [['ua' => false]],
346
            [['ua' => 'true']],
347
            [['ua' => 'false']],
348
            [['cb' => true]],
349
            [['cb' => false]],
350
            [['cb' => 'true']],
351
            [['cb' => 'false']],
352
            [['ub' => true]],
353
            [['ub' => false]],
354
            [['ub' => 'true']],
355
            [['ub' => 'false']],
356
        ];
357
    }
358
}
359