Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Routing\Tests\Matcher;
13
 
14
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
15
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16
use Symfony\Component\Routing\Matcher\UrlMatcher;
17
use Symfony\Component\Routing\Route;
18
use Symfony\Component\Routing\RouteCollection;
19
use Symfony\Component\Routing\RequestContext;
20
 
21
class UrlMatcherTest extends \PHPUnit_Framework_TestCase
22
{
23
    public function testNoMethodSoAllowed()
24
    {
25
        $coll = new RouteCollection();
26
        $coll->add('foo', new Route('/foo'));
27
 
28
        $matcher = new UrlMatcher($coll, new RequestContext());
29
        $this->assertInternalType('array', $matcher->match('/foo'));
30
    }
31
 
32
    public function testMethodNotAllowed()
33
    {
34
        $coll = new RouteCollection();
35
        $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post')));
36
 
37
        $matcher = new UrlMatcher($coll, new RequestContext());
38
 
39
        try {
40
            $matcher->match('/foo');
41
            $this->fail();
42
        } catch (MethodNotAllowedException $e) {
43
            $this->assertEquals(array('POST'), $e->getAllowedMethods());
44
        }
45
    }
46
 
47
    public function testHeadAllowedWhenRequirementContainsGet()
48
    {
49
        $coll = new RouteCollection();
50
        $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
51
 
52
        $matcher = new UrlMatcher($coll, new RequestContext('', 'head'));
53
        $this->assertInternalType('array', $matcher->match('/foo'));
54
    }
55
 
56
    public function testMethodNotAllowedAggregatesAllowedMethods()
57
    {
58
        $coll = new RouteCollection();
59
        $coll->add('foo1', new Route('/foo', array(), array(), array(), '', array(), array('post')));
60
        $coll->add('foo2', new Route('/foo', array(), array(), array(), '', array(), array('put', 'delete')));
61
 
62
        $matcher = new UrlMatcher($coll, new RequestContext());
63
 
64
        try {
65
            $matcher->match('/foo');
66
            $this->fail();
67
        } catch (MethodNotAllowedException $e) {
68
            $this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
69
        }
70
    }
71
 
72
    public function testMatch()
73
    {
74
        // test the patterns are matched and parameters are returned
75
        $collection = new RouteCollection();
76
        $collection->add('foo', new Route('/foo/{bar}'));
77
        $matcher = new UrlMatcher($collection, new RequestContext());
78
        try {
79
            $matcher->match('/no-match');
80
            $this->fail();
81
        } catch (ResourceNotFoundException $e) {
82
        }
83
        $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
84
 
85
        // test that defaults are merged
86
        $collection = new RouteCollection();
87
        $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
88
        $matcher = new UrlMatcher($collection, new RequestContext());
89
        $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
90
 
91
        // test that route "method" is ignored if no method is given in the context
92
        $collection = new RouteCollection();
93
        $collection->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get', 'head')));
94
        $matcher = new UrlMatcher($collection, new RequestContext());
95
        $this->assertInternalType('array', $matcher->match('/foo'));
96
 
97
        // route does not match with POST method context
98
        $matcher = new UrlMatcher($collection, new RequestContext('', 'post'));
99
        try {
100
            $matcher->match('/foo');
101
            $this->fail();
102
        } catch (MethodNotAllowedException $e) {
103
        }
104
 
105
        // route does match with GET or HEAD method context
106
        $matcher = new UrlMatcher($collection, new RequestContext());
107
        $this->assertInternalType('array', $matcher->match('/foo'));
108
        $matcher = new UrlMatcher($collection, new RequestContext('', 'head'));
109
        $this->assertInternalType('array', $matcher->match('/foo'));
110
 
111
        // route with an optional variable as the first segment
112
        $collection = new RouteCollection();
113
        $collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
114
        $matcher = new UrlMatcher($collection, new RequestContext());
115
        $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
116
        $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
117
 
118
        $collection = new RouteCollection();
119
        $collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
120
        $matcher = new UrlMatcher($collection, new RequestContext());
121
        $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
122
        $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
123
 
124
        // route with only optional variables
125
        $collection = new RouteCollection();
126
        $collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array()));
127
        $matcher = new UrlMatcher($collection, new RequestContext());
128
        $this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/'));
129
        $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a'));
130
        $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b'));
131
    }
132
 
133
    public function testMatchWithPrefixes()
134
    {
135
        $collection = new RouteCollection();
136
        $collection->add('foo', new Route('/{foo}'));
137
        $collection->addPrefix('/b');
138
        $collection->addPrefix('/a');
139
 
140
        $matcher = new UrlMatcher($collection, new RequestContext());
141
        $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
142
    }
143
 
144
    public function testMatchWithDynamicPrefix()
145
    {
146
        $collection = new RouteCollection();
147
        $collection->add('foo', new Route('/{foo}'));
148
        $collection->addPrefix('/b');
149
        $collection->addPrefix('/{_locale}');
150
 
151
        $matcher = new UrlMatcher($collection, new RequestContext());
152
        $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
153
    }
154
 
155
    public function testMatchSpecialRouteName()
156
    {
157
        $collection = new RouteCollection();
158
        $collection->add('$péß^a|', new Route('/bar'));
159
 
160
        $matcher = new UrlMatcher($collection, new RequestContext());
161
        $this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
162
    }
163
 
164
    public function testMatchNonAlpha()
165
    {
166
        $collection = new RouteCollection();
167
        $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
168
        $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+')));
169
 
170
        $matcher = new UrlMatcher($collection, new RequestContext());
171
        $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar'));
172
        $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar'));
173
    }
174
 
175
    public function testMatchWithDotMetacharacterInRequirements()
176
    {
177
        $collection = new RouteCollection();
178
        $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
179
 
180
        $matcher = new UrlMatcher($collection, new RequestContext());
181
        $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
182
    }
183
 
184
    public function testMatchOverriddenRoute()
185
    {
186
        $collection = new RouteCollection();
187
        $collection->add('foo', new Route('/foo'));
188
 
189
        $collection1 = new RouteCollection();
190
        $collection1->add('foo', new Route('/foo1'));
191
 
192
        $collection->addCollection($collection1);
193
 
194
        $matcher = new UrlMatcher($collection, new RequestContext());
195
 
196
        $this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
197
        $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
198
        $this->assertEquals(array(), $matcher->match('/foo'));
199
    }
200
 
201
    public function testMatchRegression()
202
    {
203
        $coll = new RouteCollection();
204
        $coll->add('foo', new Route('/foo/{foo}'));
205
        $coll->add('bar', new Route('/foo/bar/{foo}'));
206
 
207
        $matcher = new UrlMatcher($coll, new RequestContext());
208
        $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
209
 
210
        $collection = new RouteCollection();
211
        $collection->add('foo', new Route('/{bar}'));
212
        $matcher = new UrlMatcher($collection, new RequestContext());
213
        try {
214
            $matcher->match('/');
215
            $this->fail();
216
        } catch (ResourceNotFoundException $e) {
217
        }
218
    }
219
 
220
    public function testDefaultRequirementForOptionalVariables()
221
    {
222
        $coll = new RouteCollection();
223
        $coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
224
 
225
        $matcher = new UrlMatcher($coll, new RequestContext());
226
        $this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
227
    }
228
 
229
    public function testMatchingIsEager()
230
    {
231
        $coll = new RouteCollection();
232
        $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
233
 
234
        $matcher = new UrlMatcher($coll, new RequestContext());
235
        $this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
236
    }
237
 
238
    public function testAdjacentVariables()
239
    {
240
        $coll = new RouteCollection();
241
        $coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => 'y|Y')));
242
 
243
        $matcher = new UrlMatcher($coll, new RequestContext());
244
        // 'w' eagerly matches as much as possible and the other variables match the remaining chars.
245
        // This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
246
        // Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
247
        $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'Y', 'z' => 'Z', '_format' => 'xml', '_route' => 'test'), $matcher->match('/wwwwwxYZ.xml'));
248
        // As 'y' has custom requirement and can only be of value 'y|Y', it will leave  'ZZZ' to variable z.
249
        // So with carefully chosen requirements adjacent variables, can be useful.
250
        $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'ZZZ', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxyZZZ'));
251
        // z and _format are optional.
252
        $this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'default-z', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxy'));
253
 
254
        $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
255
        $matcher->match('/wxy.html');
256
    }
257
 
258
    public function testOptionalVariableWithNoRealSeparator()
259
    {
260
        $coll = new RouteCollection();
261
        $coll->add('test', new Route('/get{what}', array('what' => 'All')));
262
        $matcher = new UrlMatcher($coll, new RequestContext());
263
 
264
        $this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
265
        $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
266
 
267
        // Usually the character in front of an optional parameter can be left out, e.g. with pattern '/get/{what}' just '/get' would match.
268
        // But here the 't' in 'get' is not a separating character, so it makes no sense to match without it.
269
        $this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
270
        $matcher->match('/ge');
271
    }
272
 
273
    public function testRequiredVariableWithNoRealSeparator()
274
    {
275
        $coll = new RouteCollection();
276
        $coll->add('test', new Route('/get{what}Suffix'));
277
        $matcher = new UrlMatcher($coll, new RequestContext());
278
 
279
        $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
280
    }
281
 
282
    public function testDefaultRequirementOfVariable()
283
    {
284
        $coll = new RouteCollection();
285
        $coll->add('test', new Route('/{page}.{_format}'));
286
        $matcher = new UrlMatcher($coll, new RequestContext());
287
 
288
        $this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
289
    }
290
 
291
    /**
292
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
293
     */
294
    public function testDefaultRequirementOfVariableDisallowsSlash()
295
    {
296
        $coll = new RouteCollection();
297
        $coll->add('test', new Route('/{page}.{_format}'));
298
        $matcher = new UrlMatcher($coll, new RequestContext());
299
 
300
        $matcher->match('/index.sl/ash');
301
    }
302
 
303
    /**
304
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
305
     */
306
    public function testDefaultRequirementOfVariableDisallowsNextSeparator()
307
    {
308
        $coll = new RouteCollection();
309
        $coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
310
        $matcher = new UrlMatcher($coll, new RequestContext());
311
 
312
        $matcher->match('/do.t.html');
313
    }
314
 
315
    /**
316
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
317
     */
318
    public function testSchemeRequirement()
319
    {
320
        $coll = new RouteCollection();
321
        $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
322
        $matcher = new UrlMatcher($coll, new RequestContext());
323
        $matcher->match('/foo');
324
    }
325
 
326
    /**
327
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
328
     */
329
    public function testCondition()
330
    {
331
        $coll = new RouteCollection();
332
        $route = new Route('/foo');
333
        $route->setCondition('context.getMethod() == "POST"');
334
        $coll->add('foo', $route);
335
        $matcher = new UrlMatcher($coll, new RequestContext());
336
        $matcher->match('/foo');
337
    }
338
 
339
    public function testDecodeOnce()
340
    {
341
        $coll = new RouteCollection();
342
        $coll->add('foo', new Route('/foo/{foo}'));
343
 
344
        $matcher = new UrlMatcher($coll, new RequestContext());
345
        $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
346
    }
347
 
348
    public function testCannotRelyOnPrefix()
349
    {
350
        $coll = new RouteCollection();
351
 
352
        $subColl = new RouteCollection();
353
        $subColl->add('bar', new Route('/bar'));
354
        $subColl->addPrefix('/prefix');
355
        // overwrite the pattern, so the prefix is not valid anymore for this route in the collection
356
        $subColl->get('bar')->setPath('/new');
357
 
358
        $coll->addCollection($subColl);
359
 
360
        $matcher = new UrlMatcher($coll, new RequestContext());
361
        $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
362
    }
363
 
364
    public function testWithHost()
365
    {
366
        $coll = new RouteCollection();
367
        $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
368
 
369
        $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
370
        $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
371
    }
372
 
373
    public function testWithHostOnRouteCollection()
374
    {
375
        $coll = new RouteCollection();
376
        $coll->add('foo', new Route('/foo/{foo}'));
377
        $coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
378
        $coll->setHost('{locale}.example.com');
379
 
380
        $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
381
        $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
382
 
383
        $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
384
        $this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
385
    }
386
 
387
    /**
388
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
389
     */
390
    public function testWithOutHostHostDoesNotMatch()
391
    {
392
        $coll = new RouteCollection();
393
        $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
394
 
395
        $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
396
        $matcher->match('/foo/bar');
397
    }
398
 
399
    /**
400
     * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
401
     */
402
    public function testPathIsCaseSensitive()
403
    {
404
        $coll = new RouteCollection();
405
        $coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
406
 
407
        $matcher = new UrlMatcher($coll, new RequestContext());
408
        $matcher->match('/en');
409
    }
410
 
411
    public function testHostIsCaseInsensitive()
412
    {
413
        $coll = new RouteCollection();
414
        $coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
415
 
416
        $matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
417
        $this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
418
    }
419
}