@@ -387,29 +387,60 @@ public function testGetClassType(): void
387
387
$ this ->assertSame (['$ref ' => 'ref ' ], $ typeFactory ->getType (new Type (Type::BUILTIN_TYPE_OBJECT , false , Dummy::class), 'jsonld ' , true , ['foo ' => 'bar ' ], new Schema ()));
388
388
}
389
389
390
- public function testGetClassTypeWithNullability (): void
390
+ /** @dataProvider classTypeWithNullabilityDataProvider */
391
+ public function testGetClassTypeWithNullability (array $ expected , SchemaFactoryInterface $ schemaFactory , Schema $ schema ): void
392
+ {
393
+ $ typeFactory = new TypeFactory ();
394
+ $ typeFactory ->setSchemaFactory ($ schemaFactory );
395
+
396
+ self ::assertSame (
397
+ $ expected ,
398
+ $ typeFactory ->getType (new Type (Type::BUILTIN_TYPE_OBJECT , true , Dummy::class), 'jsonld ' , true , ['foo ' => 'bar ' ], $ schema )
399
+ );
400
+ }
401
+
402
+ public function classTypeWithNullabilityDataProvider (): iterable
403
+ {
404
+ $ schemaFactory = $ this ->createSchemaFactoryMock ($ schema = new Schema ());
405
+
406
+ yield 'JSON-Schema version ' => [
407
+ [
408
+ 'anyOf ' => [
409
+ ['$ref ' => 'the-ref-name ' ],
410
+ ['type ' => 'null ' ],
411
+ ],
412
+ ],
413
+ $ schemaFactory ,
414
+ $ schema ,
415
+ ];
416
+
417
+ $ schemaFactory = $ this ->createSchemaFactoryMock ($ schema = new Schema (Schema::VERSION_OPENAPI ));
418
+
419
+ yield 'OpenAPI < 3.1 version ' => [
420
+ [
421
+ 'anyOf ' => [
422
+ ['$ref ' => 'the-ref-name ' ],
423
+ ],
424
+ 'nullable ' => true ,
425
+ ],
426
+ $ schemaFactory ,
427
+ $ schema ,
428
+ ];
429
+ }
430
+
431
+ private function createSchemaFactoryMock (Schema $ schema ): SchemaFactoryInterface
391
432
{
392
433
$ schemaFactory = $ this ->createMock (SchemaFactoryInterface::class);
393
434
394
435
$ schemaFactory
395
436
->method ('buildSchema ' )
396
- ->willReturnCallback (static function (): Schema {
397
- $ schema = new Schema ();
398
-
437
+ ->willReturnCallback (static function () use ($ schema ): Schema {
399
438
$ schema ['$ref ' ] = 'the-ref-name ' ;
400
439
$ schema ['description ' ] = 'more stuff here ' ;
401
440
402
441
return $ schema ;
403
442
});
404
443
405
- $ typeFactory = new TypeFactory ();
406
- $ typeFactory ->setSchemaFactory ($ schemaFactory );
407
-
408
- self ::assertSame ([
409
- 'nullable ' => true ,
410
- 'anyOf ' => [
411
- ['$ref ' => 'the-ref-name ' ],
412
- ],
413
- ], $ typeFactory ->getType (new Type (Type::BUILTIN_TYPE_OBJECT , true , Dummy::class), 'jsonld ' , true , ['foo ' => 'bar ' ], new Schema ()));
444
+ return $ schemaFactory ;
414
445
}
415
446
}
0 commit comments