Skip to content

Commit dbafc4d

Browse files
author
Enno Woortmann
committed
add test cases
1 parent eaf9c98 commit dbafc4d

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"wol-soft/php-json-schema-model-generator-production": "dev-enumPostProcessor",
15-
"wol-soft/php-micro-template": "^1.3.2",
15+
"wol-soft/php-micro-template": "^1.9.0",
1616

1717
"php": ">=7.2",
1818
"ext-json": "*",

src/SchemaProcessor/PostProcessor/EnumPostProcessor.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use PHPModelGenerator\Model\Validator\EnumValidator;
1717
use PHPModelGenerator\Model\Validator\FilterValidator;
1818
use PHPModelGenerator\ModelGenerator;
19-
use PHPModelGenerator\PropertyProcessor\Decorator\TypeHint\ClearTypeHintDecorator;
2019
use PHPModelGenerator\PropertyProcessor\Filter\FilterProcessor;
2120

2221
/**
@@ -82,7 +81,7 @@ public function process(Schema $schema, GeneratorConfiguration $generatorConfigu
8281
$generatorConfiguration,
8382
$json['$id'] ?? $schema->getClassName() . ucfirst($property->getName()),
8483
$values,
85-
$json['map'] ?? null
84+
$json['enum-map'] ?? null
8685
);
8786
}
8887

@@ -153,25 +152,27 @@ private function validateEnum(PropertyInterface $property): bool
153152

154153
$types = $this->getArrayTypes($json['enum']);
155154

156-
// the enum must contain either only string or int values to be represented by a backed enum or provide a value
157-
// map to resolve the values
158-
if ($types !== ['string'] && !isset($json['map'])) {
155+
// the enum must contain either only string values or provide a value map to resolve the values
156+
if ($types !== ['string'] && !isset($json['enum-map'])) {
159157
if ($this->skipNonMappedEnums) {
160158
return false;
161159
}
162160

163161
$throw('Unmapped enum %s in file %s');
164162
}
165163

166-
if (isset($json['map'])) {
167-
if (count(array_uintersect(
168-
$json['map'],
169-
$json['enum'],
170-
fn($a, $b) => $a === $b ? 0 : 1
171-
)) !== count($json['enum'])
172-
) {
173-
$throw('invalid enum map %s in file %s');
174-
}
164+
if (isset($json['enum-map']) &&
165+
(
166+
!is_array($json['enum-map'])
167+
|| $this->getArrayTypes(array_keys($json['enum-map'])) !== ['string']
168+
|| count(array_uintersect(
169+
$json['enum-map'],
170+
$json['enum'],
171+
fn($a, $b) => $a === $b ? 0 : 1
172+
)) !== count($json['enum'])
173+
)
174+
) {
175+
$throw('invalid enum map %s in file %s');
175176
}
176177

177178
return true;

tests/PostProcessor/EnumPostProcessorTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPModelGenerator\ModelGenerator;
1212
use PHPModelGenerator\SchemaProcessor\PostProcessor\EnumPostProcessor;
1313
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
14+
use ReflectionClass;
1415

1516
class EnumPostProcessorTest extends AbstractPHPModelGeneratorTest
1617
{
@@ -48,9 +49,10 @@ public function testStringOnlyEnum(): void
4849
$returnType = $this->getReturnType($object, 'getProperty');
4950
$this->assertTrue($returnType->allowsNull());
5051
$enum = $returnType->getName();
52+
$enumName = (new ReflectionClass($enum))->getShortName();
5153

5254
$this->assertEqualsCanonicalizing(
53-
[basename($enum), 'null'],
55+
[$enumName, 'null'],
5456
explode('|', $this->getReturnTypeAnnotation($object, 'getProperty'))
5557
);
5658

@@ -70,7 +72,7 @@ public function testStringOnlyEnum(): void
7072

7173
$this->assertNull($this->getParameterType($object, 'setProperty'));
7274
$this->assertEqualsCanonicalizing(
73-
[basename($enum), 'string', 'null'],
75+
[$enumName, 'string', 'null'],
7476
explode('|', $this->getParameterTypeAnnotation($object, 'setProperty'))
7577
);
7678

@@ -91,6 +93,52 @@ public function testInvalidStringOnlyEnumValue(): void
9193
new $className(['property' => 'Meier']);
9294
}
9395

96+
/**
97+
* @dataProvider unmappedEnumThrowsAnExceptionDataProvider
98+
*/
99+
public function testUnmappedEnumThrowsAnException(string $enumValues): void
100+
{
101+
$this->expectException(SchemaException::class);
102+
$this->expectExceptionMessage("Unmapped enum property in file");
103+
104+
$this->addPostProcessor();
105+
106+
$this->generateClassFromFileTemplate('EnumProperty.json', [$enumValues], null, false);
107+
}
108+
109+
public function unmappedEnumThrowsAnExceptionDataProvider(): array
110+
{
111+
return [
112+
'int enum' => ['[0, 1, 2]'],
113+
'mixed enum with string values' => ['["dieter", 1, "hans"]'],
114+
'mixed enum without string values' => ['[0, 1, false, true]'],
115+
];
116+
}
117+
118+
/**
119+
* @dataProvider invalidEnumMapThrowsAnExceptionDataProvider
120+
*/
121+
public function testInvalidEnumMapThrowsAnException(string $enumValues, string $enumMap): void
122+
{
123+
$this->expectException(SchemaException::class);
124+
$this->expectExceptionMessage("invalid enum map property in file");
125+
126+
$this->addPostProcessor();
127+
128+
$this->generateClassFromFileTemplate('EnumPropertyMapped.json', [$enumValues, $enumMap], null, false);
129+
}
130+
131+
public function invalidEnumMapThrowsAnExceptionDataProvider(): array
132+
{
133+
return [
134+
'invalid map (int)' => ['[0, 1, 2]', '100'],
135+
'invalid map (array)' => ['[0, 1, 2]', '[0, 1, 2]'],
136+
'missing mapped elements' => ['[0, 1, 2]', '{"a": 0, "b": 1}'],
137+
'too many mapped elements' => ['[0, 1]', '{"a": 0, "b": 1, "c": 2}'],
138+
'wrong elements mapped' => ['[0, 1]', '{"a": 0, "c": 2}'],
139+
];
140+
}
141+
94142
public function testEnumPropertyWithTransformingFilterThrowsAnException(): void
95143
{
96144
$this->expectException(SchemaException::class);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"property": {
5+
"enum": %s,
6+
"enum-map": %s
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)