Skip to content

Commit 1ae4cf1

Browse files
committed
Add recursive multi type tests
1 parent 4c9986f commit 1ae4cf1

File tree

2 files changed

+92
-24
lines changed

2 files changed

+92
-24
lines changed

src/PropertyProcessor/Property/MultiTypeProcessor.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,36 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
8585

8686
$subProperties = $this->processSubProperties($propertyName, $propertySchema, $property);
8787

88-
if (empty($this->allowedPropertyTypes)) {
89-
return;
88+
$processedSubProperties = 0;
89+
foreach ($subProperties as $subProperty) {
90+
$subProperty->onResolve(function () use ($property, $subProperties, &$processedSubProperties) {
91+
if (++$processedSubProperties === count($subProperties)) {
92+
if (empty($this->allowedPropertyTypes)) {
93+
return;
94+
}
95+
96+
$property->addTypeHintDecorator(
97+
new TypeHintDecorator(
98+
array_map(
99+
static function (PropertyInterface $subProperty): string {
100+
return $subProperty->getTypeHint();
101+
},
102+
$subProperties
103+
)
104+
)
105+
);
106+
107+
$property->addValidator(
108+
new MultiTypeCheckValidator(
109+
array_unique($this->allowedPropertyTypes),
110+
$property,
111+
$this->isImplicitNullAllowed($property)
112+
),
113+
2
114+
);
115+
}
116+
});
90117
}
91-
92-
$property->addTypeHintDecorator(
93-
new TypeHintDecorator(
94-
array_map(
95-
static function (PropertyInterface $subProperty): string {
96-
return $subProperty->getTypeHint();
97-
},
98-
$subProperties
99-
)
100-
)
101-
);
102-
103-
$property->addValidator(
104-
new MultiTypeCheckValidator(
105-
array_unique($this->allowedPropertyTypes),
106-
$property,
107-
$this->isImplicitNullAllowed($property)
108-
),
109-
2
110-
);
111118
});
112119

113120
return $property;

tests/Objects/MultiTypePropertyTest.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace PHPModelGenerator\Tests\Objects;
44

5+
use PHPModelGenerator\Exception\Arrays\InvalidItemException;
6+
use PHPModelGenerator\Exception\Arrays\MinItemsException;
57
use PHPModelGenerator\Exception\FileSystemException;
8+
use PHPModelGenerator\Exception\Generic\InvalidTypeException;
69
use PHPModelGenerator\Exception\RenderException;
710
use PHPModelGenerator\Exception\SchemaException;
811
use PHPModelGenerator\Model\GeneratorConfiguration;
@@ -229,8 +232,66 @@ public function validRecursiveMultiTypeDataProvider(): array
229232
{
230233
return [
231234
'string' => ['Test'],
232-
# 'array' => [['Test1', 'Test2']],
233-
# 'nested array' => [[['Test1', 'Test2'], 'Test3']],
235+
'array' => [['Test1', 'Test2']],
236+
'nested array' => [[['Test1', 'Test2'], 'Test3']],
237+
];
238+
}
239+
240+
/**
241+
* @dataProvider invalidRecursiveMultiTypeDataProvider
242+
*/
243+
public function testInvalidRecursiveMultiType($input, string $expectedException, string $exceptionMessage): void
244+
{
245+
$this->expectException($expectedException);
246+
$this->expectExceptionMessage($exceptionMessage);
247+
248+
$className = $this->generateClassFromFile('RecursiveMultiTypeProperty.json');
249+
250+
new $className(['property' => $input]);
251+
}
252+
253+
public function invalidRecursiveMultiTypeDataProvider(): array
254+
{
255+
return [
256+
'int' => [
257+
1,
258+
InvalidTypeException::class,
259+
'Invalid type for property. Requires [string, array], got integer',
260+
],
261+
'invalid item in array' => [
262+
['Test1', 1],
263+
InvalidItemException::class,
264+
<<<ERROR
265+
Invalid items in array item of array property:
266+
- invalid item #1
267+
* Invalid type for item of array property. Requires [string, array], got integer
268+
ERROR
269+
],
270+
'invalid array length' => [
271+
[],
272+
MinItemsException::class,
273+
'Array property must not contain less than 2 items',
274+
],
275+
'invalid item in nested array' => [
276+
['Test1', [3, 'Test3']],
277+
InvalidItemException::class,
278+
<<<ERROR
279+
Invalid items in array item of array property:
280+
- invalid item #1
281+
* Invalid items in array item of array property:
282+
- invalid item #0
283+
* Invalid type for item of array property. Requires [string, array], got integer
284+
ERROR
285+
],
286+
'invalid array length in nested array' => [
287+
['Test1', []],
288+
InvalidItemException::class,
289+
<<<ERROR
290+
Invalid items in array item of array property:
291+
- invalid item #1
292+
* Array item of array property must not contain less than 2 items
293+
ERROR
294+
],
234295
];
235296
}
236297
}

0 commit comments

Comments
 (0)