@@ -29,9 +29,10 @@ public function testNotProvidedPropertyLevelOneOfIsValid(string $schema): void
29
29
public function propertyLevelOneOfSchemaFileDataProvider (): array
30
30
{
31
31
return [
32
- 'OneOfType.json ' => ['OneOfType.json ' ],
33
- 'ExtendedPropertyDefinition.json ' => ['ExtendedPropertyDefinition.json ' ],
34
- 'ReferencedObjectSchema.json ' => ['ReferencedObjectSchema.json ' ],
32
+ 'Scalar types ' => ['OneOfType.json ' ],
33
+ 'Property level composition ' => ['ExtendedPropertyDefinition.json ' ],
34
+ 'Object with scalar type ' => ['ReferencedObjectSchema.json ' ],
35
+ 'Multiple objects ' => ['ReferencedObjectSchema2.json ' ],
35
36
];
36
37
}
37
38
@@ -72,6 +73,29 @@ public function testValidProvidedOneOfTypePropertyIsValid($propertyValue): void
72
73
$ this ->assertSame ($ propertyValue , $ object ->getProperty ());
73
74
}
74
75
76
+ /**
77
+ * @dataProvider annotationDataProvider
78
+ *
79
+ * @param string $schema
80
+ * @param string $annotationPattern
81
+ */
82
+ public function testOneOfTypePropertyHasTypeAnnotation (string $ schema , string $ annotationPattern ): void
83
+ {
84
+ $ className = $ this ->generateClassFromFile ($ schema );
85
+
86
+ $ object = new $ className ([]);
87
+ $ this ->assertRegExp ($ annotationPattern , $ this ->getPropertyType ($ object , 'property ' ));
88
+ }
89
+
90
+ public function annotationDataProvider (): array
91
+ {
92
+ return [
93
+ 'Multiple scalar types ' => ['OneOfType.json ' , '/string\|int\|bool/ ' ],
94
+ 'Object with scalar type ' => ['ReferencedObjectSchema.json ' , '/ComposedOneOfTest[\w]*\|string/ ' ],
95
+ 'Multiple objects ' => ['ReferencedObjectSchema2.json ' , '/ComposedOneOfTest[\w]*\|ComposedOneOfTest[\w]*/ ' ]
96
+ ];
97
+ }
98
+
75
99
/**
76
100
* @dataProvider invalidPropertyTypeDataProvider
77
101
*
@@ -224,11 +248,13 @@ public function composedPropertyWithReferencedSchemaDataProvider(): array
224
248
}
225
249
226
250
/**
227
- * @param $propertyValue
251
+ * @dataProvider referencedPersonDataProvider
252
+ *
253
+ * @param string $schema
228
254
*/
229
- public function testMatchingObjectPropertyWithReferencedSchemaIsValid ( ): void
255
+ public function testMatchingObjectPropertyWithReferencedPersonSchemaIsValid ( string $ schema ): void
230
256
{
231
- $ className = $ this ->generateClassFromFile (' ReferencedObjectSchema.json ' );
257
+ $ className = $ this ->generateClassFromFile ($ schema );
232
258
233
259
$ object = new $ className (['property ' => ['name ' => 'Ha ' , 'age ' => 42 ]]);
234
260
@@ -237,36 +263,93 @@ public function testMatchingObjectPropertyWithReferencedSchemaIsValid(): void
237
263
$ this ->assertSame (42 , $ object ->getProperty ()->getAge ());
238
264
}
239
265
266
+ public function referencedPersonDataProvider (): array
267
+ {
268
+ return [
269
+ 'ReferencedObjectSchema.json ' => ['ReferencedObjectSchema.json ' ],
270
+ 'ReferencedObjectSchema2.json ' => ['ReferencedObjectSchema2.json ' ],
271
+ ];
272
+ }
273
+
274
+ public function testMatchingObjectPropertyWithReferencedPetSchemaIsValid (): void
275
+ {
276
+ $ className = $ this ->generateClassFromFile ('ReferencedObjectSchema2.json ' );
277
+
278
+ $ object = new $ className (['property ' => ['race ' => 'Horse ' ]]);
279
+
280
+ $ this ->assertTrue (is_object ($ object ->getProperty ()));
281
+ $ this ->assertSame ('Horse ' , $ object ->getProperty ()->getRace ());
282
+ }
283
+
284
+ /**
285
+ * @dataProvider invalidObjectPropertyWithReferencedPersonSchemaDataProvider
286
+ *
287
+ * @param string $schema
288
+ * @param $propertyValue
289
+ */
290
+ public function testNotMatchingObjectPropertyWithReferencedPersonSchemaThrowsAnException (
291
+ string $ schema ,
292
+ $ propertyValue
293
+ ): void {
294
+ $ this ->expectException (InvalidArgumentException::class);
295
+ $ this ->expectExceptionMessage ('Invalid value for property declined by composition constraint ' );
296
+
297
+ $ className = $ this ->generateClassFromFile ($ schema );
298
+
299
+ new $ className (['property ' => $ propertyValue ]);
300
+ }
301
+
302
+ public function invalidObjectPropertyWithReferencedPersonSchemaDataProvider (): array
303
+ {
304
+ return $ this ->combineDataProvider (
305
+ $ this ->referencedPersonDataProvider (),
306
+ [
307
+ 'int ' => [0 ],
308
+ 'float ' => [0.92 ],
309
+ 'bool ' => [true ],
310
+ 'object ' => [new stdClass ()],
311
+ 'empty string ' => ['' ],
312
+ 'Too short string ' => ['Hann ' ],
313
+ 'empty array ' => [[]],
314
+ 'Missing property ' => [['name ' => 'Hannes ' ]],
315
+ 'Too many properties ' => [['name ' => 'Hannes ' , 'age ' => 42 , 'alive ' => true ]],
316
+ 'Matching object with invalid type ' => [['name ' => 'Hannes ' , 'age ' => '42 ' ]],
317
+ 'Matching object with invalid data ' => [['name ' => 'H ' , 'age ' => 42 ]],
318
+ ]
319
+ );
320
+ }
321
+
240
322
/**
241
- * @dataProvider invalidObjectPropertyWithReferencedSchemaDataProvider
323
+ * @dataProvider invalidObjectPropertyWithReferencedPetSchemaDataProvider
242
324
*
243
325
* @param $propertyValue
244
326
*/
245
- public function testNotMatchingObjectPropertyWithReferencedSchemaThrowsAnException ($ propertyValue ): void
327
+ public function testNotMatchingObjectPropertyWithReferencedPetSchemaThrowsAnException ($ propertyValue ): void
246
328
{
247
329
$ this ->expectException (InvalidArgumentException::class);
248
330
$ this ->expectExceptionMessage ('Invalid value for property declined by composition constraint ' );
249
331
250
- $ className = $ this ->generateClassFromFile ('ReferencedObjectSchema .json ' );
332
+ $ className = $ this ->generateClassFromFile ('ReferencedObjectSchema2 .json ' );
251
333
252
334
new $ className (['property ' => $ propertyValue ]);
253
335
}
254
336
255
- public function invalidObjectPropertyWithReferencedSchemaDataProvider (): array
337
+ public function invalidObjectPropertyWithReferencedPetSchemaDataProvider (): array
256
338
{
257
- return [
258
- 'int ' => [0 ],
259
- 'float ' => [0.92 ],
260
- 'bool ' => [true ],
261
- 'object ' => [new stdClass ()],
262
- 'empty string ' => ['' ],
263
- 'Too short string ' => ['Hann ' ],
264
- 'empty array ' => [[]],
265
- 'Missing property ' => [['name ' => 'Hannes ' ]],
266
- 'Too many properties ' => [['name ' => 'Hannes ' , 'age ' => 42 , 'alive ' => true ]],
267
- 'Matching object with invalid type ' => [['name ' => 'Hannes ' , 'age ' => '42 ' ]],
268
- 'Matching object with invalid data ' => [['name ' => 'H ' , 'age ' => 42 ]],
269
- ];
339
+ return $ this ->combineDataProvider (
340
+ $ this ->referencedPersonDataProvider (),
341
+ [
342
+ 'int ' => [0 ],
343
+ 'float ' => [0.92 ],
344
+ 'bool ' => [true ],
345
+ 'object ' => [new stdClass ()],
346
+ 'string ' => ['Horse ' ],
347
+ 'empty array ' => [[]],
348
+ 'Too many properties ' => [['race ' => 'Horse ' , 'alive ' => true ]],
349
+ 'Matching object with invalid type ' => [['race ' => 123 ]],
350
+ 'Matching object with invalid data ' => [['race ' => 'H ' ]],
351
+ ]
352
+ );
270
353
}
271
354
272
355
/**
0 commit comments