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
|
|
|
/** |
90
|
|
|
* @dataProvider getNameInvalid |
91
|
|
|
* |
92
|
|
|
* @param mixed $name |
93
|
|
|
*/ |
94
|
|
|
public function testItSchemaFromArrayNameInvalidThrowException($name): void |
95
|
|
|
{ |
96
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
97
|
|
|
$this->expectExceptionMessage('name is'); |
98
|
|
|
|
99
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
100
|
|
|
$array[$name] = $array['table']; |
101
|
|
|
unset($array['table']); |
102
|
|
|
|
103
|
|
|
Schema::fromArray($array); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testItSchemaFromArrayWithoutTitleThrowException(): void |
107
|
|
|
{ |
108
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
109
|
|
|
$this->expectExceptionMessage(':title'); |
110
|
|
|
|
111
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
112
|
|
|
unset($array['table'][Keyword::TITLE]); |
113
|
|
|
|
114
|
|
|
Schema::fromArray($array); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @dataProvider getTitleInvalid |
119
|
|
|
* |
120
|
|
|
* @param mixed $title |
121
|
|
|
*/ |
122
|
|
|
public function testItSchemaFromArrayTitleInvalidThrowException($title): void |
123
|
|
|
{ |
124
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
125
|
|
|
$this->expectExceptionMessage(':title'); |
126
|
|
|
|
127
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
128
|
|
|
$array['table'][Keyword::TITLE] = $title; |
129
|
|
|
|
130
|
|
|
Schema::fromArray($array); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testItSchemaFromArrayWithoutTableAttributesOk(): void |
134
|
|
|
{ |
135
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
136
|
|
|
unset($array['table'][Keyword::ATTRIBUTES]); |
137
|
|
|
|
138
|
|
|
Schema::fromArray($array); |
139
|
|
|
|
140
|
|
|
$this->assertTrue(true); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testItSchemaFromArrayAttributesEmptyOk(): void |
144
|
|
|
{ |
145
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
146
|
|
|
$array['table'][Keyword::ATTRIBUTES] = []; |
147
|
|
|
|
148
|
|
|
Schema::fromArray($array); |
149
|
|
|
|
150
|
|
|
$this->assertTrue(true); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testItSchemaFromArrayAttributesWithInterfaceOk(): void |
154
|
|
|
{ |
155
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
156
|
|
|
$array['table'][Keyword::ATTRIBUTES] = [new SchemaAttribute('foo', 'integer')]; |
157
|
|
|
|
158
|
|
|
Schema::fromArray($array); |
159
|
|
|
|
160
|
|
|
$this->assertTrue(true); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void |
164
|
|
|
{ |
165
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
166
|
|
|
|
167
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
168
|
|
|
unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]); |
169
|
|
|
|
170
|
|
|
Schema::fromArray($array); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testItSchemaFromArrayOk(): void |
174
|
|
|
{ |
175
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
176
|
|
|
|
177
|
|
|
$schema = Schema::fromArray($array); |
178
|
|
|
|
179
|
|
|
$this->assertEquals('table', $schema->name()); |
180
|
|
|
$this->assertEquals('fas fa-icon', $schema->icon()); |
181
|
|
|
$this->assertEquals('Table Name', $schema->title()); |
182
|
|
|
$this->assertEquals('PrimaryColumn', $schema->pkName()); |
183
|
|
|
$this->assertEquals('int', $schema->pkTypeHint()); |
184
|
|
|
$this->assertIsArray($schema->attributes()); |
185
|
|
|
$this->assertIsArray($schema->fkRelations()); |
186
|
|
|
$this->assertEquals('en', $schema->language()); |
187
|
|
|
|
188
|
|
|
foreach ($schema->attributes() as $attribute) { |
189
|
|
|
$this->assertInstanceOf(SchemaAttributeInterface::class, $attribute); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testItSchemaFromFileNotExistsThrowException(): void |
194
|
|
|
{ |
195
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class); |
196
|
|
|
|
197
|
|
|
Schema::fromFile('/path/error'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testItSchemaFromFileFormatErrorThrowException(): void |
201
|
|
|
{ |
202
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
203
|
|
|
|
204
|
|
|
Schema::fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testItSchemaFromFileOk(): void |
208
|
|
|
{ |
209
|
|
|
$schema = Schema::fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
210
|
|
|
|
211
|
|
|
$this->assertEquals('table', $schema->name()); |
212
|
|
|
$this->assertEquals('fas fa-icon', $schema->icon()); |
213
|
|
|
$this->assertEquals('Table Name', $schema->title()); |
214
|
|
|
$this->assertEquals('PrimaryColumn', $schema->pkName()); |
215
|
|
|
$this->assertEquals('int', $schema->pkTypeHint()); |
216
|
|
|
$this->assertIsArray($schema->attributes()); |
217
|
|
|
$this->assertIsArray($schema->fkRelations()); |
218
|
|
|
$this->assertEquals('en', $schema->language()); |
219
|
|
|
|
220
|
|
|
foreach ($schema->attributes() as $attribute) { |
221
|
|
|
$this->assertInstanceOf(SchemaAttributeInterface::class, $attribute); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getNameInvalid(): array |
226
|
|
|
{ |
227
|
|
|
return [ |
228
|
|
|
[''], |
229
|
|
|
[' '], |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getTitleInvalid(): array |
234
|
|
|
{ |
235
|
|
|
return [ |
236
|
|
|
[''], |
237
|
|
|
[' '], |
238
|
|
|
]; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|