|
12 | 12 | use PHPModelGenerator\SchemaProcessor\PostProcessor\EnumPostProcessor;
|
13 | 13 | use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
|
14 | 14 | use ReflectionClass;
|
| 15 | +use ReflectionEnum; |
15 | 16 |
|
16 | 17 | class EnumPostProcessorTest extends AbstractPHPModelGeneratorTest
|
17 | 18 | {
|
@@ -49,14 +50,15 @@ public function testStringOnlyEnum(): void
|
49 | 50 | $returnType = $this->getReturnType($object, 'getProperty');
|
50 | 51 | $this->assertTrue($returnType->allowsNull());
|
51 | 52 | $enum = $returnType->getName();
|
52 |
| - $enumName = (new ReflectionClass($enum))->getShortName(); |
| 53 | + $reflectionEnum = new ReflectionEnum($enum); |
| 54 | + $enumName = $reflectionEnum->getShortName(); |
53 | 55 |
|
54 | 56 | $this->assertEqualsCanonicalizing(
|
55 | 57 | [$enumName, 'null'],
|
56 | 58 | explode('|', $this->getReturnTypeAnnotation($object, 'getProperty'))
|
57 | 59 | );
|
58 | 60 |
|
59 |
| - $this->assertTrue(enum_exists($enum)); |
| 61 | + $this->assertSame('string', $reflectionEnum->getBackingType()->getName()); |
60 | 62 |
|
61 | 63 | $this->assertEqualsCanonicalizing(
|
62 | 64 | ['Hans', 'Dieter'],
|
@@ -93,6 +95,50 @@ public function testInvalidStringOnlyEnumValue(): void
|
93 | 95 | new $className(['property' => 'Meier']);
|
94 | 96 | }
|
95 | 97 |
|
| 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 | + |
96 | 142 | /**
|
97 | 143 | * @dataProvider unmappedEnumThrowsAnExceptionDataProvider
|
98 | 144 | */
|
@@ -131,11 +177,14 @@ public function testInvalidEnumMapThrowsAnException(string $enumValues, string $
|
131 | 177 | public function invalidEnumMapThrowsAnExceptionDataProvider(): array
|
132 | 178 | {
|
133 | 179 | 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"}'], |
139 | 188 | ];
|
140 | 189 | }
|
141 | 190 |
|
|
0 commit comments