19
19
use ApiPlatform \JsonSchema \DefinitionNameFactory ;
20
20
use ApiPlatform \JsonSchema \Schema ;
21
21
use ApiPlatform \JsonSchema \SchemaFactory as BaseSchemaFactory ;
22
+ use ApiPlatform \Metadata \ApiProperty ;
22
23
use ApiPlatform \Metadata \ApiResource ;
23
24
use ApiPlatform \Metadata \Get ;
24
25
use ApiPlatform \Metadata \GetCollection ;
28
29
use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
29
30
use ApiPlatform \Metadata \Resource \ResourceMetadataCollection ;
30
31
use PHPUnit \Framework \TestCase ;
32
+ use Prophecy \Argument ;
31
33
use Prophecy \PhpUnit \ProphecyTrait ;
32
34
33
35
class SchemaFactoryTest extends TestCase
@@ -48,10 +50,12 @@ protected function setUp(): void
48
50
);
49
51
50
52
$ 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 ' ] ));
52
54
$ 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 ());
53
57
54
- $ definitionNameFactory = new DefinitionNameFactory ([ ' jsonapi ' => true , ' jsonhal ' => true , ' jsonld ' => true ] );
58
+ $ definitionNameFactory = new DefinitionNameFactory ();
55
59
56
60
$ baseSchemaFactory = new BaseSchemaFactory (
57
61
resourceMetadataFactory: $ resourceMetadataFactoryCollection ->reveal (),
@@ -60,7 +64,12 @@ protected function setUp(): void
60
64
definitionNameFactory: $ definitionNameFactory ,
61
65
);
62
66
63
- $ this ->schemaFactory = new SchemaFactory ($ baseSchemaFactory );
67
+ $ this ->schemaFactory = new SchemaFactory (
68
+ $ baseSchemaFactory ,
69
+ [],
70
+ $ definitionNameFactory ,
71
+ $ resourceMetadataFactoryCollection ->reveal (),
72
+ );
64
73
}
65
74
66
75
public function testBuildSchema (): void
@@ -86,12 +95,13 @@ public function testHasRootDefinitionKeyBuildSchema(): void
86
95
$ rootDefinitionKey = $ resultSchema ->getRootDefinitionKey ();
87
96
88
97
$ 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 ' ];
91
102
$ this ->assertArrayHasKey ('@context ' , $ properties );
92
103
$ this ->assertEquals (
93
104
[
94
- 'readOnly ' => true ,
95
105
'oneOf ' => [
96
106
['type ' => 'string ' ],
97
107
[
@@ -119,55 +129,34 @@ public function testHasRootDefinitionKeyBuildSchema(): void
119
129
public function testSchemaTypeBuildSchema (): void
120
130
{
121
131
$ resultSchema = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , new GetCollection ());
122
- $ definitionName = 'Dummy.jsonld ' ;
123
-
124
132
$ 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) ;
131
139
$ this ->assertArrayNotHasKey ('@context ' , $ properties );
132
- $ this ->assertArrayHasKey ('@type ' , $ properties );
133
- $ this ->assertArrayHasKey ('@id ' , $ properties );
134
140
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 ' ]);
136
147
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 ' ]);
161
150
}
162
151
163
152
public function testSchemaTypeBuildSchemaWithoutPrefix (): void
164
153
{
165
154
$ resultSchema = $ this ->schemaFactory ->buildSchema (Dummy::class, 'jsonld ' , Schema::TYPE_OUTPUT , new GetCollection (), null , [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false ]);
166
155
$ 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 );
172
161
}
173
162
}
0 commit comments