@@ -162,6 +162,82 @@ public function testGlobalParameterHasHigherPriorityThanDefault()
162
162
$ this ->assertSame ('/app.php/de ' , $ url );
163
163
}
164
164
165
+ public function testGenerateWithDefaultLocale (): void
166
+ {
167
+ $ routes = new RouteCollection ();
168
+
169
+ $ route = new Route ('' );
170
+
171
+ $ name = 'test ' ;
172
+
173
+ foreach (['hr ' => '/foo ' , 'en ' => '/bar ' ] as $ locale => $ path ) {
174
+ $ localizedRoute = clone $ route ;
175
+ $ localizedRoute ->setDefault ('_locale ' , $ locale );
176
+ $ localizedRoute ->setDefault ('_canonical_route ' , $ name );
177
+ $ localizedRoute ->setPath ($ path );
178
+ $ routes ->add ($ name .'. ' .$ locale , $ localizedRoute );
179
+ }
180
+
181
+ $ generator = $ this ->getGenerator ($ routes , [], null , 'hr ' );
182
+
183
+ $ this ->assertSame (
184
+ 'http://localhost/app.php/foo ' ,
185
+ $ generator ->generate ($ name , [], UrlGeneratorInterface::ABSOLUTE_URL )
186
+ );
187
+ }
188
+
189
+ public function testGenerateWithOverriddenParameterLocale (): void
190
+ {
191
+ $ routes = new RouteCollection ();
192
+
193
+ $ route = new Route ('' );
194
+
195
+ $ name = 'test ' ;
196
+
197
+ foreach (['hr ' => '/foo ' , 'en ' => '/bar ' ] as $ locale => $ path ) {
198
+ $ localizedRoute = clone $ route ;
199
+ $ localizedRoute ->setDefault ('_locale ' , $ locale );
200
+ $ localizedRoute ->setDefault ('_canonical_route ' , $ name );
201
+ $ localizedRoute ->setPath ($ path );
202
+ $ routes ->add ($ name .'. ' .$ locale , $ localizedRoute );
203
+ }
204
+
205
+ $ generator = $ this ->getGenerator ($ routes , [], null , 'hr ' );
206
+
207
+ $ this ->assertSame (
208
+ 'http://localhost/app.php/bar ' ,
209
+ $ generator ->generate ($ name , ['_locale ' => 'en ' ], UrlGeneratorInterface::ABSOLUTE_URL )
210
+ );
211
+ }
212
+
213
+ public function testGenerateWithOverriddenParameterLocaleFromRequestContext (): void
214
+ {
215
+ $ routes = new RouteCollection ();
216
+
217
+ $ route = new Route ('' );
218
+
219
+ $ name = 'test ' ;
220
+
221
+ foreach (['hr ' => '/foo ' , 'en ' => '/bar ' ] as $ locale => $ path ) {
222
+ $ localizedRoute = clone $ route ;
223
+ $ localizedRoute ->setDefault ('_locale ' , $ locale );
224
+ $ localizedRoute ->setDefault ('_canonical_route ' , $ name );
225
+ $ localizedRoute ->setPath ($ path );
226
+ $ routes ->add ($ name .'. ' .$ locale , $ localizedRoute );
227
+ }
228
+
229
+ $ generator = $ this ->getGenerator ($ routes , [], null , 'hr ' );
230
+
231
+ $ context = new RequestContext ('/app.php ' );
232
+ $ context ->setParameter ('_locale ' , 'en ' );
233
+ $ generator ->setContext ($ context );
234
+
235
+ $ this ->assertSame (
236
+ 'http://localhost/app.php/bar ' ,
237
+ $ generator ->generate ($ name , [], UrlGeneratorInterface::ABSOLUTE_URL )
238
+ );
239
+ }
240
+
165
241
/**
166
242
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
167
243
*/
@@ -171,6 +247,29 @@ public function testGenerateWithoutRoutes()
171
247
$ this ->getGenerator ($ routes )->generate ('test ' , [], UrlGeneratorInterface::ABSOLUTE_URL );
172
248
}
173
249
250
+ /**
251
+ * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
252
+ */
253
+ public function testGenerateWithInvalidLocale (): void
254
+ {
255
+ $ routes = new RouteCollection ();
256
+
257
+ $ route = new Route ('' );
258
+
259
+ $ name = 'test ' ;
260
+
261
+ foreach (['hr ' => '/foo ' , 'en ' => '/bar ' ] as $ locale => $ path ) {
262
+ $ localizedRoute = clone $ route ;
263
+ $ localizedRoute ->setDefault ('_locale ' , $ locale );
264
+ $ localizedRoute ->setDefault ('_canonical_route ' , $ name );
265
+ $ localizedRoute ->setPath ($ path );
266
+ $ routes ->add ($ name .'. ' .$ locale , $ localizedRoute );
267
+ }
268
+
269
+ $ generator = $ this ->getGenerator ($ routes , [], null , 'fr ' );
270
+ $ generator ->generate ($ name );
271
+ }
272
+
174
273
/**
175
274
* @expectedException \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
176
275
*/
@@ -720,15 +819,15 @@ public function provideLookAroundRequirementsInPath()
720
819
yield ['/app.php/bar/a/b/bam/c/d/e ' , '/bar/{foo}/bam/{baz} ' , '(?<!^).+ ' ];
721
820
}
722
821
723
- protected function getGenerator (RouteCollection $ routes , array $ parameters = [], $ logger = null )
822
+ protected function getGenerator (RouteCollection $ routes , array $ parameters = [], $ logger = null , ? string $ defaultLocale = null )
724
823
{
725
824
$ context = new RequestContext ('/app.php ' );
726
825
foreach ($ parameters as $ key => $ value ) {
727
826
$ method = 'set ' .$ key ;
728
827
$ context ->$ method ($ value );
729
828
}
730
829
731
- return new UrlGenerator ($ routes , $ context , $ logger );
830
+ return new UrlGenerator ($ routes , $ context , $ logger, $ defaultLocale );
732
831
}
733
832
734
833
protected function getRoutes ($ name , Route $ route )
0 commit comments