|
2 | 2 |
|
3 | 3 | namespace PHPModelGenerator\Tests\Objects;
|
4 | 4 |
|
| 5 | +use PHPModelGenerator\Exception\ErrorRegistryException; |
5 | 6 | use PHPModelGenerator\Exception\FileSystemException;
|
6 | 7 | use PHPModelGenerator\Exception\ValidationException;
|
7 | 8 | use PHPModelGenerator\Exception\RenderException;
|
8 | 9 | use PHPModelGenerator\Exception\SchemaException;
|
| 10 | +use PHPModelGenerator\Model\GeneratorConfiguration; |
9 | 11 | use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
|
10 | 12 | use stdClass;
|
11 | 13 |
|
@@ -494,4 +496,82 @@ public function validBaseReferenceObjectInputProvider(): array
|
494 | 496 | ]
|
495 | 497 | );
|
496 | 498 | }
|
| 499 | + |
| 500 | + public function testMultiplePropertiesWithIdenticalReference(): void |
| 501 | + { |
| 502 | + $className = $this->generateClassFromFile('multiplePropertiesIdenticalReference.json'); |
| 503 | + |
| 504 | + $object = new $className([ |
| 505 | + 'personA' => ['name' => 'Hannes'], |
| 506 | + 'personB' => ['name' => 'Susi']] |
| 507 | + ); |
| 508 | + |
| 509 | + $this->assertTrue(is_callable([$object, 'getPersonA'])); |
| 510 | + $this->assertTrue(is_callable([$object, 'getPersonB'])); |
| 511 | + |
| 512 | + // test if the properties are typed correctly |
| 513 | + $returnTypePersonA = $this->getReturnType($object, 'getPersonA'); |
| 514 | + $returnTypePersonB = $this->getReturnType($object, 'getPersonB'); |
| 515 | + $this->assertSame($returnTypePersonA->getName(), $returnTypePersonB->getName()); |
| 516 | + // as the property is optional they may contain an initial null value |
| 517 | + $this->assertTrue($returnTypePersonA->allowsNull()); |
| 518 | + $this->assertTrue($returnTypePersonB->allowsNull()); |
| 519 | + |
| 520 | + $this->assertSame('Hannes', $object->getPersonA()->getName()); |
| 521 | + $this->assertSame('Susi', $object->getPersonB()->getName()); |
| 522 | + } |
| 523 | + |
| 524 | + /** |
| 525 | + * @dataProvider invalidValuesForMultiplePropertiesWithIdenticalReferenceDataProvider |
| 526 | + * |
| 527 | + * @param array $input |
| 528 | + * @param string $exceptionMessage |
| 529 | + */ |
| 530 | + public function testInvalidValuesForMultiplePropertiesWithIdenticalReferenceThrowsAnException( |
| 531 | + array $input, |
| 532 | + string $exceptionMessage |
| 533 | + ): void { |
| 534 | + $this->expectException(ErrorRegistryException::class); |
| 535 | + $this->expectExceptionMessage($exceptionMessage); |
| 536 | + |
| 537 | + $className = $this->generateClassFromFile( |
| 538 | + 'multiplePropertiesIdenticalReference.json', |
| 539 | + (new GeneratorConfiguration())->setCollectErrors(true) |
| 540 | + ); |
| 541 | + |
| 542 | + new $className($input); |
| 543 | + } |
| 544 | + |
| 545 | + public function invalidValuesForMultiplePropertiesWithIdenticalReferenceDataProvider(): array { |
| 546 | + return [ |
| 547 | + 'Invalid value for personA' => [ |
| 548 | + ['personA' => 10], |
| 549 | + 'Invalid type for personA. Requires object, got integer', |
| 550 | + ], |
| 551 | + 'Invalid value for both persons' => [ |
| 552 | + ['personA' => 10, 'personB' => false], |
| 553 | + <<<ERROR |
| 554 | +Invalid type for personA. Requires object, got integer |
| 555 | +Invalid type for personB. Requires object, got boolean |
| 556 | +ERROR |
| 557 | + ], |
| 558 | + 'Invalid names for personB' => [ |
| 559 | + ['personA' => ['name' => 'A'], 'personB' => ['name' => 10]], |
| 560 | + <<<ERROR |
| 561 | +Invalid nested object for property personA: |
| 562 | + - Value for name must not be shorter than 3 |
| 563 | +Invalid nested object for property personB: |
| 564 | + - Invalid type for name. Requires string, got integer |
| 565 | +ERROR |
| 566 | + ], |
| 567 | + 'Combined top level validation error and nested error' => [ |
| 568 | + ['personA' => ['name' => 'A'], 'personB' => 10], |
| 569 | + <<<ERROR |
| 570 | +Invalid nested object for property personA: |
| 571 | + - Value for name must not be shorter than 3 |
| 572 | +Invalid type for personB. Requires object, got integer |
| 573 | +ERROR |
| 574 | + ], |
| 575 | + ]; |
| 576 | + } |
497 | 577 | }
|
0 commit comments