Skip to content

Commit 4b12989

Browse files
committed
Add test cases for identical reference resolving for multiple properties from the same object
1 parent 867a0c6 commit 4b12989

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

tests/Objects/ReferencePropertyTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace PHPModelGenerator\Tests\Objects;
44

5+
use PHPModelGenerator\Exception\ErrorRegistryException;
56
use PHPModelGenerator\Exception\FileSystemException;
67
use PHPModelGenerator\Exception\ValidationException;
78
use PHPModelGenerator\Exception\RenderException;
89
use PHPModelGenerator\Exception\SchemaException;
10+
use PHPModelGenerator\Model\GeneratorConfiguration;
911
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
1012
use stdClass;
1113

@@ -494,4 +496,82 @@ public function validBaseReferenceObjectInputProvider(): array
494496
]
495497
);
496498
}
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+
}
497577
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"definitions": {
3+
"person": {
4+
"type": "object",
5+
"properties": {
6+
"name": {
7+
"type": "string",
8+
"minLength": 3
9+
}
10+
}
11+
}
12+
},
13+
"type": "object",
14+
"properties": {
15+
"personA": { "$ref": "#/definitions/person" },
16+
"personB": { "$ref": "#/definitions/person" }
17+
}
18+
}

0 commit comments

Comments
 (0)