|
13 | 13 | use PHPModelGenerator\SchemaProcessor\PostProcessor\EnumPostProcessor;
|
14 | 14 | use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
|
15 | 15 | use ReflectionEnum;
|
| 16 | +use UnitEnum; |
16 | 17 |
|
17 |
| -// TODO: mixed enums, multiple enums, enum redirect |
| 18 | +// TODO: multiple enums, enum redirect |
18 | 19 | class EnumPostProcessorTest extends AbstractPHPModelGeneratorTest
|
19 | 20 | {
|
20 | 21 | /**
|
@@ -267,6 +268,65 @@ public function testIntOnlyEnum(): void
|
267 | 268 | $object->setProperty(1);
|
268 | 269 | }
|
269 | 270 |
|
| 271 | + /** |
| 272 | + * @requires PHP >= 8.1 |
| 273 | + */ |
| 274 | + public function testMixedEnum(): void |
| 275 | + { |
| 276 | + $this->addPostProcessor(); |
| 277 | + |
| 278 | + $className = $this->generateClassFromFileTemplate( |
| 279 | + 'EnumPropertyMapped.json', |
| 280 | + ['["Hans", 100, true]', '{"a": "Hans", "b": 100, "c": true}'], |
| 281 | + (new GeneratorConfiguration())->setImmutable(false)->setCollectErrors(false), |
| 282 | + false |
| 283 | + ); |
| 284 | + |
| 285 | + $this->includeGeneratedEnums(1); |
| 286 | + |
| 287 | + $object = new $className(['property' => 'Hans']); |
| 288 | + $this->assertSame('Hans', $object->getProperty()->value()); |
| 289 | + |
| 290 | + $object->setProperty(100); |
| 291 | + $this->assertSame(100, $object->getProperty()->value()); |
| 292 | + |
| 293 | + $object->setProperty(null); |
| 294 | + $this->assertNull($object->getProperty()); |
| 295 | + |
| 296 | + $returnType = $this->getReturnType($object, 'getProperty'); |
| 297 | + $this->assertTrue($returnType->allowsNull()); |
| 298 | + $enum = $returnType->getName(); |
| 299 | + |
| 300 | + $this->assertTrue(enum_exists($enum)); |
| 301 | + $reflectionEnum = new ReflectionEnum($enum); |
| 302 | + $enumName = $reflectionEnum->getShortName(); |
| 303 | + |
| 304 | + $this->assertEqualsCanonicalizing( |
| 305 | + [$enumName, 'null'], |
| 306 | + explode('|', $this->getReturnTypeAnnotation($object, 'getProperty')) |
| 307 | + ); |
| 308 | + |
| 309 | + $this->assertNull($reflectionEnum->getBackingType()); |
| 310 | + |
| 311 | + $this->assertEqualsCanonicalizing( |
| 312 | + ['A', 'B', 'C'], |
| 313 | + array_map(function (UnitEnum $value): string { return $value->name; }, $enum::cases()) |
| 314 | + ); |
| 315 | + |
| 316 | + $object->setProperty($enum::C); |
| 317 | + $this->assertSame(true, $object->getProperty()->value()); |
| 318 | + |
| 319 | + $this->assertNull($this->getParameterType($object, 'setProperty')); |
| 320 | + |
| 321 | + $this->assertSame($enum::A, $enum::from('Hans')); |
| 322 | + $this->assertSame($enum::A, $enum::tryFrom('Hans')); |
| 323 | + $this->assertNull($enum::tryFrom('Dieter')); |
| 324 | + |
| 325 | + $this->expectException(EnumException::class); |
| 326 | + $this->expectExceptionMessage('Invalid value for property declined by enum constraint'); |
| 327 | + $object->setProperty(1); |
| 328 | + } |
| 329 | + |
270 | 330 | /**
|
271 | 331 | * @requires PHP >= 8.1
|
272 | 332 | */
|
|
0 commit comments