Skip to content

Commit f80cb00

Browse files
committed
Add recursive array test
1 parent 1907602 commit f80cb00

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

src/Model/Property/AbstractProperty.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ public function onResolve(callable $callback): PropertyInterface
7272
return $this;
7373
}
7474

75-
public function isResolved(): bool
76-
{
77-
return $this->resolved;
78-
}
79-
8075
/**
8176
* Convert a name of a JSON-field into a valid PHP variable name to be used as class attribute
8277
*

src/Model/Property/PropertyInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,4 @@ public function getJsonSchema(): JsonSchema;
210210
* Adds a callback which will be executed after the property is set up completely
211211
*/
212212
public function onResolve(callable $callback): PropertyInterface;
213-
214-
/**
215-
* Check if the property set up is finished
216-
*/
217-
public function isResolved(): bool;
218213
}

tests/Objects/ArrayPropertyTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PHPModelGenerator\Tests\Objects;
44

5+
use PHPModelGenerator\Exception\Arrays\InvalidItemException;
6+
use PHPModelGenerator\Exception\Arrays\MinItemsException;
57
use PHPModelGenerator\Exception\FileSystemException;
68
use PHPModelGenerator\Exception\RenderException;
79
use PHPModelGenerator\Exception\SchemaException;
@@ -867,6 +869,7 @@ public function invalidObjectArrayDataProvider(): array
867869
)
868870
);
869871
}
872+
870873
public function invalidCombinedObjectArrayDataProvider(): array
871874
{
872875
return $this->combineDataProvider(
@@ -951,4 +954,49 @@ public function invalidCombinedObjectArrayDataProvider(): array
951954
)
952955
);
953956
}
957+
958+
/**
959+
* @dataProvider validRecursiveArrayDataProvider
960+
*/
961+
public function testValidRecursiveArray(array $input): void
962+
{
963+
$className = $this->generateClassFromFile('RecursiveArray.json');
964+
965+
$object = new $className(['property' => $input]);
966+
967+
$this->assertSame($input, $object->getProperty());
968+
}
969+
970+
public function validRecursiveArrayDataProvider(): array
971+
{
972+
return [
973+
'only string' => [['Hello']],
974+
'only nested array' => [[['Hello']]],
975+
'string and nested array' => [[['Hello'], 'World']],
976+
'two level nested array' => [[[['Hello'], 'World'], '!']],
977+
];
978+
}
979+
980+
/**
981+
* @dataProvider invalidRecursiveArrayDataProvider
982+
*/
983+
public function testInvalidRecursiveArrayThrowsAnException(string $expectedException, array $input): void
984+
{
985+
$this->expectException($expectedException);
986+
987+
$className = $this->generateClassFromFile('RecursiveArray.json');
988+
989+
new $className(['property' => $input]);
990+
}
991+
992+
public function invalidRecursiveArrayDataProvider(): array
993+
{
994+
return [
995+
'empty array' => [MinItemsException::class, []],
996+
'empty nested array' => [InvalidItemException::class, [[]]],
997+
'string with empty nested array' => [InvalidItemException::class, ['Hello', []]],
998+
'invalid type' => [InvalidItemException::class, [2]],
999+
'invalid nested type' => [InvalidItemException::class, ['Hello', [2]]],
1000+
];
1001+
}
9541002
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"definitions": {
3+
"list": {
4+
"type": "array",
5+
"items": {
6+
"oneOf": [
7+
{
8+
"$ref": "#/definitions/list"
9+
},
10+
{
11+
"type": "string"
12+
}
13+
]
14+
},
15+
"minItems": 1
16+
}
17+
},
18+
"type": "object",
19+
"properties": {
20+
"property": {
21+
"$ref": "#/definitions/list"
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)