-
Notifications
You must be signed in to change notification settings - Fork 36
/
SchemaGeneratorTest.php
235 lines (211 loc) · 6.85 KB
/
SchemaGeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare(strict_types=1);
namespace FlixTech\AvroSerializer\Test\Objects\Schema\Generation;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use FlixTech\AvroSerializer\Objects\Schema;
use FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\ArraysWithComplexType;
use FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\EmptyRecord;
use FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\MapsWithComplexType;
use FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\PrimitiveTypes;
use FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\RecordWithComplexTypes;
use FlixTech\AvroSerializer\Test\Objects\Schema\Generation\Fixture\RecordWithRecordType;
use PHPUnit\Framework\TestCase;
class SchemaGeneratorTest extends TestCase
{
/**
* @var Schema\Generation\SchemaGenerator
*/
private $generator;
protected function setUp(): void
{
AnnotationRegistry::registerLoader('class_exists');
$this->generator = new Schema\Generation\SchemaGenerator(
new Schema\Generation\AnnotationReader(
new AnnotationReader()
)
);
}
/**
* @test
*/
public function it_should_generate_an_empty_record()
{
$schema = $this->generator->generate(EmptyRecord::class);
$expected = Schema::record()
->name('EmptyRecord')
->namespace('org.acme');
$this->assertEquals($expected, $schema);
}
/**
* @test
*/
public function it_should_generate_a_record_schema_with_primitive_types()
{
$schema = $this->generator->generate(PrimitiveTypes::class);
$expected = Schema::record()
->name('PrimitiveTypes')
->namespace('org.acme')
->field(
'nullType',
Schema::null(),
Schema\Record\FieldOption::doc('null type')
)
->field(
'isItTrue',
Schema::boolean(),
Schema\Record\FieldOption::default(false)
)
->field(
'intType',
Schema::int()
)
->field(
'longType',
Schema::long(),
Schema\Record\FieldOption::orderAsc()
)
->field(
'floatType',
Schema::float(),
Schema\Record\FieldOption::aliases('foo', 'bar')
)
->field(
'doubleType',
Schema::double()
)
->field(
'bytesType',
Schema::bytes()
)
->field(
'stringType',
Schema::string()
);
$this->assertEquals($expected, $schema);
}
/**
* @test
*/
public function it_should_generate_a_schema_record_with_complex_types()
{
$schema = $this->generator->generate(RecordWithComplexTypes::class);
$expected = Schema::record()
->name('RecordWithComplexTypes')
->field(
'array',
Schema::array()
->items(Schema::string())
->default(['foo', 'bar'])
)
->field(
'map',
Schema::map()
->values(Schema::int())
->default(['foo' => 42, 'bar' => 42])
)
->field(
'enum',
Schema::enum()
->name('Suit')
->symbols('SPADES', 'HEARTS', 'DIAMONDS', 'CLUBS'),
Schema\Record\FieldOrder::asc()
)
->field(
'fixed',
Schema::fixed()
->name('md5')
->namespace('org.acme')
->aliases('foo', 'bar')
->size(16)
)
->field(
'union',
Schema::union(Schema::string(), Schema::int(), Schema::array()->items(Schema::string()))
);
$this->assertEquals($expected, $schema);
}
/**
* @test
*/
public function it_should_generate_records_containing_records()
{
$schema = $this->generator->generate(RecordWithRecordType::class);
$expected = Schema::record()
->name('RecordWithRecordType')
->field(
'simpleField',
Schema::record()
->name('SimpleRecord')
->namespace('org.acme')
->doc('This a simple record for testing purposes')
->field(
'intType',
Schema::int(),
Schema\Record\FieldOption::default(42)
),
)
->field(
'unionField',
Schema::union(
Schema::null(),
Schema::named('org.acme.SimpleRecord')
)
);
$this->assertEquals($expected, $schema);
}
/**
* @test
*/
public function it_should_generate_a_record_schema_with_arrays_containing_complex_types()
{
$schema = $this->generator->generate(ArraysWithComplexType::class);
$expected = Schema::record()
->name('ArraysWithComplexType')
->field(
'arrayWithUnion',
Schema::array()
->items(
Schema::union(
Schema::string(),
Schema::array()->items(Schema::string())
)
)
)
->field(
'arrayWithMap',
Schema::array()
->items(
Schema::map()->values(Schema::string())
)
);
$this->assertEquals($expected, $schema);
}
/**
* @test
*/
public function it_should_generate_a_record_schema_with_maps_containing_complex_types()
{
$schema = $this->generator->generate(MapsWithComplexType::class);
$expected = Schema::record()
->name('MapsWithComplexType')
->field(
'mapWithUnion',
Schema::map()
->values(
Schema::union(
Schema::string(),
Schema::array()->items(Schema::string())
)
)
)
->field(
'mapWithArray',
Schema::map()
->values(
Schema::array()->items(Schema::string())
)
);
$this->assertEquals($expected, $schema);
}
}