@@ -757,7 +757,46 @@ public function testKeepCachePoolClearerCacheWarmerWithDebug()
757
757
$ this ->extension ->load (self ::DEFAULT_CONFIG , $ containerBuilder );
758
758
}
759
759
760
- private function getPartialContainerBuilderProphecy ()
760
+ public function testDisabledSwaggerAndHydra ()
761
+ {
762
+ $ config = self ::DEFAULT_CONFIG ;
763
+ $ config ['api_platform ' ]['enable_swagger ' ] = false ;
764
+ $ config ['api_platform ' ]['enable_swagger_ui ' ] = false ;
765
+ $ config ['api_platform ' ]['formats ' ] = [
766
+ 'jsonhal ' => ['mime_types ' => ['application/hal+json ' ]],
767
+ ];
768
+
769
+ $ containerBuilderProphecy = $ this ->getBaseContainerBuilderProphecy (['orm ' ], $ config );
770
+ $ containerBuilderProphecy ->setDefinition ('api_platform.swagger.action.ui ' , Argument::type (Definition::class))->shouldNotBeCalled ();
771
+ $ containerBuilderProphecy ->setDefinition ('api_platform.swagger.listener.ui ' , Argument::type (Definition::class))->shouldNotBeCalled ();
772
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_swagger_ui ' , true )->shouldNotBeCalled ();
773
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_swagger_ui ' , true )->shouldNotBeCalled ();
774
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_swagger_ui ' , false )->shouldNotBeCalled ();
775
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_re_doc ' , true )->shouldNotBeCalled ();
776
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_re_doc ' , false )->shouldNotBeCalled ();
777
+ $ containerBuilder = $ containerBuilderProphecy ->reveal ();
778
+ $ this ->extension ->load ($ config , $ containerBuilder );
779
+ }
780
+
781
+ public function testDisabledSwagger ()
782
+ {
783
+ $ config = self ::DEFAULT_CONFIG ;
784
+ $ config ['api_platform ' ]['enable_swagger ' ] = false ;
785
+ $ config ['api_platform ' ]['enable_swagger_ui ' ] = false ;
786
+
787
+ $ containerBuilderProphecy = $ this ->getBaseContainerBuilderProphecy (['orm ' ], $ config );
788
+ $ containerBuilderProphecy ->setDefinition ('api_platform.swagger.action.ui ' , Argument::type (Definition::class))->shouldNotBeCalled ();
789
+ $ containerBuilderProphecy ->setDefinition ('api_platform.swagger.listener.ui ' , Argument::type (Definition::class))->shouldNotBeCalled ();
790
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_swagger_ui ' , true )->shouldNotBeCalled ();
791
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_swagger_ui ' , true )->shouldNotBeCalled ();
792
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_swagger_ui ' , false )->shouldNotBeCalled ();
793
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_re_doc ' , true )->shouldNotBeCalled ();
794
+ $ containerBuilderProphecy ->setParameter ('api_platform.enable_re_doc ' , false )->shouldNotBeCalled ();
795
+ $ containerBuilder = $ containerBuilderProphecy ->reveal ();
796
+ $ this ->extension ->load ($ config , $ containerBuilder );
797
+ }
798
+
799
+ private function getPartialContainerBuilderProphecy ($ configuration = null )
761
800
{
762
801
$ parameterBag = new EnvPlaceholderParameterBag ();
763
802
@@ -783,7 +822,7 @@ private function getPartialContainerBuilderProphecy()
783
822
'api_platform.collection.order_parameter_name ' => 'order ' ,
784
823
'api_platform.description ' => 'description ' ,
785
824
'api_platform.error_formats ' => ['jsonproblem ' => ['application/problem+json ' ], 'jsonld ' => ['application/ld+json ' ]],
786
- 'api_platform.formats ' => ['jsonld ' => ['application/ld+json ' ], 'jsonhal ' => ['application/hal+json ' ]],
825
+ 'api_platform.formats ' => null === $ configuration ? ['jsonld ' => ['application/ld+json ' ], 'jsonhal ' => ['application/hal+json ' ]] : $ this -> getFormatsFromConfiguration ( $ configuration [ ' api_platform ' ][ ' formats ' ]) ?? [ ],
787
826
'api_platform.patch_formats ' => [],
788
827
'api_platform.exception_to_status ' => [
789
828
ExceptionInterface::class => Response::HTTP_BAD_REQUEST ,
@@ -977,9 +1016,12 @@ private function getPartialContainerBuilderProphecy()
977
1016
return $ containerBuilderProphecy ;
978
1017
}
979
1018
980
- private function getBaseContainerBuilderProphecy (array $ doctrineIntegrationsToLoad = ['orm ' ])
1019
+ private function getBaseContainerBuilderProphecy (array $ doctrineIntegrationsToLoad = ['orm ' ], $ configuration = null )
981
1020
{
982
- $ containerBuilderProphecy = $ this ->getPartialContainerBuilderProphecy ();
1021
+ $ hasSwagger = null === $ configuration || true === $ configuration ['api_platform ' ]['enable_swagger ' ] ?? false ;
1022
+ $ hasHydra = null === $ configuration || isset ($ configuration ['api_platform ' ]['formats ' ]['jsonld ' ]);
1023
+
1024
+ $ containerBuilderProphecy = $ this ->getPartialContainerBuilderProphecy ($ configuration );
983
1025
984
1026
$ containerBuilderProphecy ->hasParameter ('kernel.debug ' )->willReturn (true );
985
1027
$ containerBuilderProphecy ->getParameter ('kernel.debug ' )->willReturn (false );
@@ -1063,8 +1105,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1063
1105
'api_platform.oauth.tokenUrl ' => '/oauth/v2/token ' ,
1064
1106
'api_platform.oauth.authorizationUrl ' => '/oauth/v2/auth ' ,
1065
1107
'api_platform.oauth.scopes ' => [],
1066
- 'api_platform.swagger.versions ' => [2 , 3 ],
1067
- 'api_platform.swagger.api_keys ' => [],
1068
1108
'api_platform.enable_swagger_ui ' => true ,
1069
1109
'api_platform.enable_re_doc ' => true ,
1070
1110
'api_platform.graphql.enabled ' => true ,
@@ -1078,6 +1118,13 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1078
1118
'api_platform.elasticsearch.enabled ' => false ,
1079
1119
];
1080
1120
1121
+ if ($ hasSwagger ) {
1122
+ $ parameters ['api_platform.swagger.versions ' ] = [2 , 3 ];
1123
+ $ parameters ['api_platform.swagger.api_keys ' ] = [];
1124
+ } else {
1125
+ $ parameters ['api_platform.swagger.versions ' ] = [];
1126
+ }
1127
+
1081
1128
foreach ($ parameters as $ key => $ value ) {
1082
1129
$ containerBuilderProphecy ->setParameter ($ key , $ value )->shouldBeCalled ();
1083
1130
}
@@ -1153,20 +1200,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1153
1200
'api_platform.http_cache.listener.response.configure ' ,
1154
1201
'api_platform.http_cache.purger.varnish_client ' ,
1155
1202
'api_platform.http_cache.purger.varnish ' ,
1156
- 'api_platform.hydra.listener.response.add_link_header ' ,
1157
- 'api_platform.hydra.normalizer.collection ' ,
1158
- 'api_platform.hydra.normalizer.collection_filters ' ,
1159
- 'api_platform.hydra.normalizer.constraint_violation_list ' ,
1160
- 'api_platform.hydra.normalizer.documentation ' ,
1161
- 'api_platform.hydra.normalizer.entrypoint ' ,
1162
- 'api_platform.hydra.normalizer.error ' ,
1163
- 'api_platform.hydra.normalizer.partial_collection_view ' ,
1164
- 'api_platform.hydra.json_schema.schema_factory ' ,
1165
- 'api_platform.jsonld.action.context ' ,
1166
- 'api_platform.jsonld.context_builder ' ,
1167
- 'api_platform.jsonld.encoder ' ,
1168
- 'api_platform.jsonld.normalizer.item ' ,
1169
- 'api_platform.jsonld.normalizer.object ' ,
1170
1203
'api_platform.listener.view.validate ' ,
1171
1204
'api_platform.listener.view.validate_query_parameters ' ,
1172
1205
'api_platform.mercure.listener.response.add_link_header ' ,
@@ -1190,13 +1223,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1190
1223
'api_platform.problem.normalizer.constraint_violation_list ' ,
1191
1224
'api_platform.problem.normalizer.error ' ,
1192
1225
'api_platform.swagger.action.ui ' ,
1193
- 'api_platform.swagger.command.swagger_command ' ,
1194
1226
'api_platform.swagger.listener.ui ' ,
1195
- 'api_platform.swagger.normalizer.api_gateway ' ,
1196
- 'api_platform.swagger.normalizer.documentation ' ,
1197
- 'api_platform.json_schema.type_factory ' ,
1198
- 'api_platform.json_schema.schema_factory ' ,
1199
- 'api_platform.json_schema.json_schema_generate_command ' ,
1200
1227
'api_platform.validator ' ,
1201
1228
'test.api_platform.client ' ,
1202
1229
];
@@ -1229,6 +1256,41 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1229
1256
$ definitions [] = 'api_platform.doctrine.metadata_factory ' ;
1230
1257
}
1231
1258
1259
+ // Only when swagger is enabled
1260
+ if ($ hasSwagger ) {
1261
+ $ definitions [] = 'api_platform.swagger.command.swagger_command ' ;
1262
+ $ definitions [] = 'api_platform.swagger.normalizer.api_gateway ' ;
1263
+ $ definitions [] = 'api_platform.swagger.normalizer.documentation ' ;
1264
+ $ definitions [] = 'api_platform.json_schema.type_factory ' ;
1265
+ $ definitions [] = 'api_platform.json_schema.schema_factory ' ;
1266
+ $ definitions [] = 'api_platform.json_schema.json_schema_generate_command ' ;
1267
+ }
1268
+
1269
+ // has jsonld
1270
+ if ($ hasHydra ) {
1271
+ $ containerBuilderProphecy ->has ('api_platform.json_schema.schema_factory ' )->shouldBeCalled ()->willReturn ($ hasSwagger );
1272
+
1273
+ if (!$ hasSwagger ) {
1274
+ $ containerBuilderProphecy ->has ('api_platform.json_schema.schema_factory ' )->shouldBeCalled ()->willReturn (false );
1275
+ $ containerBuilderProphecy ->removeDefinition ('api_platform.hydra.json_schema.schema_factory ' )->shouldBeCalled ();
1276
+ }
1277
+
1278
+ $ definitions [] = 'api_platform.hydra.json_schema.schema_factory ' ;
1279
+ $ definitions [] = 'api_platform.hydra.listener.response.add_link_header ' ;
1280
+ $ definitions [] = 'api_platform.hydra.normalizer.collection ' ;
1281
+ $ definitions [] = 'api_platform.hydra.normalizer.collection_filters ' ;
1282
+ $ definitions [] = 'api_platform.hydra.normalizer.constraint_violation_list ' ;
1283
+ $ definitions [] = 'api_platform.hydra.normalizer.documentation ' ;
1284
+ $ definitions [] = 'api_platform.hydra.normalizer.entrypoint ' ;
1285
+ $ definitions [] = 'api_platform.hydra.normalizer.error ' ;
1286
+ $ definitions [] = 'api_platform.hydra.normalizer.partial_collection_view ' ;
1287
+ $ definitions [] = 'api_platform.jsonld.action.context ' ;
1288
+ $ definitions [] = 'api_platform.jsonld.context_builder ' ;
1289
+ $ definitions [] = 'api_platform.jsonld.encoder ' ;
1290
+ $ definitions [] = 'api_platform.jsonld.normalizer.item ' ;
1291
+ $ definitions [] = 'api_platform.jsonld.normalizer.object ' ;
1292
+ }
1293
+
1232
1294
foreach ($ definitions as $ definition ) {
1233
1295
$ containerBuilderProphecy ->setDefinition ($ definition , Argument::type (Definition::class))->shouldBeCalled ();
1234
1296
}
@@ -1250,8 +1312,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1250
1312
NumericFilter::class => 'api_platform.doctrine.orm.numeric_filter ' ,
1251
1313
ExistsFilter::class => 'api_platform.doctrine.orm.exists_filter ' ,
1252
1314
GraphQlSerializerContextBuilderInterface::class => 'api_platform.graphql.serializer.context_builder ' ,
1253
- TypeFactoryInterface::class => 'api_platform.json_schema.type_factory ' ,
1254
- SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory ' ,
1255
1315
];
1256
1316
1257
1317
if (\in_array ('odm ' , $ doctrineIntegrationsToLoad , true )) {
@@ -1269,6 +1329,14 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1269
1329
];
1270
1330
}
1271
1331
1332
+ // Only when swagger is enabled
1333
+ if ($ hasSwagger ) {
1334
+ $ aliases += [
1335
+ TypeFactoryInterface::class => 'api_platform.json_schema.type_factory ' ,
1336
+ SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory ' ,
1337
+ ];
1338
+ }
1339
+
1272
1340
foreach ($ aliases as $ alias => $ service ) {
1273
1341
$ containerBuilderProphecy ->setAlias ($ alias , $ service )->shouldBeCalled ();
1274
1342
}
@@ -1283,4 +1351,21 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
1283
1351
1284
1352
return $ containerBuilderProphecy ;
1285
1353
}
1354
+
1355
+ private function getFormatsFromConfiguration (array $ formatsConfiguration )
1356
+ {
1357
+ $ formats = [];
1358
+
1359
+ foreach ($ formatsConfiguration as $ format => $ mimeTypes ) {
1360
+ if (!isset ($ formats [$ format ])) {
1361
+ $ formats [$ format ] = [];
1362
+ }
1363
+
1364
+ foreach ($ mimeTypes ['mime_types ' ] as $ mimeType ) {
1365
+ $ formats [$ format ][] = $ mimeType ;
1366
+ }
1367
+ }
1368
+
1369
+ return $ formats ;
1370
+ }
1286
1371
}
0 commit comments