1919use ApiPlatform \JsonSchema \DefinitionNameFactory ;
2020use ApiPlatform \JsonSchema \Schema ;
2121use ApiPlatform \JsonSchema \SchemaFactory as BaseSchemaFactory ;
22+ use ApiPlatform \Metadata \ApiProperty ;
2223use ApiPlatform \Metadata \ApiResource ;
2324use ApiPlatform \Metadata \Get ;
2425use ApiPlatform \Metadata \GetCollection ;
2829use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
2930use ApiPlatform \Metadata \Resource \ResourceMetadataCollection ;
3031use PHPUnit \Framework \TestCase ;
32+ use Prophecy \Argument ;
3133use Prophecy \PhpUnit \ProphecyTrait ;
3234
3335class SchemaFactoryTest extends TestCase
@@ -48,10 +50,12 @@ protected function setUp(): void
4850 );
4951
5052 $ propertyNameCollectionFactory = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
51- $ propertyNameCollectionFactory ->create (Dummy::class, ['enable_getter_setter_extraction ' => true , 'schema_type ' => Schema::TYPE_OUTPUT ])->willReturn (new PropertyNameCollection ());
53+ $ propertyNameCollectionFactory ->create (Dummy::class, ['enable_getter_setter_extraction ' => true , 'schema_type ' => Schema::TYPE_OUTPUT ])->willReturn (new PropertyNameCollection ([ ' id ' , ' name ' ] ));
5254 $ propertyMetadataFactory = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
55+ $ propertyMetadataFactory ->create (Dummy::class, 'id ' , Argument::type ('array ' ))->willReturn (new ApiProperty (identifier: true ));
56+ $ propertyMetadataFactory ->create (Dummy::class, 'name ' , Argument::type ('array ' ))->willReturn (new ApiProperty ());
5357
54- $ definitionNameFactory = new DefinitionNameFactory ([ ' jsonapi ' => true , ' jsonhal ' => true , ' jsonld ' => true ] );
58+ $ definitionNameFactory = new DefinitionNameFactory ();
5559
5660 $ baseSchemaFactory = new BaseSchemaFactory (
5761 resourceMetadataFactory: $ resourceMetadataFactoryCollection ->reveal (),
@@ -60,7 +64,12 @@ protected function setUp(): void
6064 definitionNameFactory: $ definitionNameFactory ,
6165 );
6266
63- $ this ->schemaFactory = new SchemaFactory ($ baseSchemaFactory );
67+ $ this ->schemaFactory = new SchemaFactory (
68+ $ baseSchemaFactory ,
69+ [],
70+ $ definitionNameFactory ,
71+ $ resourceMetadataFactoryCollection ->reveal (),
72+ );
6473 }
6574
6675 public function testBuildSchema (): void
@@ -86,12 +95,13 @@ public function testHasRootDefinitionKeyBuildSchema(): void
8695 $ rootDefinitionKey = $ resultSchema ->getRootDefinitionKey ();
8796
8897 $ this ->assertTrue (isset ($ definitions [$ rootDefinitionKey ]));
89- $ this ->assertTrue (isset ($ definitions [$ rootDefinitionKey ]['properties ' ]));
90- $ properties = $ resultSchema ['definitions ' ][$ rootDefinitionKey ]['properties ' ];
98+ $ this ->assertTrue (isset ($ definitions [$ rootDefinitionKey ]['allOf ' ][1 ]['properties ' ]));
99+ $ this ->assertEquals ($ definitions [$ rootDefinitionKey ]['allOf ' ][0 ], ['$ref ' => '#/definitions/HydraItemBaseSchema ' ]);
100+
101+ $ properties = $ definitions ['HydraItemBaseSchema ' ]['properties ' ];
91102 $ this ->assertArrayHasKey ('@context ' , $ properties );
92103 $ this ->assertEquals (
93104 [
94- 'readOnly ' => true ,
95105 'oneOf ' => [
96106 ['type ' => 'string ' ],
97107 [
@@ -119,55 +129,34 @@ public function testHasRootDefinitionKeyBuildSchema(): void
119129 public function testSchemaTypeBuildSchema (): void
120130 {
121131 $ resultSchema = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , new GetCollection ());
122- $ definitionName = 'Dummy.jsonld ' ;
123-
124132 $ this ->assertNull ($ resultSchema ->getRootDefinitionKey ());
125- $ this -> assertTrue ( isset ( $ resultSchema ['properties ' ])) ;
126- $ this -> assertTrue ( isset ( $ resultSchema ['properties ' ][ ' hydra:member ' ])) ;
127- $ this ->assertArrayHasKey ( 'hydra:totalItems ' , $ resultSchema [ ' properties ' ] );
128- $ this ->assertArrayHasKey ('hydra:view ' , $ resultSchema [ ' properties ' ] );
129- $ this ->assertArrayHasKey ('hydra:search ' , $ resultSchema [ ' properties ' ] );
130- $ properties = $ resultSchema [ ' definitions ' ][ $ definitionName ][ ' properties ' ] ;
133+ $ hydraCollectionSchema = $ resultSchema ['definitions ' ][ ' HydraCollectionBaseSchema ' ] ;
134+ $ properties = $ hydraCollectionSchema ['properties ' ];
135+ $ this ->assertTrue ( isset ( $ properties [ 'hydra:member ' ]) );
136+ $ this ->assertArrayHasKey ('hydra:totalItems ' , $ properties );
137+ $ this ->assertArrayHasKey ('hydra:view ' , $ properties );
138+ $ this -> assertArrayHasKey ( ' hydra:search ' , $ properties) ;
131139 $ this ->assertArrayNotHasKey ('@context ' , $ properties );
132- $ this ->assertArrayHasKey ('@type ' , $ properties );
133- $ this ->assertArrayHasKey ('@id ' , $ properties );
134140
135- $ resultSchema = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , null , null , null , true );
141+ $ this ->assertTrue (isset ($ properties ['hydra:view ' ]));
142+ $ this ->assertArrayHasKey ('properties ' , $ properties ['hydra:view ' ]);
143+ $ this ->assertArrayHasKey ('hydra:first ' , $ properties ['hydra:view ' ]['properties ' ]);
144+ $ this ->assertArrayHasKey ('hydra:last ' , $ properties ['hydra:view ' ]['properties ' ]);
145+ $ this ->assertArrayHasKey ('hydra:previous ' , $ properties ['hydra:view ' ]['properties ' ]);
146+ $ this ->assertArrayHasKey ('hydra:next ' , $ properties ['hydra:view ' ]['properties ' ]);
136147
137- $ this ->assertNull ($ resultSchema ->getRootDefinitionKey ());
138- $ this ->assertTrue (isset ($ resultSchema ['properties ' ]));
139- $ this ->assertTrue (isset ($ resultSchema ['properties ' ]['hydra:member ' ]));
140- $ this ->assertArrayHasKey ('hydra:totalItems ' , $ resultSchema ['properties ' ]);
141- $ this ->assertArrayHasKey ('hydra:view ' , $ resultSchema ['properties ' ]);
142- $ this ->assertArrayHasKey ('hydra:search ' , $ resultSchema ['properties ' ]);
143- $ properties = $ resultSchema ['definitions ' ][$ definitionName ]['properties ' ];
144- $ this ->assertArrayNotHasKey ('@context ' , $ properties );
145- $ this ->assertArrayHasKey ('@type ' , $ properties );
146- $ this ->assertArrayHasKey ('@id ' , $ properties );
147- }
148-
149- public function testHasHydraViewNavigationBuildSchema (): void
150- {
151- $ resultSchema = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , new GetCollection ());
152-
153- $ this ->assertNull ($ resultSchema ->getRootDefinitionKey ());
154- $ this ->assertTrue (isset ($ resultSchema ['properties ' ]));
155- $ this ->assertTrue (isset ($ resultSchema ['properties ' ]['hydra:view ' ]));
156- $ this ->assertArrayHasKey ('properties ' , $ resultSchema ['properties ' ]['hydra:view ' ]);
157- $ this ->assertArrayHasKey ('hydra:first ' , $ resultSchema ['properties ' ]['hydra:view ' ]['properties ' ]);
158- $ this ->assertArrayHasKey ('hydra:last ' , $ resultSchema ['properties ' ]['hydra:view ' ]['properties ' ]);
159- $ this ->assertArrayHasKey ('hydra:previous ' , $ resultSchema ['properties ' ]['hydra:view ' ]['properties ' ]);
160- $ this ->assertArrayHasKey ('hydra:next ' , $ resultSchema ['properties ' ]['hydra:view ' ]['properties ' ]);
148+ $ forcedCollection = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , null , null , null , true );
149+ $ this ->assertEquals ($ resultSchema ['allOf ' ][0 ]['$ref ' ], $ forcedCollection ['allOf ' ][0 ]['$ref ' ]);
161150 }
162151
163152 public function testSchemaTypeBuildSchemaWithoutPrefix (): void
164153 {
165154 $ resultSchema = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , new GetCollection (), null , [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false ]);
166155 $ this ->assertNull ($ resultSchema ->getRootDefinitionKey ());
167- $ this -> assertTrue ( isset ( $ resultSchema ['properties ' ])) ;
168- $ this -> assertTrue ( isset ( $ resultSchema ['properties ' ][ ' member ' ])) ;
169- $ this ->assertArrayHasKey ('totalItems ' , $ resultSchema [ ' properties ' ] );
170- $ this ->assertArrayHasKey ('view ' , $ resultSchema [ ' properties ' ] );
171- $ this ->assertArrayHasKey ('search ' , $ resultSchema [ ' properties ' ] );
156+ $ hydraCollectionSchema = $ resultSchema ['definitions ' ][ ' HydraCollectionBaseSchema ' ] ;
157+ $ properties = $ hydraCollectionSchema ['properties ' ];
158+ $ this ->assertArrayHasKey ('totalItems ' , $ properties );
159+ $ this ->assertArrayHasKey ('view ' , $ properties );
160+ $ this ->assertArrayHasKey ('search ' , $ properties );
172161 }
173162}
0 commit comments