12
12
namespace Symfony \Component \Routing \Tests ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Bridge \PhpUnit \ExpectUserDeprecationMessageTrait ;
15
16
use Symfony \Component \Config \Resource \FileResource ;
16
17
use Symfony \Component \Routing \Route ;
17
18
use Symfony \Component \Routing \RouteCollection ;
18
19
19
20
class RouteCollectionTest extends TestCase
20
21
{
22
+ use ExpectUserDeprecationMessageTrait;
23
+
21
24
public function testRoute ()
22
25
{
23
26
$ collection = new RouteCollection ();
@@ -115,27 +118,65 @@ public function testAddCollectionWithResources()
115
118
public function testAddDefaultsAndRequirementsAndOptions ()
116
119
{
117
120
$ collection = new RouteCollection ();
118
- $ collection ->add ('foo ' , new Route ('/{placeholder} ' ));
119
121
$ collection1 = new RouteCollection ();
120
- $ collection1 ->add ('bar ' , new Route ('/{placeholder} ' ,
121
- ['_controller ' => 'fixed ' , 'placeholder ' => 'default ' ], ['placeholder ' => '.+ ' ], ['option ' => 'value ' ])
122
- );
122
+ $ collection1 ->add ('foo ' , new Route ('/{placeholder} ' ));
123
123
$ collection ->addCollection ($ collection1 );
124
124
125
125
$ collection ->addDefaults (['placeholder ' => 'new-default ' ]);
126
126
$ this ->assertEquals (['placeholder ' => 'new-default ' ], $ collection ->get ('foo ' )->getDefaults (), '->addDefaults() adds defaults to all routes ' );
127
- $ this ->assertEquals (['_controller ' => 'fixed ' , 'placeholder ' => 'new-default ' ], $ collection ->get ('bar ' )->getDefaults (),
128
- '->addDefaults() adds defaults to all routes and overwrites existing ones ' );
129
127
130
128
$ collection ->addRequirements (['placeholder ' => '\d+ ' ]);
131
129
$ this ->assertEquals (['placeholder ' => '\d+ ' ], $ collection ->get ('foo ' )->getRequirements (), '->addRequirements() adds requirements to all routes ' );
132
- $ this ->assertEquals (['placeholder ' => '\d+ ' ], $ collection ->get ('bar ' )->getRequirements (),
133
- '->addRequirements() adds requirements to all routes and overwrites existing ones ' );
134
130
135
131
$ collection ->addOptions (['option ' => 'new-value ' ]);
136
132
$ this ->assertEquals (
137
133
['option ' => 'new-value ' , 'compiler_class ' => 'Symfony \\Component \\Routing \\RouteCompiler ' ],
138
- $ collection ->get ('bar ' )->getOptions (), '->addOptions() adds options to all routes and overwrites existing ones '
134
+ $ collection ->get ('foo ' )->getOptions (), '->addOptions() adds options to all routes '
135
+ );
136
+ }
137
+
138
+ /**
139
+ * @group legacy
140
+ */
141
+ public function testCollectionOverridingDefaultsIsDeprecated ()
142
+ {
143
+ $ collection = new RouteCollection ();
144
+ $ collection ->add ('foo ' , new Route ('/{placeholder} ' , ['placeholder ' => 'default ' ]));
145
+
146
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route default with its group \' is deprecated, you should remove it from the route. ' );
147
+ $ collection ->addDefaults (['placeholder ' => 'new-default ' ]);
148
+
149
+ $ this ->assertEquals (['placeholder ' => 'new-default ' ], $ collection ->get ('foo ' )->getDefaults ());
150
+ }
151
+
152
+ /**
153
+ * @group legacy
154
+ */
155
+ public function testCollectionOverridingRequirementsIsDeprecated ()
156
+ {
157
+ $ collection = new RouteCollection ();
158
+ $ collection ->add ('foo ' , new Route ('/{placeholder} ' , requirements: ['placeholder ' => '.+ ' ]));
159
+
160
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route requirement with its group \' is deprecated, you should remove it from the route. ' );
161
+ $ collection ->addRequirements (['placeholder ' => '\d+ ' ]);
162
+
163
+ $ this ->assertEquals (['placeholder ' => '\d+ ' ], $ collection ->get ('foo ' )->getRequirements ());
164
+ }
165
+
166
+ /**
167
+ * @group legacy
168
+ */
169
+ public function testCollectionOverridingOptionsIsDeprecated ()
170
+ {
171
+ $ collection = new RouteCollection ();
172
+ $ collection ->add ('foo ' , new Route ('/{placeholder} ' , options: ['option ' => 'value ' ]));
173
+
174
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route option with its group \' is deprecated, you should remove it from the route. ' );
175
+ $ collection ->addOptions (['option ' => 'new-value ' ]);
176
+
177
+ $ this ->assertEquals (
178
+ ['option ' => 'new-value ' , 'compiler_class ' => 'Symfony \\Component \\Routing \\RouteCompiler ' ],
179
+ $ collection ->get ('foo ' )->getOptions ()
139
180
);
140
181
}
141
182
@@ -240,29 +281,52 @@ public function testRemove()
240
281
public function testSetHost ()
241
282
{
242
283
$ collection = new RouteCollection ();
243
- $ routea = new Route ('/a ' );
244
- $ routeb = new Route ('/b ' , [], [], [], '{locale}.example.net ' );
245
- $ collection ->add ('a ' , $ routea );
246
- $ collection ->add ('b ' , $ routeb );
284
+ $ route = new Route ('/a ' );
285
+ $ collection ->add ('a ' , $ route );
247
286
248
287
$ collection ->setHost ('{locale}.example.com ' );
249
288
250
- $ this ->assertEquals ('{locale}.example.com ' , $ routea ->getHost ());
251
- $ this ->assertEquals ('{locale}.example.com ' , $ routeb ->getHost ());
289
+ $ this ->assertEquals ('{locale}.example.com ' , $ route ->getHost ());
290
+ }
291
+
292
+ /**
293
+ * @group legacy
294
+ */
295
+ public function testCollectionOverridingHostIsDeprecated ()
296
+ {
297
+ $ collection = new RouteCollection ();
298
+ $ route = new Route ('/a ' , [], [], [], '{locale}.example.net ' );
299
+ $ collection ->add ('a ' , $ route );
300
+
301
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route host with its group \' is deprecated, you should remove it from the route. ' );
302
+ $ collection ->setHost ('{locale}.example.com ' );
303
+
304
+ $ this ->assertEquals ('{locale}.example.com ' , $ route ->getHost ());
252
305
}
253
306
254
307
public function testSetCondition ()
255
308
{
256
309
$ collection = new RouteCollection ();
257
310
$ routea = new Route ('/a ' );
258
- $ routeb = new Route ('/b ' , [], [], [], '{locale}.example.net ' , [], [], 'context.getMethod() == "GET" ' );
259
311
$ collection ->add ('a ' , $ routea );
260
- $ collection ->add ('b ' , $ routeb );
261
312
262
313
$ collection ->setCondition ('context.getMethod() == "POST" ' );
263
314
264
315
$ this ->assertEquals ('context.getMethod() == "POST" ' , $ routea ->getCondition ());
265
- $ this ->assertEquals ('context.getMethod() == "POST" ' , $ routeb ->getCondition ());
316
+ }
317
+
318
+ /**
319
+ * @group legacy
320
+ */
321
+ public function testCollectionOverridingConditionsIsDeprecated ()
322
+ {
323
+ $ collection = new RouteCollection ();
324
+ $ collection ->add ('foo ' , new Route ('/{placeholder} ' , condition: 'condition ' ));
325
+
326
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route condition with its group \' is deprecated, you should remove it from the route. ' );
327
+ $ collection ->setCondition (null );
328
+
329
+ $ this ->assertEquals ('' , $ collection ->get ('foo ' )->getCondition ());
266
330
}
267
331
268
332
public function testClone ()
@@ -283,29 +347,53 @@ public function testClone()
283
347
public function testSetSchemes ()
284
348
{
285
349
$ collection = new RouteCollection ();
286
- $ routea = new Route ('/a ' , [], [], [], '' , 'http ' );
287
- $ routeb = new Route ('/b ' );
288
- $ collection ->add ('a ' , $ routea );
289
- $ collection ->add ('b ' , $ routeb );
350
+ $ route = new Route ('/a ' );
351
+ $ collection ->add ('a ' , $ route );
352
+
353
+ $ collection ->setSchemes (['http ' , 'https ' ]);
354
+
355
+ $ this ->assertEquals (['http ' , 'https ' ], $ route ->getSchemes ());
356
+ }
290
357
358
+ /**
359
+ * @group legacy
360
+ */
361
+ public function testCollectionOverridingSchemesIsDeprecated ()
362
+ {
363
+ $ collection = new RouteCollection ();
364
+ $ route = new Route ('/a ' , [], [], [], '' , 'http ' );
365
+ $ collection ->add ('a ' , $ route );
366
+
367
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route schemes with its group \' is deprecated, you should remove it from the route. ' );
291
368
$ collection ->setSchemes (['http ' , 'https ' ]);
292
369
293
- $ this ->assertEquals (['http ' , 'https ' ], $ routea ->getSchemes ());
294
- $ this ->assertEquals (['http ' , 'https ' ], $ routeb ->getSchemes ());
370
+ $ this ->assertEquals (['http ' , 'https ' ], $ route ->getSchemes ());
295
371
}
296
372
297
373
public function testSetMethods ()
298
374
{
299
375
$ collection = new RouteCollection ();
300
- $ routea = new Route ('/a ' , [], [], [], '' , [], ['GET ' , 'POST ' ]);
301
- $ routeb = new Route ('/b ' );
302
- $ collection ->add ('a ' , $ routea );
303
- $ collection ->add ('b ' , $ routeb );
376
+ $ route = new Route ('/a ' );
377
+ $ collection ->add ('a ' , $ route );
378
+
379
+ $ collection ->setMethods ('PUT ' );
380
+
381
+ $ this ->assertEquals (['PUT ' ], $ route ->getMethods ());
382
+ }
383
+
384
+ /**
385
+ * @group legacy
386
+ */
387
+ public function testCollectionOverridingMethodsIsDeprecated ()
388
+ {
389
+ $ collection = new RouteCollection ();
390
+ $ route = new Route ('/a ' , [], [], [], '' , [], ['GET ' , 'POST ' ]);
391
+ $ collection ->add ('a ' , $ route );
304
392
393
+ $ this ->expectUserDeprecationMessage ('Since symfony/routing 7.3: Overriding a route methods with its group \' is deprecated, you should remove it from the route. ' );
305
394
$ collection ->setMethods ('PUT ' );
306
395
307
- $ this ->assertEquals (['PUT ' ], $ routea ->getMethods ());
308
- $ this ->assertEquals (['PUT ' ], $ routeb ->getMethods ());
396
+ $ this ->assertEquals (['PUT ' ], $ route ->getMethods ());
309
397
}
310
398
311
399
public function testAddNamePrefix ()
0 commit comments