Test Failed
Push — develop ( eeff2c...6435cb )
by Freddie
05:45
created

SchemaTest::getTitleInvalid()   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;
11
12
use FlexPHP\Schema\Constants\Action;
13
use FlexPHP\Schema\Constants\Keyword;
14
use FlexPHP\Schema\Schema;
15
use FlexPHP\Schema\SchemaAttribute;
16
use FlexPHP\Schema\SchemaAttributeInterface;
17
use FlexPHP\Schema\Tests\TestCase;
18
use Symfony\Component\Yaml\Yaml;
19
20
class SchemaTest extends TestCase
21
{
22
    /**
23
     * @dataProvider getNameInvalid
24
     *
25
     * @param mixed $name
26
     */
27
    public function testItSchemaNameInvalidThrowException($name): void
28
    {
29
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
30
        $this->expectExceptionMessage('name is');
31
32
        new Schema($name, 'title', []);
33
    }
34
35
    /**
36
     * @dataProvider getNameErrorInvalid
37
     *
38
     * @param mixed $name
39
     */
40
    public function testItSchemaNameErrorThrowException($name): void
41
    {
42
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
43
        $this->expectExceptionMessage('only accept');
44
45
        new Schema($name, 'title', []);
46
    }
47
48
    /**
49
     * @dataProvider getTitleInvalid
50
     *
51
     * @param mixed $title
52
     */
53
    public function testItSchemaTitleInvalidThrowException($title): void
54
    {
55
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
56
        $this->expectExceptionMessage(':title');
57
58
        new Schema('name', $title, []);
59
    }
60
61
    /**
62
     * @dataProvider getActionInvalid
63
     *
64
     * @param mixed $action
65
     */
66
    public function testItSchemaActionInvalidThrowException($action): void
67
    {
68
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
69
        $this->expectExceptionMessage(':action');
70
71
        new Schema('name', 'title', [], '', '', [
72
            $action,
73
        ]);
74
    }
75
76
    public function testItSchemaAttributesEmptyOk(): void
77
    {
78
        new Schema('name', 'title', []);
79
80
        $this->assertTrue(true);
81
    }
82
83
    public function testItSchemaActionsEmptySetDefaultsOk(): void
84
    {
85
        $schema = new Schema('name', 'title', []);
86
87
        $this->assertTrue($schema->hasAction(Action::INDEX));
88
        $this->assertTrue($schema->hasAction(Action::CREATE));
89
        $this->assertTrue($schema->hasAction(Action::READ));
90
        $this->assertTrue($schema->hasAction(Action::UPDATE));
91
        $this->assertTrue($schema->hasAction(Action::DELETE));
92
93
        $this->assertFalse($schema->hasAction(Action::ALL));
94
        $this->assertFalse($schema->hasAction(Action::FILTER));
95
    }
96
97
    /**
98
     * @dataProvider getAction
99
     */
100
    public function testItSchemaActionsSetupOk(string $action, string $noActions): void
101
    {
102
        $schema = new Schema('name', 'title', [], '', '', [$action]);
103
104
        $this->assertTrue($schema->hasAction($action));
105
106
        foreach (str_split($noActions) as $noAction) {
107
            $this->assertFalse($schema->hasAction($noAction));
108
        }
109
    }
110
111
    public function testItSchemaSetOk(): void
112
    {
113
        $name = 'name';
114
        $title = 'title';
115
        $attributes = [
116
            [
117
                Keyword::NAME => 'foo',
118
                Keyword::DATATYPE => 'string',
119
                Keyword::CONSTRAINTS => 'required:true',
120
            ],
121
            [
122
                Keyword::NAME => 'bar',
123
                Keyword::DATATYPE => 'integer',
124
                Keyword::CONSTRAINTS => 'required:false|min:8|max:10',
125
            ],
126
        ];
127
128
        $schema = new Schema($name, $title, $attributes);
129
130
        $this->assertEquals($name, $schema->name());
131
        $this->assertEquals($title, $schema->title());
132
        $this->assertIsArray($schema->attributes());
133
        $this->assertSame(2, \count($schema->attributes()));
134
        $this->assertEquals('id', $schema->pkName());
135
136
        foreach ($schema->attributes() as $attribute) {
137
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
138
        }
139
140
        $attribute = $schema->attributes()[1];
141
142
        $this->assertSame(false, $attribute->isRequired());
143
        $this->assertSame(8, $attribute->min());
144
        $this->assertSame(10, $attribute->max());
145
    }
146
147
    public function testItSchemaUsingSchemaAttributeOk(): void
148
    {
149
        $name = 'name';
150
        $title = 'title';
151
        $attributes = [
152
            new SchemaAttribute('foo', 'string', 'required:true'),
153
            new SchemaAttribute('bar', 'integer', 'required:false|min:8|max:10'),
154
        ];
155
156
        $schema = new Schema($name, $title, $attributes);
157
158
        $this->assertEquals($name, $schema->name());
159
        $this->assertEquals($title, $schema->title());
160
        $this->assertIsArray($schema->attributes());
161
        $this->assertSame(2, \count($schema->attributes()));
162
        $this->assertEquals('id', $schema->pkName());
163
164
        foreach ($schema->attributes() as $attribute) {
165
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
166
        }
167
168
        $attribute = $schema->attributes()[1];
169
170
        $this->assertSame(false, $attribute->isRequired());
171
        $this->assertSame(8, $attribute->min());
172
        $this->assertSame(10, $attribute->max());
173
    }
174
175
    /**
176
     * @dataProvider getNameInvalid
177
     *
178
     * @param mixed $name
179
     */
180
    public function testItSchemaFromArrayNameInvalidThrowException($name): void
181
    {
182
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
183
        $this->expectExceptionMessage('name is');
184
185
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
186
        $array[$name] = $array['table'];
187
        unset($array['table']);
188
189
        Schema::fromArray($array);
190
    }
191
192
    public function testItSchemaFromArrayWithoutTitleThrowException(): void
193
    {
194
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
195
        $this->expectExceptionMessage(':title');
196
197
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
198
        unset($array['table'][Keyword::TITLE]);
199
200
        Schema::fromArray($array);
201
    }
202
203
    /**
204
     * @dataProvider getTitleInvalid
205
     *
206
     * @param mixed $title
207
     */
208
    public function testItSchemaFromArrayTitleInvalidThrowException($title): void
209
    {
210
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
211
        $this->expectExceptionMessage(':title');
212
213
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
214
        $array['table'][Keyword::TITLE] = $title;
215
216
        Schema::fromArray($array);
217
    }
218
219
    public function testItSchemaFromArrayWithoutTableAttributesOk(): void
220
    {
221
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
222
        unset($array['table'][Keyword::ATTRIBUTES]);
223
224
        Schema::fromArray($array);
225
226
        $this->assertTrue(true);
227
    }
228
229
    public function testItSchemaFromArrayAttributesEmptyOk(): void
230
    {
231
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
232
        $array['table'][Keyword::ATTRIBUTES] = [];
233
234
        Schema::fromArray($array);
235
236
        $this->assertTrue(true);
237
    }
238
239
    public function testItSchemaFromArrayActionsEmptyOk(): void
240
    {
241
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
242
        $array['table'][Keyword::ACTIONS] = [];
243
244
        Schema::fromArray($array);
245
246
        $this->assertTrue(true);
247
    }
248
249
    public function testItSchemaFromArrayAttributesWithInterfaceOk(): void
250
    {
251
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
252
        $array['table'][Keyword::ATTRIBUTES] = [new SchemaAttribute('foo', 'integer')];
253
254
        Schema::fromArray($array);
255
256
        $this->assertTrue(true);
257
    }
258
259
    public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void
260
    {
261
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
262
263
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
264
        unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]);
265
266
        Schema::fromArray($array);
267
    }
268
269
    /**
270
     * @dataProvider getActionInvalid
271
     */
272
    public function testItSchemaFromArrayWithTableActionsInvalidThrowException(string $action): void
273
    {
274
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
275
276
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
277
        $array['table'][Keyword::ACTIONS] = $action;
278
279
        Schema::fromArray($array);
280
    }
281
282
283
    public function testItSchemaFromArrayOk(): void
284
    {
285
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
286
287
        $schema = Schema::fromArray($array);
288
289
        $this->assertEquals('table', $schema->name());
290
        $this->assertEquals('fas fa-icon', $schema->icon());
291
        $this->assertEquals('Table Name', $schema->title());
292
        $this->assertEquals('PrimaryColumn', $schema->pkName());
293
        $this->assertEquals('int', $schema->pkTypeHint());
294
        $this->assertIsArray($schema->attributes());
295
        $this->assertEquals(7, \count($schema->attributes()));
296
        $this->assertIsArray($schema->fkRelations());
297
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['pkTable']);
298
        $this->assertEquals('FkColumn', $schema->fkRelations()['FkColumn']['pkId']);
299
        $this->assertEquals('integer', $schema->fkRelations()['FkColumn']['pkDataType']);
300
        $this->assertEquals('int', $schema->fkRelations()['FkColumn']['pkTypeHint']);
301
        $this->assertEquals('id', $schema->fkRelations()['FkColumn']['fkId']);
302
        $this->assertEquals('name', $schema->fkRelations()['FkColumn']['fkName']);
303
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['fkTable']);
304
        $this->assertSame(false, $schema->fkRelations()['FkColumn']['isBlameBy']);
305
        $this->assertSame(false, $schema->fkRelations()['FkColumn']['isRequired']);
306
        $this->assertSame(10, $schema->fkRelations()['FkColumn']['minChars']);
307
        $this->assertEquals('en', $schema->language());
308
        $this->assertIsArray($schema->actions());
309
310
        foreach ($schema->attributes() as $attribute) {
311
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
312
        }
313
    }
314
315
    public function testItSchemaFromFileNotExistsThrowException(): void
316
    {
317
        $this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class);
318
319
        Schema::fromFile('/path/error');
320
    }
321
322
    public function testItSchemaFromFileFormatErrorThrowException(): void
323
    {
324
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
325
326
        Schema::fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__));
327
    }
328
329
    public function testItSchemaFromFileOk(): void
330
    {
331
        $schema = Schema::fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
332
333
        $this->assertEquals('table', $schema->name());
334
        $this->assertEquals('fas fa-icon', $schema->icon());
335
        $this->assertEquals('Table Name', $schema->title());
336
        $this->assertEquals('PrimaryColumn', $schema->pkName());
337
        $this->assertEquals('int', $schema->pkTypeHint());
338
        $this->assertIsArray($schema->attributes());
339
        $this->assertEquals(7, \count($schema->attributes()));
340
        $this->assertIsArray($schema->fkRelations());
341
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['pkTable']);
342
        $this->assertEquals('FkColumn', $schema->fkRelations()['FkColumn']['pkId']);
343
        $this->assertEquals('integer', $schema->fkRelations()['FkColumn']['pkDataType']);
344
        $this->assertEquals('int', $schema->fkRelations()['FkColumn']['pkTypeHint']);
345
        $this->assertEquals('id', $schema->fkRelations()['FkColumn']['fkId']);
346
        $this->assertEquals('name', $schema->fkRelations()['FkColumn']['fkName']);
347
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['fkTable']);
348
        $this->assertSame(false, $schema->fkRelations()['FkColumn']['isBlameBy']);
349
        $this->assertSame(false, $schema->fkRelations()['FkColumn']['isRequired']);
350
        $this->assertSame(10, $schema->fkRelations()['FkColumn']['minChars']);
351
        $this->assertEquals('en', $schema->language());
352
        $this->assertIsArray($schema->actions());
353
354
        foreach ($schema->attributes() as $attribute) {
355
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
356
        }
357
    }
358
359
    public function getNameInvalid(): array
360
    {
361
        return [
362
            [''],
363
            [' '],
364
        ];
365
    }
366
367
    public function getNameErrorInvalid(): array
368
    {
369
        return [
370
            ['db-'],
371
            ['1234db'],
372
            ['jóbs'],
373
        ];
374
    }
375
376
    public function getTitleInvalid(): array
377
    {
378
        return [
379
            [''],
380
            [' '],
381
        ];
382
    }
383
384
    public function getAction(): array
385
    {
386
        return [
387
            ['i', 'crudfl'],
388
            ['c', 'irudfl'],
389
            ['r', 'ciudfl'],
390
            ['u', 'cridfl'],
391
            ['d', 'cruifl'],
392
            ['f', 'crudil'],
393
        ];
394
    }
395
396
    public function getActionInvalid(): array
397
    {
398
        return [
399
            ['a'],
400
            ['A'],
401
            ['I'],
402
            ['C'],
403
            ['R'],
404
            ['U'],
405
            ['D'],
406
            ['F'],
407
        ];
408
    }
409
}
410