Skip to content

Commit 28bfd32

Browse files
author
Enno Woortmann
committed
Additional mapped enum test cases
1 parent dbafc4d commit 28bfd32

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

tests/PostProcessor/EnumPostProcessorTest.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPModelGenerator\SchemaProcessor\PostProcessor\EnumPostProcessor;
1313
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
1414
use ReflectionClass;
15+
use ReflectionEnum;
1516

1617
class EnumPostProcessorTest extends AbstractPHPModelGeneratorTest
1718
{
@@ -49,14 +50,15 @@ public function testStringOnlyEnum(): void
4950
$returnType = $this->getReturnType($object, 'getProperty');
5051
$this->assertTrue($returnType->allowsNull());
5152
$enum = $returnType->getName();
52-
$enumName = (new ReflectionClass($enum))->getShortName();
53+
$reflectionEnum = new ReflectionEnum($enum);
54+
$enumName = $reflectionEnum->getShortName();
5355

5456
$this->assertEqualsCanonicalizing(
5557
[$enumName, 'null'],
5658
explode('|', $this->getReturnTypeAnnotation($object, 'getProperty'))
5759
);
5860

59-
$this->assertTrue(enum_exists($enum));
61+
$this->assertSame('string', $reflectionEnum->getBackingType()->getName());
6062

6163
$this->assertEqualsCanonicalizing(
6264
['Hans', 'Dieter'],
@@ -93,6 +95,50 @@ public function testInvalidStringOnlyEnumValue(): void
9395
new $className(['property' => 'Meier']);
9496
}
9597

98+
public function testMappedStringOnlyEnum(): void
99+
{
100+
$this->addPostProcessor();
101+
102+
$className = $this->generateClassFromFileTemplate(
103+
'EnumPropertyMapped.json',
104+
['["Hans", "Dieter"]', '{"CEO": "Hans", "CTO": "Dieter"}'],
105+
(new GeneratorConfiguration())->setImmutable(false)->setCollectErrors(false),
106+
false
107+
);
108+
109+
$this->includeGeneratedEnums(1);
110+
111+
$object = new $className(['property' => 'Hans']);
112+
$this->assertSame('Hans', $object->getProperty()->value);
113+
$this->assertSame('CEO', $object->getProperty()->name);
114+
115+
$object->setProperty('Dieter');
116+
$this->assertSame('Dieter', $object->getProperty()->value);
117+
$this->assertSame('CTO', $object->getProperty()->name);
118+
119+
$object->setProperty(null);
120+
$this->assertNull($object->getProperty());
121+
122+
$returnType = $this->getReturnType($object, 'getProperty');
123+
$this->assertTrue($returnType->allowsNull());
124+
$enum = $returnType->getName();
125+
126+
$this->assertSame('string', (new ReflectionEnum($enum))->getBackingType()->getName());
127+
128+
$this->assertEqualsCanonicalizing(
129+
['CEO', 'CTO'],
130+
array_map(function (BackedEnum $value): string { return $value->name; }, $enum::cases())
131+
);
132+
$this->assertEqualsCanonicalizing(
133+
['Hans', 'Dieter'],
134+
array_map(function (BackedEnum $value): string { return $value->value; }, $enum::cases())
135+
);
136+
137+
$object->setProperty($enum::CEO);
138+
$this->assertSame('Hans', $object->getProperty()->value);
139+
$this->assertSame('CEO', $object->getProperty()->name);
140+
}
141+
96142
/**
97143
* @dataProvider unmappedEnumThrowsAnExceptionDataProvider
98144
*/
@@ -131,11 +177,14 @@ public function testInvalidEnumMapThrowsAnException(string $enumValues, string $
131177
public function invalidEnumMapThrowsAnExceptionDataProvider(): array
132178
{
133179
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}'],
180+
'invalid map (int)' => ['[0, 1, 2]', '100'],
181+
'invalid map (array)' => ['[0, 1, 2]', '[0, 1, 2]'],
182+
'missing mapped elements (int)' => ['[0, 1, 2]', '{"a": 0, "b": 1}'],
183+
'too many mapped elements (int)' => ['[0, 1]', '{"a": 0, "b": 1, "c": 2}'],
184+
'wrong elements mapped (int)' => ['[0, 1]', '{"a": 0, "c": 2}'],
185+
'missing mapped elements (string)' => ['["a", "b", "c"]', '{"x": "a", "y": "b"}'],
186+
'too many mapped elements (string)' => ['["a", "b"]', '{"x": "a", "y": "b", "z": "c"}'],
187+
'wrong elements mapped (string)' => ['["a", "b"]', '{"x": "a", "y": "c"}'],
139188
];
140189
}
141190

0 commit comments

Comments
 (0)