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