Skip to content

Commit 65d50fc

Browse files
committed
Added additional test cases for a composition containing multiple elements with nested schemas and elements without nested schema
1 parent 30eb113 commit 65d50fc

File tree

4 files changed

+118
-28
lines changed

4 files changed

+118
-28
lines changed

docs/source/combinedSchemas/mergedProperty.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Generated interface:
6262
public function getAge(): ?int
6363
public function setAge(?int $name): self
6464
65-
If our composition is defined on object level the object will gain access to all properties of the combined schemas:
65+
If your composition is defined on object level the object will gain access to all properties of the combined schemas:
6666

6767
.. code-block:: json
6868
@@ -89,7 +89,7 @@ If our composition is defined on object level the object will gain access to all
8989
]
9090
}
9191
92-
This schema will generate three classes as no merged property will be created. The main class will be `CEO` and two classes will be generated to validate the subschemas combined with the `allOf` independent:
92+
This schema will generate three classes as no merged property is created. The main class will be `CEO` and two classes will be generated to validate the subschemas combined with the `allOf` independent:
9393

9494
* Ceo.php
9595
* Ceo_Ceo5e4a82e39edc3.php

tests/AbstractPHPModelGeneratorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,12 @@ public function namespaceDataProvider(): array
403403
/**
404404
* Get the annotated type for an object property
405405
*
406-
* @param object $object
406+
* @param string|object $object
407407
* @param string $property
408408
*
409409
* @return string
410410
*/
411-
protected function getPropertyType(object $object, string $property): string
411+
protected function getPropertyType($object, string $property): string
412412
{
413413
$matches = [];
414414
preg_match(
@@ -423,12 +423,12 @@ protected function getPropertyType(object $object, string $property): string
423423
/**
424424
* Get the annotated return type for an object method
425425
*
426-
* @param object $object
426+
* @param string|object $object
427427
* @param string $method
428428
*
429429
* @return string
430430
*/
431-
protected function getMethodReturnType(object $object, string $method): string
431+
protected function getMethodReturnType($object, string $method): string
432432
{
433433
$matches = [];
434434
preg_match(

tests/ComposedValue/ComposedAnyOfTest.php

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public function propertyLevelAnyOfSchemaFileDataProvider(): array
6969
return [
7070
'Scalar types' => ['AnyOfType.json'],
7171
'Property level composition' => ['ExtendedPropertyDefinition.json'],
72-
'Object with scalar type' => ['ReferencedObjectSchema.json'],
72+
'Object with scalar type and one object' => ['ReferencedObjectSchema.json'],
7373
'Multiple objects' => ['ReferencedObjectSchema2.json'],
74+
'Object with scalar type and multiple objects' => ['ReferencedObjectSchema3.json'],
7475
'Empty any of' => ['EmptyAnyOf.json'],
7576
];
7677
}
@@ -122,14 +123,17 @@ public function testValidProvidedAnyOfTypePropertyIsValid($propertyValue): void
122123
*
123124
* @param string $schema
124125
* @param string $annotationPattern
126+
* @param int $generatedClasses
125127
*/
126-
public function testAnyOfTypePropertyHasTypeAnnotation(string $schema, string $annotationPattern, int $generatedClasses): void
127-
{
128+
public function testAnyOfTypePropertyHasTypeAnnotation(
129+
string $schema,
130+
string $annotationPattern,
131+
int $generatedClasses
132+
): void {
128133
$className = $this->generateClassFromFile($schema);
129134

130-
$object = new $className([]);
131-
$this->assertRegExp($annotationPattern, $this->getPropertyType($object, 'property'));
132-
$this->assertRegExp($annotationPattern, $this->getMethodReturnType($object, 'getProperty'));
135+
$this->assertRegExp($annotationPattern, $this->getPropertyType($className, 'property'));
136+
$this->assertRegExp($annotationPattern, $this->getMethodReturnType($className, 'getProperty'));
133137

134138
$this->assertCount($generatedClasses, $this->getGeneratedFiles());
135139
}
@@ -139,17 +143,27 @@ public function annotationDataProvider(): array
139143
return [
140144
'Multiple scalar types (no merged property)' => [
141145
'AnyOfType.json',
142-
'/string\|int\|bool\|null/',
146+
'/^string\|int\|bool\|null$/',
147+
1,
148+
],
149+
'Multiple scalar types required (no merged property)' => [
150+
'AnyOfTypeRequired.json',
151+
'/^string\|int\|bool$/',
143152
1,
144153
],
145154
'Object with scalar type (no merged property - redirect to generated object)' => [
146155
'ReferencedObjectSchema.json',
147-
'/string\|ComposedAnyOfTest[\w]*Property[\w]*\|null/',
156+
'/^string\|ComposedAnyOfTest[\w]*Property[\w]*\|null$/',
148157
2,
149158
],
150159
'Multiple objects (merged property created)' => [
151160
'ReferencedObjectSchema2.json',
152-
'/ComposedAnyOfTest[\w]*_Merged_[\w]*\|null/',
161+
'/^ComposedAnyOfTest[\w]*_Merged_[\w]*\|null$/',
162+
4,
163+
],
164+
'Scalar type and multiple objects (merged property created)' => [
165+
'ReferencedObjectSchema3.json',
166+
'/^string\|ComposedAnyOfTest[\w]*_Merged_[\w]*\|null$/',
153167
4,
154168
],
155169
];
@@ -288,22 +302,29 @@ public function invalidExtendedPropertyDataProvider(): array
288302
/**
289303
* @dataProvider composedPropertyWithReferencedSchemaDataProvider
290304
*
305+
* @param string $schema
291306
* @param $propertyValue
292307
*/
293-
public function testMatchingComposedPropertyWithReferencedSchemaIsValid($propertyValue): void
308+
public function testMatchingComposedPropertyWithReferencedSchemaIsValid(string $schema, $propertyValue): void
294309
{
295-
$className = $this->generateClassFromFile('ReferencedObjectSchema.json');
310+
$className = $this->generateClassFromFile($schema);
296311

297312
$object = new $className(['property' => $propertyValue]);
298313
$this->assertSame($propertyValue, $object->getProperty());
299314
}
300315

301316
public function composedPropertyWithReferencedSchemaDataProvider(): array
302317
{
303-
return [
304-
'null' => [null],
305-
'string matching required length' => ['Hanne'],
306-
];
318+
return $this->combineDataProvider(
319+
[
320+
'ReferencedObjectSchema.json' => ['ReferencedObjectSchema.json'],
321+
'ReferencedObjectSchema3.json' => ['ReferencedObjectSchema3.json'],
322+
],
323+
[
324+
'null' => [null],
325+
'string matching required length' => ['Hanne'],
326+
]
327+
);
307328
}
308329

309330
/**
@@ -327,12 +348,26 @@ public function referencedPersonDataProvider(): array
327348
return [
328349
'ReferencedObjectSchema.json' => ['ReferencedObjectSchema.json'],
329350
'ReferencedObjectSchema2.json' => ['ReferencedObjectSchema2.json'],
351+
'ReferencedObjectSchema3.json' => ['ReferencedObjectSchema3.json'],
330352
];
331353
}
332354

333-
public function testMatchingObjectPropertyWithReferencedPetSchemaIsValid(): void
355+
public function referencedPetDataProvider(): array
334356
{
335-
$className = $this->generateClassFromFile('ReferencedObjectSchema2.json');
357+
return [
358+
'ReferencedObjectSchema2.json' => ['ReferencedObjectSchema2.json'],
359+
'ReferencedObjectSchema3.json' => ['ReferencedObjectSchema3.json'],
360+
];
361+
}
362+
363+
/**
364+
* @dataProvider referencedPetDataProvider
365+
*
366+
* @param string $schema
367+
*/
368+
public function testMatchingObjectPropertyWithReferencedPetSchemaIsValid(string $schema): void
369+
{
370+
$className = $this->generateClassFromFile($schema);
336371

337372
$object = new $className(['property' => ['race' => 'Horse']]);
338373

@@ -381,28 +416,32 @@ public function invalidObjectPropertyWithReferencedPersonSchemaDataProvider(): a
381416
/**
382417
* @dataProvider invalidObjectPropertyWithReferencedPetSchemaDataProvider
383418
*
419+
* @param string $schema
384420
* @param $propertyValue
385421
*/
386-
public function testNotMatchingObjectPropertyWithReferencedPetSchemaThrowsAnException($propertyValue): void
387-
{
422+
public function testNotMatchingObjectPropertyWithReferencedPetSchemaThrowsAnException(
423+
string $schema,
424+
$propertyValue
425+
): void {
388426
$this->expectException(ValidationException::class);
389427
$this->expectExceptionMessage('Invalid value for property declined by composition constraint');
390428

391-
$className = $this->generateClassFromFile('ReferencedObjectSchema2.json');
429+
$className = $this->generateClassFromFile($schema);
392430

393431
new $className(['property' => $propertyValue]);
394432
}
395433

396434
public function invalidObjectPropertyWithReferencedPetSchemaDataProvider(): array
397435
{
398436
return $this->combineDataProvider(
399-
$this->referencedPersonDataProvider(),
437+
$this->referencedPetDataProvider(),
400438
[
401439
'int' => [0],
402440
'float' => [0.92],
403441
'bool' => [true],
404442
'object' => [new stdClass()],
405-
'string' => ['Horse'],
443+
// a string is allowed by ReferencedObjectSchema3 but must be declined due to length violation
444+
'string' => ['Cat'],
406445
'empty array' => [[]],
407446
'Too many properties' => [['race' => 'Horse', 'alive' => true]],
408447
'Matching object with invalid type' => [['race' => 123]],
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"definitions": {
3+
"person": {
4+
"type": "object",
5+
"properties": {
6+
"name": {
7+
"type": "string",
8+
"minLength": 2
9+
},
10+
"age": {
11+
"type": "integer"
12+
}
13+
},
14+
"required": [
15+
"name",
16+
"age"
17+
],
18+
"additionalProperties": false
19+
},
20+
"pet": {
21+
"type": "object",
22+
"properties": {
23+
"race": {
24+
"type": "string",
25+
"minLength": 2
26+
}
27+
},
28+
"required": [
29+
"race"
30+
],
31+
"additionalProperties": false
32+
}
33+
},
34+
"type": "object",
35+
"properties": {
36+
"property": {
37+
"anyOf": [
38+
{
39+
"$ref": "#/definitions/person"
40+
},
41+
{
42+
"$ref": "#/definitions/pet"
43+
},
44+
{
45+
"type": "string",
46+
"minLength": 5
47+
}
48+
]
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)