@@ -1054,6 +1054,80 @@ public function testUtf8VarName()
1054
1054
$ this ->assertSame ('/app.php/foo/baz ' , $ this ->getGenerator ($ routes )->generate ('test ' , ['bär ' => 'baz ' ]));
1055
1055
}
1056
1056
1057
+ public function testQueryParameters ()
1058
+ {
1059
+ $ routes = $ this ->getRoutes ('user ' , new Route ('/user/{username} ' ));
1060
+ $ url = $ this ->getGenerator ($ routes )->generate ('user ' , [
1061
+ 'username ' => 'john ' ,
1062
+ 'a ' => 'foo ' ,
1063
+ 'b ' => 'bar ' ,
1064
+ 'c ' => 'baz ' ,
1065
+ '_query ' => [
1066
+ 'a ' => '123 ' ,
1067
+ 'd ' => '789 ' ,
1068
+ ],
1069
+ ]);
1070
+ $ this ->assertSame ('/app.php/user/john?a=123&b=bar&c=baz&d=789 ' , $ url );
1071
+ }
1072
+
1073
+ public function testRouteHostParameterAndQueryParameterWithSameName ()
1074
+ {
1075
+ $ routes = $ this ->getRoutes ('admin_stats ' , new Route ('/admin/stats ' , requirements: ['domain ' => '.+ ' ], host: '{siteCode}.{domain} ' ));
1076
+ $ url = $ this ->getGenerator ($ routes )->generate ('admin_stats ' , [
1077
+ 'siteCode ' => 'fr ' ,
1078
+ 'domain ' => 'example.com ' ,
1079
+ '_query ' => [
1080
+ 'siteCode ' => 'us ' ,
1081
+ ],
1082
+ ], UrlGeneratorInterface::NETWORK_PATH );
1083
+ $ this ->assertSame ('//fr.example.com/app.php/admin/stats?siteCode=us ' , $ url );
1084
+ }
1085
+
1086
+ public function testRoutePathParameterAndQueryParameterWithSameName ()
1087
+ {
1088
+ $ routes = $ this ->getRoutes ('user ' , new Route ('/user/{id} ' ));
1089
+ $ url = $ this ->getGenerator ($ routes )->generate ('user ' , [
1090
+ 'id ' => '123 ' ,
1091
+ '_query ' => [
1092
+ 'id ' => '456 ' ,
1093
+ ],
1094
+ ]);
1095
+ $ this ->assertSame ('/app.php/user/123?id=456 ' , $ url );
1096
+ }
1097
+
1098
+ public function testQueryParameterCannotSubstituteRouteParameter ()
1099
+ {
1100
+ $ routes = $ this ->getRoutes ('user ' , new Route ('/user/{id} ' ));
1101
+
1102
+ $ this ->expectException (MissingMandatoryParametersException::class);
1103
+ $ this ->expectExceptionMessage ('Some mandatory parameters are missing ("id") to generate a URL for route "user". ' );
1104
+
1105
+ $ this ->getGenerator ($ routes )->generate ('user ' , [
1106
+ '_query ' => [
1107
+ 'id ' => '456 ' ,
1108
+ ],
1109
+ ]);
1110
+ }
1111
+
1112
+ /**
1113
+ * @group legacy
1114
+ */
1115
+ public function testQueryParametersWithScalarValue ()
1116
+ {
1117
+ $ routes = $ this ->getRoutes ('user ' , new Route ('/user/{id} ' ));
1118
+
1119
+ $ this ->expectDeprecation (
1120
+ 'Since symfony/routing 7.4: Parameter "_query" is reserved for passing an array of query parameters. ' .
1121
+ 'Passing a scalar value is deprecated and will throw an exception in Symfony 8.0. ' ,
1122
+ );
1123
+
1124
+ $ url = $ this ->getGenerator ($ routes )->generate ('user ' , [
1125
+ 'id ' => '123 ' ,
1126
+ '_query ' => 'foo ' ,
1127
+ ]);
1128
+ $ this ->assertSame ('/app.php/user/123?_query=foo ' , $ url );
1129
+ }
1130
+
1057
1131
protected function getGenerator (RouteCollection $ routes , array $ parameters = [], $ logger = null , ?string $ defaultLocale = null )
1058
1132
{
1059
1133
$ context = new RequestContext ('/app.php ' );
0 commit comments