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