Completed
Push — develop ( 6b671f...4eb6c9 )
by Freddie
11:11
created

SchemaTest::getNameErrorInvalid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 6
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;
11
12
use FlexPHP\Schema\Constants\Keyword;
13
use FlexPHP\Schema\Schema;
14
use FlexPHP\Schema\SchemaAttribute;
15
use FlexPHP\Schema\SchemaAttributeInterface;
16
use Symfony\Component\Yaml\Yaml;
17
18
class SchemaTest extends TestCase
19
{
20
    /**
21
     * @dataProvider getNameInvalid
22
     *
23
     * @param mixed $name
24
     */
25
    public function testItSchemaNameInvalidThrowException($name): void
26
    {
27
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
28
        $this->expectExceptionMessage('name is');
29
30
        new Schema($name, 'title', []);
31
    }
32
33
    /**
34
     * @dataProvider getNameErrorInvalid
35
     *
36
     * @param mixed $name
37
     */
38
    public function testItSchemaNameErrorThrowException($name): void
39
    {
40
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
41
        $this->expectExceptionMessage('only accept');
42
43
        new Schema($name, 'title', []);
44
    }
45
46
    /**
47
     * @dataProvider getTitleInvalid
48
     *
49
     * @param mixed $title
50
     */
51
    public function testItSchemaTitleInvalidThrowException($title): void
52
    {
53
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
54
        $this->expectExceptionMessage(':title');
55
56
        new Schema('name', $title, []);
57
    }
58
59
    public function testItSchemaAttributesEmptyOk(): void
60
    {
61
        new Schema('name', 'title', []);
62
63
        $this->assertTrue(true);
64
    }
65
66
    public function testItSchemaSetOk(): void
67
    {
68
        $name = 'name';
69
        $title = 'title';
70
        $attributes = [
71
            [
72
                Keyword::NAME => 'foo',
73
                Keyword::DATATYPE => 'string',
74
                Keyword::CONSTRAINTS => 'required:true',
75
            ],
76
            [
77
                Keyword::NAME => 'bar',
78
                Keyword::DATATYPE => 'integer',
79
                Keyword::CONSTRAINTS => 'required:false|min:8|max:10',
80
            ],
81
        ];
82
83
        $schema = new Schema($name, $title, $attributes);
84
85
        $this->assertEquals($name, $schema->name());
86
        $this->assertEquals($title, $schema->title());
87
        $this->assertIsArray($schema->attributes());
88
        $this->assertSame(2, \count($schema->attributes()));
89
        $this->assertEquals('id', $schema->pkName());
90
91
        foreach ($schema->attributes() as $attribute) {
92
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
93
        }
94
95
        $attribute = $schema->attributes()[1];
96
97
        $this->assertSame(false, $attribute->isRequired());
98
        $this->assertSame(8, $attribute->min());
99
        $this->assertSame(10, $attribute->max());
100
    }
101
102
    public function testItSchemaUsingSchemaAttributeOk(): void
103
    {
104
        $name = 'name';
105
        $title = 'title';
106
        $attributes = [
107
            new SchemaAttribute('foo', 'string', 'required:true'),
108
            new SchemaAttribute('bar', 'integer', 'required:false|min:8|max:10'),
109
        ];
110
111
        $schema = new Schema($name, $title, $attributes);
112
113
        $this->assertEquals($name, $schema->name());
114
        $this->assertEquals($title, $schema->title());
115
        $this->assertIsArray($schema->attributes());
116
        $this->assertSame(2, \count($schema->attributes()));
117
        $this->assertEquals('id', $schema->pkName());
118
119
        foreach ($schema->attributes() as $attribute) {
120
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
121
        }
122
123
        $attribute = $schema->attributes()[1];
124
125
        $this->assertSame(false, $attribute->isRequired());
126
        $this->assertSame(8, $attribute->min());
127
        $this->assertSame(10, $attribute->max());
128
    }
129
130
    /**
131
     * @dataProvider getNameInvalid
132
     *
133
     * @param mixed $name
134
     */
135
    public function testItSchemaFromArrayNameInvalidThrowException($name): void
136
    {
137
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
138
        $this->expectExceptionMessage('name is');
139
140
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
141
        $array[$name] = $array['table'];
142
        unset($array['table']);
143
144
        Schema::fromArray($array);
145
    }
146
147
    public function testItSchemaFromArrayWithoutTitleThrowException(): void
148
    {
149
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
150
        $this->expectExceptionMessage(':title');
151
152
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
153
        unset($array['table'][Keyword::TITLE]);
154
155
        Schema::fromArray($array);
156
    }
157
158
    /**
159
     * @dataProvider getTitleInvalid
160
     *
161
     * @param mixed $title
162
     */
163
    public function testItSchemaFromArrayTitleInvalidThrowException($title): void
164
    {
165
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
166
        $this->expectExceptionMessage(':title');
167
168
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
169
        $array['table'][Keyword::TITLE] = $title;
170
171
        Schema::fromArray($array);
172
    }
173
174
    public function testItSchemaFromArrayWithoutTableAttributesOk(): void
175
    {
176
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
177
        unset($array['table'][Keyword::ATTRIBUTES]);
178
179
        Schema::fromArray($array);
180
181
        $this->assertTrue(true);
182
    }
183
184
    public function testItSchemaFromArrayAttributesEmptyOk(): void
185
    {
186
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
187
        $array['table'][Keyword::ATTRIBUTES] = [];
188
189
        Schema::fromArray($array);
190
191
        $this->assertTrue(true);
192
    }
193
194
    public function testItSchemaFromArrayAttributesWithInterfaceOk(): void
195
    {
196
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
197
        $array['table'][Keyword::ATTRIBUTES] = [new SchemaAttribute('foo', 'integer')];
198
199
        Schema::fromArray($array);
200
201
        $this->assertTrue(true);
202
    }
203
204
    public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void
205
    {
206
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
207
208
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
209
        unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]);
210
211
        Schema::fromArray($array);
212
    }
213
214
    public function testItSchemaFromArrayOk(): void
215
    {
216
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
217
218
        $schema = Schema::fromArray($array);
219
220
        $this->assertEquals('table', $schema->name());
221
        $this->assertEquals('fas fa-icon', $schema->icon());
222
        $this->assertEquals('Table Name', $schema->title());
223
        $this->assertEquals('PrimaryColumn', $schema->pkName());
224
        $this->assertEquals('int', $schema->pkTypeHint());
225
        $this->assertIsArray($schema->attributes());
226
        $this->assertEquals(7, \count($schema->attributes()));
227
        $this->assertIsArray($schema->fkRelations());
228
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['pkTable']);
229
        $this->assertEquals('FkColumn', $schema->fkRelations()['FkColumn']['pkId']);
230
        $this->assertEquals('integer', $schema->fkRelations()['FkColumn']['pkDataType']);
231
        $this->assertEquals('int', $schema->fkRelations()['FkColumn']['pkTypeHint']);
232
        $this->assertEquals('id', $schema->fkRelations()['FkColumn']['fkId']);
233
        $this->assertEquals('name', $schema->fkRelations()['FkColumn']['fkName']);
234
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['fkTable']);
235
        $this->assertSame(false, $schema->fkRelations()['FkColumn']['isBlameBy']);
236
        $this->assertSame(false, $schema->fkRelations()['FkColumn']['isRequired']);
237
        $this->assertEquals('en', $schema->language());
238
239
        foreach ($schema->attributes() as $attribute) {
240
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
241
        }
242
    }
243
244
    public function testItSchemaFromFileNotExistsThrowException(): void
245
    {
246
        $this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class);
247
248
        Schema::fromFile('/path/error');
249
    }
250
251
    public function testItSchemaFromFileFormatErrorThrowException(): void
252
    {
253
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
254
255
        Schema::fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__));
256
    }
257
258
    public function testItSchemaFromFileOk(): void
259
    {
260
        $schema = Schema::fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
261
262
        $this->assertEquals('table', $schema->name());
263
        $this->assertEquals('fas fa-icon', $schema->icon());
264
        $this->assertEquals('Table Name', $schema->title());
265
        $this->assertEquals('PrimaryColumn', $schema->pkName());
266
        $this->assertEquals('int', $schema->pkTypeHint());
267
        $this->assertIsArray($schema->attributes());
268
        $this->assertEquals(7, \count($schema->attributes()));
269
        $this->assertIsArray($schema->fkRelations());
270
        $this->assertEquals('table', $schema->fkRelations()['FkColumn']['pkTable']);
271
        $this->assertEquals('FkColumn', $schema->fkRelations()['FkColumn']['pkId']);
272
        $this->assertEquals('integer', $schema->fkRelations()['FkColumn']['pkDataType']);
273
        $this->assertEquals('int', $schema->fkRelations()['FkColumn']['pkTypeHint']);
274
        $this->assertEquals('id', $schema->fkRelations()['FkColumn']['fkId']);
275
        $this->assertEquals('name', $schema->fkRelations()['FkColumn']['fkName']);
276
        $this->assertEquals('en', $schema->language());
277
278
        foreach ($schema->attributes() as $attribute) {
279
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
280
        }
281
    }
282
283
    public function getNameInvalid(): array
284
    {
285
        return [
286
            [''],
287
            [' '],
288
        ];
289
    }
290
291
    public function getNameErrorInvalid(): array
292
    {
293
        return [
294
            ['db-'],
295
            ['1234db'],
296
            ['jóbs'],
297
        ];
298
    }
299
300
    public function getTitleInvalid(): array
301
    {
302
        return [
303
            [''],
304
            [' '],
305
        ];
306
    }
307
}
308