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

testItPropertyRequireThrownException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
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 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($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($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($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($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($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($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($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($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
            ['bool'],
206
            ['barchar'],
207
            ['interger'],
208
            ['int'],
209
        ];
210
    }
211
212
    public function propertyDataTypeValid(): array
213
    {
214
        return \array_map(function ($dataType) {
215
            return [$dataType];
216
        }, PropertyDataTypeValidator::ALLOWED_DATATYPES);
217
    }
218
219
    public function propertyTypeNotValid(): array
220
    {
221
        return [
222
            ['unknow'],
223
            ['textt'],
224
            ['text area'],
225
            ['int'],
226
            [null],
227
            [[]],
228
            [1],
229
        ];
230
    }
231
232
    public function propertyTypeValid(): array
233
    {
234
        return \array_map(function ($dataType) {
235
            return [$dataType];
236
        }, PropertyTypeValidator::ALLOWED_TYPES);
237
    }
238
239
    public function propertyConstraintsNotValid(): array
240
    {
241
        return [
242
            [['']],
243
            [['required']],
244
            [['_REQUIRED']],
245
            [['REQUIRED']],
246
            [['Required']],
247
            [['required' => null]],
248
            [['required' => '']],
249
            [[1]],
250
            [['minlength' => null]],
251
            [['maxlength' => []]],
252
            [['mincheck' => -1]],
253
            [['maxcheck' => 0]],
254
            [['min' => '']],
255
            [['max' => 'null']],
256
            [['equalto' => null]],
257
            [['type' => 'unknow']],
258
            [['check' => [
259
                'min' => \rand(5, 10),
260
            ]]],
261
            [['check' => [
262
                'min' => \rand(5, 10),
263
                'max' => \rand(0, 4),
264
            ]]],
265
            [['length' => [
266
                'max' => \rand(0, 5),
267
            ]]],
268
            [['length' => [
269
                'min' => null,
270
                'max' => \rand(0, 5),
271
            ]]],
272
            [['length' => [
273
                'min' => \rand(5, 10),
274
                'max' => \rand(0, 4),
275
            ]]],
276
            [['length' => [
277
                'min' => \rand(0, 5),
278
                'max' => null,
279
            ]]],
280
            [['length' => [
281
                'min' => \rand(5, 10),
282
            ]]],
283
        ];
284
    }
285
286
    public function propertyConstraintsValid(): array
287
    {
288
        return [
289
            [[]],
290
            [['required' => true]],
291
            [['required' => false]],
292
            [['required' => 'true']],
293
            [['required' => 'false']],
294
            [['minlength' => 0]],
295
            [['minlength' => \rand(0, 9)]],
296
            [['maxlength' => \rand(1, 9)]],
297
            [['mincheck' => 0]],
298
            [['mincheck' => \rand(1, 9)]],
299
            [['maxcheck' => \rand(1, 9)]],
300
            [['min' => 0]],
301
            [['min' => \rand(1, 9)]],
302
            [['max' => \rand(1, 9)]],
303
            [['equalto' => 'foo']],
304
            [['type' => 'text']],
305
            [['check' => [
306
                'min' => \rand(0, 4),
307
                'max' => \rand(5, 10),
308
            ]]],
309
            [['length' => [
310
                'min' => \rand(0, 4),
311
                'max' => \rand(5, 10),
312
            ]]],
313
        ];
314
    }
315
}
316