Skip to content

Commit eca19ad

Browse files
committed
Finx wrong merge, add additional multi type type hint test
1 parent 4e9ede1 commit eca19ad

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

src/Templates/Model.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class {{ class }} implements \PHPModelGenerator\Interfaces\JSONModelInterface
102102
* {% if property.getTypeHint(true) %}@return {{ property.getTypeHint(true) }}{% endif %}
103103
*/
104104
public function get{{ viewHelper.ucfirst(property.getAttribute()) }}()
105-
{% if property.getType(true) %}: {% if viewHelper.implicitNull(property) %}?{% endif %}{{ property.getType(true) }}{% endif %}
105+
{% if property.getType(true) %}: {% if viewHelper.isPropertyNullable(property) %}?{% endif %}{{ property.getType(true) }}{% endif %}
106106
{
107107
return $this->{{ property.getAttribute() }};
108108
}

tests/AbstractPHPModelGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public function namespaceDataProvider(): array
408408
*
409409
* @return string
410410
*/
411-
protected function getPropertyType($object, string $property): string
411+
protected function getPropertyTypeAnnotation($object, string $property): string
412412
{
413413
$matches = [];
414414
preg_match(
@@ -428,7 +428,7 @@ protected function getPropertyType($object, string $property): string
428428
*
429429
* @return string
430430
*/
431-
protected function getMethodReturnType($object, string $method): string
431+
protected function getMethodReturnTypeAnnotation($object, string $method): string
432432
{
433433
$matches = [];
434434
preg_match(

tests/ComposedValue/ComposedAllOfTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public function testAllOfTypePropertyHasTypeAnnotation(): void
9797
$object = new $className([]);
9898
$regexp = '/ComposedAllOfTest[\w]*_Merged_[\w]*/';
9999

100-
$this->assertRegExp($regexp, $this->getPropertyType($object, 'property'));
101-
$this->assertRegExp($regexp, $this->getMethodReturnType($object, 'getProperty'));
100+
$this->assertRegExp($regexp, $this->getPropertyTypeAnnotation($object, 'property'));
101+
$this->assertRegExp($regexp, $this->getMethodReturnTypeAnnotation($object, 'getProperty'));
102102

103103
// base class, merged property class and two classes for validating the composition components
104104
$this->assertCount(4, $this->getGeneratedFiles());

tests/ComposedValue/ComposedAnyOfTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public function testAnyOfTypePropertyHasTypeAnnotation(
132132
): void {
133133
$className = $this->generateClassFromFile($schema);
134134

135-
$this->assertRegExp($annotationPattern, $this->getPropertyType($className, 'property'));
136-
$this->assertRegExp($annotationPattern, $this->getMethodReturnType($className, 'getProperty'));
135+
$this->assertRegExp($annotationPattern, $this->getPropertyTypeAnnotation($className, 'property'));
136+
$this->assertRegExp($annotationPattern, $this->getMethodReturnTypeAnnotation($className, 'getProperty'));
137137

138138
$this->assertCount($generatedClasses, $this->getGeneratedFiles());
139139
}
@@ -143,7 +143,7 @@ public function annotationDataProvider(): array
143143
return [
144144
'Multiple scalar types (no merged property)' => [
145145
'AnyOfType.json',
146-
'/^string\|int\|bool\|null$/',
146+
'/^null\|string\|int\|bool$/',
147147
1,
148148
],
149149
'Multiple scalar types required (no merged property)' => [
@@ -153,17 +153,17 @@ public function annotationDataProvider(): array
153153
],
154154
'Object with scalar type (no merged property - redirect to generated object)' => [
155155
'ReferencedObjectSchema.json',
156-
'/^string\|ComposedAnyOfTest[\w]*Property[\w]*\|null$/',
156+
'/^null\|string\|ComposedAnyOfTest[\w]*Property[\w]*$/',
157157
2,
158158
],
159159
'Multiple objects (merged property created)' => [
160160
'ReferencedObjectSchema2.json',
161-
'/^ComposedAnyOfTest[\w]*_Merged_[\w]*\|null$/',
161+
'/^null\|ComposedAnyOfTest[\w]*_Merged_[\w]*$/',
162162
4,
163163
],
164164
'Scalar type and multiple objects (merged property created)' => [
165165
'ReferencedObjectSchema3.json',
166-
'/^string\|ComposedAnyOfTest[\w]*_Merged_[\w]*\|null$/',
166+
'/^null\|string\|ComposedAnyOfTest[\w]*_Merged_[\w]*$/',
167167
4,
168168
],
169169
];

tests/ComposedValue/ComposedOneOfTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public function testOneOfTypePropertyHasTypeAnnotation(string $schema, string $a
130130
$className = $this->generateClassFromFile($schema);
131131

132132
$object = new $className([]);
133-
$this->assertRegExp($annotationPattern, $this->getPropertyType($object, 'property'));
134-
$this->assertRegExp($annotationPattern, $this->getMethodReturnType($object, 'getProperty'));
133+
$this->assertRegExp($annotationPattern, $this->getPropertyTypeAnnotation($object, 'property'));
134+
$this->assertRegExp($annotationPattern, $this->getMethodReturnTypeAnnotation($object, 'getProperty'));
135135
}
136136

137137
public function annotationDataProvider(): array

tests/Objects/MultiTypePropertyTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ public function testNotProvidedOptionalMultiTypePropertyIsValid(): void
2929
$this->assertNull($object->getProperty());
3030
}
3131

32+
/**
33+
* @dataProvider implicitNullDataProvider
34+
*/
35+
public function testMultiTypeAnnotation(bool $implicitNull): void
36+
{
37+
$className = $this->generateClassFromFile('MultiTypeProperty.json', null, false, $implicitNull);
38+
39+
$expectedTypeHint = $implicitNull ? 'null|float|string|array' : 'float|string|array';
40+
41+
$this->assertSame($expectedTypeHint, $this->getPropertyTypeAnnotation($className, 'property'));
42+
$this->assertSame($expectedTypeHint, $this->getMethodReturnTypeAnnotation($className, 'getProperty'));
43+
}
44+
3245
/**
3346
* @dataProvider validValueDataProvider
3447
*
@@ -40,9 +53,6 @@ public function testValidProvidedValuePassesValidation($propertyValue): void
4053

4154
$object = new $className(['property' => $propertyValue]);
4255
$this->assertEquals($propertyValue, $object->getProperty());
43-
44-
$this->assertSame('null|float|string|array', $this->getPropertyType($object, 'property'));
45-
$this->assertSame('null|float|string|array', $this->getMethodReturnType($object, 'getProperty'));
4656
}
4757

4858
public function validValueDataProvider(): array

0 commit comments

Comments
 (0)