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\SchemaAttributeInterface; |
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
16
|
|
|
|
17
|
|
|
class SchemaTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @dataProvider getNameInvalid |
21
|
|
|
* |
22
|
|
|
* @param mixed $name |
23
|
|
|
*/ |
24
|
|
|
public function testItSchemaNameInvalidThrowException($name): void |
25
|
|
|
{ |
26
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
27
|
|
|
$this->expectExceptionMessage('name is'); |
28
|
|
|
|
29
|
|
|
new Schema($name, 'title', []); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider getTitleInvalid |
34
|
|
|
* |
35
|
|
|
* @param mixed $title |
36
|
|
|
*/ |
37
|
|
|
public function testItSchemaTitleInvalidThrowException($title): void |
38
|
|
|
{ |
39
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
40
|
|
|
$this->expectExceptionMessage(':title'); |
41
|
|
|
|
42
|
|
|
new Schema('name', $title, []); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testItSchemaAttributesEmptyOk(): void |
46
|
|
|
{ |
47
|
|
|
new Schema('name', 'title', []); |
48
|
|
|
|
49
|
|
|
$this->assertTrue(true); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testItSchemaSetOk(): void |
53
|
|
|
{ |
54
|
|
|
$name = 'name'; |
55
|
|
|
$title = 'title'; |
56
|
|
|
$attributes = [ |
57
|
|
|
[ |
58
|
|
|
Keyword::NAME => 'foo', |
59
|
|
|
Keyword::DATATYPE => 'string', |
60
|
|
|
Keyword::CONSTRAINTS => 'required:true', |
61
|
|
|
], |
62
|
|
|
[ |
63
|
|
|
Keyword::NAME => 'bar', |
64
|
|
|
Keyword::DATATYPE => 'integer', |
65
|
|
|
Keyword::CONSTRAINTS => 'required:false|min:8|max:10', |
66
|
|
|
], |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$schema = new Schema($name, $title, $attributes); |
70
|
|
|
|
71
|
|
|
$this->assertEquals($name, $schema->name()); |
72
|
|
|
$this->assertEquals($title, $schema->title()); |
73
|
|
|
$this->assertIsArray($schema->attributes()); |
74
|
|
|
$this->assertSame(2, \count($schema->attributes())); |
75
|
|
|
$this->assertEquals('id', $schema->pkName()); |
76
|
|
|
|
77
|
|
|
foreach ($schema->attributes() as $attribute) { |
78
|
|
|
$this->assertInstanceOf(SchemaAttributeInterface::class, $attribute); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$attribute = $schema->attributes()[1]; |
82
|
|
|
|
83
|
|
|
$this->assertSame(false, $attribute->isRequired()); |
84
|
|
|
$this->assertSame(8, $attribute->min()); |
85
|
|
|
$this->assertSame(10, $attribute->max()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @dataProvider getNameInvalid |
90
|
|
|
* |
91
|
|
|
* @param mixed $name |
92
|
|
|
*/ |
93
|
|
|
public function testItSchemaFromArrayNameInvalidThrowException($name): void |
94
|
|
|
{ |
95
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
96
|
|
|
$this->expectExceptionMessage('name is'); |
97
|
|
|
|
98
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
99
|
|
|
$array[$name] = $array['table']; |
100
|
|
|
unset($array['table']); |
101
|
|
|
|
102
|
|
|
Schema::fromArray($array); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testItSchemaFromArrayWithoutTitleThrowException(): void |
106
|
|
|
{ |
107
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
108
|
|
|
$this->expectExceptionMessage(':title'); |
109
|
|
|
|
110
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
111
|
|
|
unset($array['table'][Keyword::TITLE]); |
112
|
|
|
|
113
|
|
|
Schema::fromArray($array); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @dataProvider getTitleInvalid |
118
|
|
|
* |
119
|
|
|
* @param mixed $title |
120
|
|
|
*/ |
121
|
|
|
public function testItSchemaFromArrayTitleInvalidThrowException($title): void |
122
|
|
|
{ |
123
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class); |
124
|
|
|
$this->expectExceptionMessage(':title'); |
125
|
|
|
|
126
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
127
|
|
|
$array['table'][Keyword::TITLE] = $title; |
128
|
|
|
|
129
|
|
|
Schema::fromArray($array); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testItSchemaFromArrayWithoutTableAttributesOk(): void |
133
|
|
|
{ |
134
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
135
|
|
|
unset($array['table'][Keyword::ATTRIBUTES]); |
136
|
|
|
|
137
|
|
|
Schema::fromArray($array); |
138
|
|
|
|
139
|
|
|
$this->assertTrue(true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testItSchemaFromArrayAttributesEmptyOk(): void |
143
|
|
|
{ |
144
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
145
|
|
|
$array['table'][Keyword::ATTRIBUTES] = []; |
146
|
|
|
|
147
|
|
|
Schema::fromArray($array); |
148
|
|
|
|
149
|
|
|
$this->assertTrue(true); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void |
153
|
|
|
{ |
154
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
155
|
|
|
|
156
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
157
|
|
|
unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]); |
158
|
|
|
|
159
|
|
|
Schema::fromArray($array); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testItSchemaFromArrayOk(): void |
163
|
|
|
{ |
164
|
|
|
$array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
165
|
|
|
|
166
|
|
|
$schema = Schema::fromArray($array); |
167
|
|
|
|
168
|
|
|
$this->assertEquals('table', $schema->name()); |
169
|
|
|
$this->assertEquals('fas fa-icon', $schema->icon()); |
170
|
|
|
$this->assertEquals('Table Name', $schema->title()); |
171
|
|
|
$this->assertEquals('PrimaryColumn', $schema->pkName()); |
172
|
|
|
$this->assertEquals('int', $schema->pkTypeHint()); |
173
|
|
|
$this->assertIsArray($schema->attributes()); |
174
|
|
|
$this->assertIsArray($schema->fkRelations()); |
175
|
|
|
$this->assertEquals('en', $schema->language()); |
176
|
|
|
|
177
|
|
|
foreach ($schema->attributes() as $attribute) { |
178
|
|
|
$this->assertInstanceOf(SchemaAttributeInterface::class, $attribute); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testItSchemaFromFileNotExistsThrowException(): void |
183
|
|
|
{ |
184
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class); |
185
|
|
|
|
186
|
|
|
Schema::fromFile('/path/error'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testItSchemaFromFileFormatErrorThrowException(): void |
190
|
|
|
{ |
191
|
|
|
$this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
192
|
|
|
|
193
|
|
|
Schema::fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testItSchemaFromFileOk(): void |
197
|
|
|
{ |
198
|
|
|
$schema = Schema::fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__)); |
199
|
|
|
|
200
|
|
|
$this->assertEquals('table', $schema->name()); |
201
|
|
|
$this->assertEquals('fas fa-icon', $schema->icon()); |
202
|
|
|
$this->assertEquals('Table Name', $schema->title()); |
203
|
|
|
$this->assertEquals('PrimaryColumn', $schema->pkName()); |
204
|
|
|
$this->assertEquals('int', $schema->pkTypeHint()); |
205
|
|
|
$this->assertIsArray($schema->attributes()); |
206
|
|
|
$this->assertIsArray($schema->fkRelations()); |
207
|
|
|
$this->assertEquals('en', $schema->language()); |
208
|
|
|
|
209
|
|
|
foreach ($schema->attributes() as $attribute) { |
210
|
|
|
$this->assertInstanceOf(SchemaAttributeInterface::class, $attribute); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function getNameInvalid(): array |
215
|
|
|
{ |
216
|
|
|
return [ |
217
|
|
|
[''], |
218
|
|
|
[' '], |
219
|
|
|
]; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getTitleInvalid(): array |
223
|
|
|
{ |
224
|
|
|
return [ |
225
|
|
|
[''], |
226
|
|
|
[' '], |
227
|
|
|
]; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|