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