|
7 | 7 | use BackedEnum;
|
8 | 8 | use Exception;
|
9 | 9 | use PHPModelGenerator\Exception\Generic\EnumException;
|
| 10 | +use PHPModelGenerator\Exception\Object\RequiredValueException; |
10 | 11 | use PHPModelGenerator\Exception\SchemaException;
|
11 | 12 | use PHPModelGenerator\Model\GeneratorConfiguration;
|
12 | 13 | use PHPModelGenerator\ModelGenerator;
|
@@ -458,6 +459,79 @@ public function differentEnumsDataProvider(): array
|
458 | 459 | ];
|
459 | 460 | }
|
460 | 461 |
|
| 462 | + /** |
| 463 | + * @requires PHP >= 8.1 |
| 464 | + */ |
| 465 | + public function testDefaultValue(): void |
| 466 | + { |
| 467 | + $this->addPostProcessor(); |
| 468 | + |
| 469 | + $className = $this->generateClassFromFile('EnumPropertyDefaultValue.json'); |
| 470 | + |
| 471 | + $this->includeGeneratedEnums(1); |
| 472 | + |
| 473 | + $object = new $className(); |
| 474 | + $this->assertSame('Dieter', $object->getProperty()->value); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * @requires PHP >= 8.1 |
| 479 | + */ |
| 480 | + public function testNotProvidedRequiredEnumThrowsAnException(): void |
| 481 | + { |
| 482 | + $this->addPostProcessor(); |
| 483 | + |
| 484 | + $className = $this->generateClassFromFile('EnumPropertyRequired.json'); |
| 485 | + |
| 486 | + $this->includeGeneratedEnums(1); |
| 487 | + |
| 488 | + $this->expectException(RequiredValueException::class); |
| 489 | + $this->expectExceptionMessage('Missing required value for property'); |
| 490 | + |
| 491 | + new $className(); |
| 492 | + } |
| 493 | + |
| 494 | + /** |
| 495 | + * @requires PHP >= 8.1 |
| 496 | + */ |
| 497 | + public function testRequiredEnum(): void |
| 498 | + { |
| 499 | + $this->addPostProcessor(); |
| 500 | + |
| 501 | + $className = $this->generateClassFromFile( |
| 502 | + 'EnumPropertyRequired.json', |
| 503 | + (new GeneratorConfiguration())->setImmutable(false)->setCollectErrors(false) |
| 504 | + ); |
| 505 | + |
| 506 | + $this->includeGeneratedEnums(1); |
| 507 | + |
| 508 | + $object = new $className(['property' => 'Dieter']); |
| 509 | + $this->assertSame('Dieter', $object->getProperty()->value); |
| 510 | + |
| 511 | + $returnType = $this->getReturnType($object, 'getProperty'); |
| 512 | + $this->assertFalse($returnType->allowsNull()); |
| 513 | + $enum = $returnType->getName(); |
| 514 | + |
| 515 | + $reflectionEnum = new ReflectionEnum($enum); |
| 516 | + $enumName = $reflectionEnum->getShortName(); |
| 517 | + |
| 518 | + $this->assertSame($enumName, $this->getReturnTypeAnnotation($object, 'getProperty')); |
| 519 | + |
| 520 | + $object->setProperty($enum::Hans); |
| 521 | + $this->assertSame('Hans', $object->getProperty()->value); |
| 522 | + |
| 523 | + $this->assertNull($this->getParameterType($object, 'setProperty')); |
| 524 | + $this->assertEqualsCanonicalizing( |
| 525 | + [$enumName, 'string'], |
| 526 | + explode('|', $this->getParameterTypeAnnotation($object, 'setProperty')) |
| 527 | + ); |
| 528 | + |
| 529 | + $this->expectException(EnumException::class); |
| 530 | + $this->expectExceptionMessage('Invalid value for property declined by enum constraint'); |
| 531 | + |
| 532 | + $object->setProperty(null); |
| 533 | + } |
| 534 | + |
461 | 535 | private function addPostProcessor(): void
|
462 | 536 | {
|
463 | 537 | $this->modifyModelGenerator = static function (ModelGenerator $generator): void {
|
|
0 commit comments