@@ -69,8 +69,9 @@ public function propertyLevelAnyOfSchemaFileDataProvider(): array
69
69
return [
70
70
'Scalar types ' => ['AnyOfType.json ' ],
71
71
'Property level composition ' => ['ExtendedPropertyDefinition.json ' ],
72
- 'Object with scalar type ' => ['ReferencedObjectSchema.json ' ],
72
+ 'Object with scalar type and one object ' => ['ReferencedObjectSchema.json ' ],
73
73
'Multiple objects ' => ['ReferencedObjectSchema2.json ' ],
74
+ 'Object with scalar type and multiple objects ' => ['ReferencedObjectSchema3.json ' ],
74
75
'Empty any of ' => ['EmptyAnyOf.json ' ],
75
76
];
76
77
}
@@ -122,14 +123,17 @@ public function testValidProvidedAnyOfTypePropertyIsValid($propertyValue): void
122
123
*
123
124
* @param string $schema
124
125
* @param string $annotationPattern
126
+ * @param int $generatedClasses
125
127
*/
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 {
128
133
$ className = $ this ->generateClassFromFile ($ schema );
129
134
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 ' ));
133
137
134
138
$ this ->assertCount ($ generatedClasses , $ this ->getGeneratedFiles ());
135
139
}
@@ -139,17 +143,27 @@ public function annotationDataProvider(): array
139
143
return [
140
144
'Multiple scalar types (no merged property) ' => [
141
145
'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$/ ' ,
143
152
1 ,
144
153
],
145
154
'Object with scalar type (no merged property - redirect to generated object) ' => [
146
155
'ReferencedObjectSchema.json ' ,
147
- '/string\|ComposedAnyOfTest[\w]*Property[\w]*\|null/ ' ,
156
+ '/^ string\|ComposedAnyOfTest[\w]*Property[\w]*\|null$ / ' ,
148
157
2 ,
149
158
],
150
159
'Multiple objects (merged property created) ' => [
151
160
'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$/ ' ,
153
167
4 ,
154
168
],
155
169
];
@@ -288,22 +302,29 @@ public function invalidExtendedPropertyDataProvider(): array
288
302
/**
289
303
* @dataProvider composedPropertyWithReferencedSchemaDataProvider
290
304
*
305
+ * @param string $schema
291
306
* @param $propertyValue
292
307
*/
293
- public function testMatchingComposedPropertyWithReferencedSchemaIsValid ($ propertyValue ): void
308
+ public function testMatchingComposedPropertyWithReferencedSchemaIsValid (string $ schema , $ propertyValue ): void
294
309
{
295
- $ className = $ this ->generateClassFromFile (' ReferencedObjectSchema.json ' );
310
+ $ className = $ this ->generateClassFromFile ($ schema );
296
311
297
312
$ object = new $ className (['property ' => $ propertyValue ]);
298
313
$ this ->assertSame ($ propertyValue , $ object ->getProperty ());
299
314
}
300
315
301
316
public function composedPropertyWithReferencedSchemaDataProvider (): array
302
317
{
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
+ );
307
328
}
308
329
309
330
/**
@@ -327,12 +348,26 @@ public function referencedPersonDataProvider(): array
327
348
return [
328
349
'ReferencedObjectSchema.json ' => ['ReferencedObjectSchema.json ' ],
329
350
'ReferencedObjectSchema2.json ' => ['ReferencedObjectSchema2.json ' ],
351
+ 'ReferencedObjectSchema3.json ' => ['ReferencedObjectSchema3.json ' ],
330
352
];
331
353
}
332
354
333
- public function testMatchingObjectPropertyWithReferencedPetSchemaIsValid (): void
355
+ public function referencedPetDataProvider (): array
334
356
{
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 );
336
371
337
372
$ object = new $ className (['property ' => ['race ' => 'Horse ' ]]);
338
373
@@ -381,28 +416,32 @@ public function invalidObjectPropertyWithReferencedPersonSchemaDataProvider(): a
381
416
/**
382
417
* @dataProvider invalidObjectPropertyWithReferencedPetSchemaDataProvider
383
418
*
419
+ * @param string $schema
384
420
* @param $propertyValue
385
421
*/
386
- public function testNotMatchingObjectPropertyWithReferencedPetSchemaThrowsAnException ($ propertyValue ): void
387
- {
422
+ public function testNotMatchingObjectPropertyWithReferencedPetSchemaThrowsAnException (
423
+ string $ schema ,
424
+ $ propertyValue
425
+ ): void {
388
426
$ this ->expectException (ValidationException::class);
389
427
$ this ->expectExceptionMessage ('Invalid value for property declined by composition constraint ' );
390
428
391
- $ className = $ this ->generateClassFromFile (' ReferencedObjectSchema2.json ' );
429
+ $ className = $ this ->generateClassFromFile ($ schema );
392
430
393
431
new $ className (['property ' => $ propertyValue ]);
394
432
}
395
433
396
434
public function invalidObjectPropertyWithReferencedPetSchemaDataProvider (): array
397
435
{
398
436
return $ this ->combineDataProvider (
399
- $ this ->referencedPersonDataProvider (),
437
+ $ this ->referencedPetDataProvider (),
400
438
[
401
439
'int ' => [0 ],
402
440
'float ' => [0.92 ],
403
441
'bool ' => [true ],
404
442
'object ' => [new stdClass ()],
405
- 'string ' => ['Horse ' ],
443
+ // a string is allowed by ReferencedObjectSchema3 but must be declined due to length violation
444
+ 'string ' => ['Cat ' ],
406
445
'empty array ' => [[]],
407
446
'Too many properties ' => [['race ' => 'Horse ' , 'alive ' => true ]],
408
447
'Matching object with invalid type ' => [['race ' => 123 ]],
0 commit comments