3
3
namespace Symfony \Cmf \Component \Routing \Tests \Routing ;
4
4
5
5
use Symfony \Component \Routing \RouteCollection ;
6
+ use Symfony \Component \HttpFoundation \Request ;
6
7
7
8
use Symfony \Cmf \Component \Routing \ChainRouter ;
8
9
use Symfony \Cmf \Component \Routing \Test \CmfUnitTestCase ;
@@ -11,7 +12,7 @@ class ChainRouterTest extends CmfUnitTestCase
11
12
{
12
13
public function setUp ()
13
14
{
14
- $ this ->router = new ChainRouter ();
15
+ $ this ->router = new ChainRouter ($ this -> getMock ( ' Symfony\Component\HttpKernel\Log\LoggerInterface ' ) );
15
16
$ this ->context = $ this ->getMock ('Symfony \\Component \\Routing \\RequestContext ' );
16
17
}
17
18
@@ -163,6 +164,49 @@ public function testMatch()
163
164
$ this ->assertEquals (array ('test ' ), $ result );
164
165
}
165
166
167
+ /**
168
+ * The first usable match is used, no further routers are queried once a match is found
169
+ */
170
+ public function testMatchRequest ()
171
+ {
172
+ $ url = '/test ' ;
173
+ list ($ lower , $ low , $ high ) = $ this ->createRouterMocks ();
174
+
175
+ $ highest = $ this ->getMock ('Symfony \\Cmf \\Component \\Routing \\Tests \\Routing \\RequestMatcher ' );
176
+
177
+ $ request = Request::create ('/test ' );
178
+
179
+ $ highest
180
+ ->expects ($ this ->once ())
181
+ ->method ('matchRequest ' )
182
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \ResourceNotFoundException ))
183
+ ;
184
+ $ high
185
+ ->expects ($ this ->once ())
186
+ ->method ('match ' )
187
+ ->with ($ url )
188
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \ResourceNotFoundException ))
189
+ ;
190
+ $ low
191
+ ->expects ($ this ->once ())
192
+ ->method ('match ' )
193
+ ->with ($ url )
194
+ ->will ($ this ->returnValue (array ('test ' )))
195
+ ;
196
+ $ lower
197
+ ->expects ($ this ->never ())
198
+ ->method ('match ' )
199
+ ;
200
+
201
+ $ this ->router ->add ($ lower , 5 );
202
+ $ this ->router ->add ($ low , 10 );
203
+ $ this ->router ->add ($ high , 100 );
204
+ $ this ->router ->add ($ highest , 200 );
205
+
206
+ $ result = $ this ->router ->matchRequest ($ request );
207
+ $ this ->assertEquals (array ('test ' ), $ result );
208
+ }
209
+
166
210
/**
167
211
* If there is a method not allowed but another router matches, that one is used
168
212
*/
@@ -190,6 +234,33 @@ public function testMatchAndNotAllowed()
190
234
$ this ->assertEquals (array ('test ' ), $ result );
191
235
}
192
236
237
+ /**
238
+ * If there is a method not allowed but another router matches, that one is used
239
+ */
240
+ public function testMatchRequestAndNotAllowed ()
241
+ {
242
+ $ url = '/test ' ;
243
+ list ($ low , $ high ) = $ this ->createRouterMocks ();
244
+
245
+ $ high
246
+ ->expects ($ this ->once ())
247
+ ->method ('match ' )
248
+ ->with ($ url )
249
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \MethodNotAllowedException (array ())))
250
+ ;
251
+ $ low
252
+ ->expects ($ this ->once ())
253
+ ->method ('match ' )
254
+ ->with ($ url )
255
+ ->will ($ this ->returnValue (array ('test ' )))
256
+ ;
257
+ $ this ->router ->add ($ low , 10 );
258
+ $ this ->router ->add ($ high , 100 );
259
+
260
+ $ result = $ this ->router ->matchRequest (Request::create ('/test ' ));
261
+ $ this ->assertEquals (array ('test ' ), $ result );
262
+ }
263
+
193
264
/**
194
265
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
195
266
*/
@@ -216,6 +287,32 @@ public function testMatchNotFound()
216
287
$ this ->router ->match ('/test ' );
217
288
}
218
289
290
+ /**
291
+ * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
292
+ */
293
+ public function testMatchRequestNotFound ()
294
+ {
295
+ $ url = '/test ' ;
296
+ list ($ low , $ high ) = $ this ->createRouterMocks ();
297
+
298
+ $ high
299
+ ->expects ($ this ->once ())
300
+ ->method ('match ' )
301
+ ->with ($ url )
302
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \ResourceNotFoundException ))
303
+ ;
304
+ $ low
305
+ ->expects ($ this ->once ())
306
+ ->method ('match ' )
307
+ ->with ($ url )
308
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \ResourceNotFoundException ))
309
+ ;
310
+ $ this ->router ->add ($ low , 10 );
311
+ $ this ->router ->add ($ high , 100 );
312
+
313
+ $ this ->router ->matchRequest (Request::create ('/test ' ));
314
+ }
315
+
219
316
/**
220
317
* If any of the routers throws a not allowed exception and no other matches, we need to see this
221
318
*
@@ -244,6 +341,34 @@ public function testMatchMethodNotAllowed()
244
341
$ this ->router ->match ('/test ' );
245
342
}
246
343
344
+ /**
345
+ * If any of the routers throws a not allowed exception and no other matches, we need to see this
346
+ *
347
+ * @expectedException \Symfony\Component\Routing\Exception\MethodNotAllowedException
348
+ */
349
+ public function testMatchRequestMethodNotAllowed ()
350
+ {
351
+ $ url = '/test ' ;
352
+ list ($ low , $ high ) = $ this ->createRouterMocks ();
353
+
354
+ $ high
355
+ ->expects ($ this ->once ())
356
+ ->method ('match ' )
357
+ ->with ($ url )
358
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \MethodNotAllowedException (array ())))
359
+ ;
360
+ $ low
361
+ ->expects ($ this ->once ())
362
+ ->method ('match ' )
363
+ ->with ($ url )
364
+ ->will ($ this ->throwException (new \Symfony \Component \Routing \Exception \ResourceNotFoundException ))
365
+ ;
366
+ $ this ->router ->add ($ low , 10 );
367
+ $ this ->router ->add ($ high , 100 );
368
+
369
+ $ this ->router ->matchRequest (Request::create ('/test ' ));
370
+ }
371
+
247
372
public function testGenerate ()
248
373
{
249
374
$ url = '/test ' ;
@@ -371,3 +496,7 @@ protected function createRouterMocks()
371
496
abstract class WarmableRouterMock implements \Symfony \Component \Routing \RouterInterface, \Symfony \Component \HttpKernel \CacheWarmer \WarmableInterface
372
497
{
373
498
}
499
+
500
+ abstract class RequestMatcher implements \Symfony \Component \Routing \RouterInterface, \Symfony \Component \Routing \Matcher \RequestMatcherInterface
501
+ {
502
+ }
0 commit comments