@@ -29,9 +29,10 @@ public function testNotProvidedPropertyLevelOneOfIsValid(string $schema): void
2929 public function propertyLevelOneOfSchemaFileDataProvider (): array
3030 {
3131 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 ' ],
3536 ];
3637 }
3738
@@ -72,6 +73,29 @@ public function testValidProvidedOneOfTypePropertyIsValid($propertyValue): void
7273 $ this ->assertSame ($ propertyValue , $ object ->getProperty ());
7374 }
7475
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+
7599 /**
76100 * @dataProvider invalidPropertyTypeDataProvider
77101 *
@@ -224,11 +248,13 @@ public function composedPropertyWithReferencedSchemaDataProvider(): array
224248 }
225249
226250 /**
227- * @param $propertyValue
251+ * @dataProvider referencedPersonDataProvider
252+ *
253+ * @param string $schema
228254 */
229- public function testMatchingObjectPropertyWithReferencedSchemaIsValid ( ): void
255+ public function testMatchingObjectPropertyWithReferencedPersonSchemaIsValid ( string $ schema ): void
230256 {
231- $ className = $ this ->generateClassFromFile (' ReferencedObjectSchema.json ' );
257+ $ className = $ this ->generateClassFromFile ($ schema );
232258
233259 $ object = new $ className (['property ' => ['name ' => 'Ha ' , 'age ' => 42 ]]);
234260
@@ -237,36 +263,93 @@ public function testMatchingObjectPropertyWithReferencedSchemaIsValid(): void
237263 $ this ->assertSame (42 , $ object ->getProperty ()->getAge ());
238264 }
239265
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+
240322 /**
241- * @dataProvider invalidObjectPropertyWithReferencedSchemaDataProvider
323+ * @dataProvider invalidObjectPropertyWithReferencedPetSchemaDataProvider
242324 *
243325 * @param $propertyValue
244326 */
245- public function testNotMatchingObjectPropertyWithReferencedSchemaThrowsAnException ($ propertyValue ): void
327+ public function testNotMatchingObjectPropertyWithReferencedPetSchemaThrowsAnException ($ propertyValue ): void
246328 {
247329 $ this ->expectException (InvalidArgumentException::class);
248330 $ this ->expectExceptionMessage ('Invalid value for property declined by composition constraint ' );
249331
250- $ className = $ this ->generateClassFromFile ('ReferencedObjectSchema .json ' );
332+ $ className = $ this ->generateClassFromFile ('ReferencedObjectSchema2 .json ' );
251333
252334 new $ className (['property ' => $ propertyValue ]);
253335 }
254336
255- public function invalidObjectPropertyWithReferencedSchemaDataProvider (): array
337+ public function invalidObjectPropertyWithReferencedPetSchemaDataProvider (): array
256338 {
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+ );
270353 }
271354
272355 /**
0 commit comments