Completed
Push — develop ( 71868c...88717d )
by Freddie
03:24
created

propertyDataTypeNotValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
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\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 testItPropertyUnknowThrownException(): void
22
    {
23
        $this->expectException(InvalidSchemaAttributeException::class);
24
        $this->expectExceptionMessage('unknow');
25
26
        $validation = new SchemaAttributeValidation([
27
            'UnknowProperty' => 'Test',
28
        ]);
29
30
        $validation->validate();
31
    }
32
33
    /**
34
     * @dataProvider propertyNameNotValid
35
     */
36
    public function testItPropertyNameNotValidThrownException($name): void
37
    {
38
        $this->expectException(InvalidSchemaAttributeException::class);
39
        $this->expectExceptionMessage('Name:');
40
41
        $validation = new SchemaAttributeValidation([
42
            Keyword::NAME     => $name,
43
            Keyword::DATATYPE => 'string',
44
        ]);
45
46
        $validation->validate();
47
    }
48
49
    /**
50
     * @dataProvider propertyNameValid
51
     */
52
    public function testItPropertyNameOk($name): void
53
    {
54
        $validation = new SchemaAttributeValidation([
55
            Keyword::NAME     => $name,
56
            Keyword::DATATYPE => 'string',
57
        ]);
58
59
        $validation->validate();
60
61
        $this->assertTrue(true);
62
    }
63
64
    /**
65
     * @dataProvider propertyDataTypeNotValid
66
     */
67
    public function testItPropertyDataTypeNotValidThrownException($dataType): void
68
    {
69
        $this->expectException(InvalidSchemaAttributeException::class);
70
        $this->expectExceptionMessage('DataType:');
71
72
        $validation = new SchemaAttributeValidation([
73
            Keyword::NAME     => 'foo',
74
            Keyword::DATATYPE => $dataType,
75
        ]);
76
77
        $validation->validate();
78
    }
79
80
    /**
81
     * @dataProvider propertyDataTypeValid
82
     */
83
    public function testItPropertyDataTypeOk($dataType): void
84
    {
85
        $validation = new SchemaAttributeValidation([
86
            Keyword::NAME     => 'foo',
87
            Keyword::DATATYPE => $dataType,
88
        ]);
89
90
        $validation->validate();
91
92
        $this->assertTrue(true);
93
    }
94
95
    /**
96
     * @dataProvider propertyDataTypeNotValid
97
     */
98
    public function testItPropertyTypeNotValidThrownException($type): void
99
    {
100
        $this->expectException(InvalidSchemaAttributeException::class);
101
        $this->expectExceptionMessage('Type:');
102
103
        $validation = new SchemaAttributeValidation([
104
            Keyword::NAME     => 'foo',
105
            Keyword::DATATYPE => 'string',
106
            Keyword::TYPE     => $type,
107
        ]);
108
109
        $validation->validate();
110
    }
111
112
    /**
113
     * @dataProvider propertyTypeValid
114
     */
115
    public function testItPropertyTypeOk($type): void
116
    {
117
        $validation = new SchemaAttributeValidation([
118
            Keyword::NAME     => 'foo',
119
            Keyword::DATATYPE => 'string',
120
            Keyword::TYPE     => $type,
121
        ]);
122
123
        $validation->validate();
124
125
        $this->assertTrue(true);
126
    }
127
128
    /**
129
     * @dataProvider propertyConstraintsNotValid
130
     */
131
    public function testItPropertyConstraintsNotValidThrownException($constraints): void
132
    {
133
        $this->expectException(InvalidSchemaAttributeException::class);
134
        $this->expectExceptionMessage('Constraints:');
135
136
        $validation = new SchemaAttributeValidation([
137
            Keyword::NAME        => 'foo',
138
            Keyword::DATATYPE    => 'string',
139
            Keyword::CONSTRAINTS => $constraints,
140
        ]);
141
142
        $validation->validate();
143
    }
144
145
    /**
146
     * @dataProvider propertyConstraintsValid
147
     */
148
    public function testItPropertyConstraintsOk($constraints): void
149
    {
150
        $validation = new SchemaAttributeValidation([
151
            Keyword::NAME        => 'foo',
152
            Keyword::DATATYPE    => 'string',
153
            Keyword::CONSTRAINTS => $constraints,
154
        ]);
155
156
        $validation->validate();
157
158
        $this->assertTrue(true);
159
    }
160
161
    public function propertyNameNotValid(): array
162
    {
163
        return [
164
            ['#Name'],
165
            ['1Name'],
166
            ['Name$'],
167
            [\str_repeat('N', 65)],
168
            [''],
169
        ];
170
    }
171
172
    public function propertyNameValid(): array
173
    {
174
        return [
175
            ['Name'],
176
            ['N123'],
177
            ['Name_Test'],
178
            ['name_test'],
179
            ['_name'],
180
            [\str_repeat('N', 64)],
181
            ['N'],
182
        ];
183
    }
184
185
    public function propertyDataTypeNotValid(): array
186
    {
187
        return [
188
            ['unknow'],
189
            ['bool'],
190
            ['barchar'],
191
            ['interger'],
192
            ['int'],
193
            [null],
194
            [[]],
195
            [1],
196
        ];
197
    }
198
199
    public function propertyDataTypeValid(): array
200
    {
201
        return \array_map(function ($dataType) {
202
            return [$dataType];
203
        }, PropertyDataTypeValidator::ALLOWED_DATATYPES);
204
    }
205
206
    public function propertyTypeNotValid(): array
207
    {
208
        return [
209
            ['unknow'],
210
            ['textt'],
211
            ['text area'],
212
            ['int'],
213
            [null],
214
            [[]],
215
            [1],
216
        ];
217
    }
218
219
    public function propertyTypeValid(): array
220
    {
221
        return \array_map(function ($dataType) {
222
            return [$dataType];
223
        }, PropertyTypeValidator::ALLOWED_TYPES);
224
    }
225
226
    public function propertyConstraintsNotValid(): array
227
    {
228
        return [
229
            ['_REQUIRED'],
230
            ['REQUIRED'],
231
            ['Required'],
232
            [1],
233
            [['minlength' => null]],
234
            [['maxlength' => []]],
235
            [['mincheck' => -1]],
236
            [['maxcheck' => 0]],
237
            [['min' => '']],
238
            [['max' => 'null']],
239
            [['equalto' => null]],
240
            [['type' => 'unknow']],
241
            [['check' => [
242
                'min' => \rand(5, 10),
243
            ]]],
244
            [['check' => [
245
                'min' => \rand(5, 10),
246
                'max' => \rand(0, 4),
247
            ]]],
248
            [['length' => [
249
                'max' => \rand(0, 5),
250
            ]]],
251
            [['length' => [
252
                'min' => \rand(5, 10),
253
                'max' => \rand(0, 5),
254
            ]]],
255
            [['length' => [
256
                'min' => \rand(5, 10),
257
            ], 'type' => 'text']],
258
        ];
259
    }
260
261
    public function propertyConstraintsValid(): array
262
    {
263
        return [
264
            [null],
265
            [''],
266
            [[]],
267
            ['required'],
268
            ['required|min:8'], // Using |
269
            ["['required']"], // Array syntax
270
            ["['required','min'=>8]"], // Array syntax multiple
271
            ['["required"]'], // JSON simple
272
            ['{"required":true}'], // JSON complex
273
            ['{"required":true,"min":8}'], // JSON complex multiple
274
            [['required']],
275
            [['required' => true]],
276
            [['required' => false]],
277
            [['minlength' => 0]],
278
            [['minlength' => \rand(0, 9)]],
279
            [['maxlength' => \rand(1, 9)]],
280
            [['mincheck' => 0]],
281
            [['mincheck' => \rand(1, 9)]],
282
            [['maxcheck' => \rand(1, 9)]],
283
            [['min' => 0]],
284
            [['min' => \rand(1, 9)]],
285
            [['max' => \rand(1, 9)]],
286
            [['equalto' => 'foo']],
287
            [['type' => 'text']],
288
            [['check' => [
289
                'min' => \rand(0, 4),
290
                'max' => \rand(5, 10),
291
            ]]],
292
            [['length' => [
293
                'min' => \rand(0, 4),
294
                'max' => \rand(5, 10),
295
            ]]],
296
        ];
297
    }
298
}
299