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