/
RouteCollectionTest.php
391 lines (350 loc) · 12.5 KB
/
RouteCollectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Routing;
use Cake\Routing\RouteBuilder;
use Cake\Routing\RouteCollection;
use Cake\Routing\Route\Route;
use Cake\TestSuite\TestCase;
class RouteCollectionTest extends TestCase
{
/**
* Setup method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->collection = new RouteCollection();
}
/**
* Test parse() throws an error on unknown routes.
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A route matching "/" could not be found
*/
public function testParseMissingRoute()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$result = $this->collection->parse('/');
$this->assertEquals([], $result, 'Should not match, missing /b');
}
/**
* Test parsing routes.
*
* @return void
*/
public function testParse()
{
$routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
$result = $this->collection->parse('/b/');
$expected = [
'controller' => 'Articles',
'action' => 'index',
'pass' => [],
'plugin' => null,
'key' => 'value',
'_matchedRoute' => '/b',
];
$this->assertEquals($expected, $result);
$result = $this->collection->parse('/b/the-thing?one=two');
$expected = [
'controller' => 'Articles',
'action' => 'view',
'id' => 'the-thing',
'pass' => [],
'plugin' => null,
'key' => 'value',
'?' => ['one' => 'two'],
'_matchedRoute' => '/b/:id',
];
$this->assertEquals($expected, $result);
$result = $this->collection->parse('/b/media/search');
$expected = [
'key' => 'value',
'pass' => [],
'plugin' => null,
'controller' => 'Media',
'action' => 'search',
'_matchedRoute' => '/b/media/search/*',
];
$this->assertEquals($expected, $result);
$result = $this->collection->parse('/b/media/search/thing');
$expected = [
'key' => 'value',
'pass' => ['thing'],
'plugin' => null,
'controller' => 'Media',
'action' => 'search',
'_matchedRoute' => '/b/media/search/*',
];
$this->assertEquals($expected, $result);
}
/**
* Test that parse decodes URL data before matching.
* This is important for multibyte URLs that pass through URL rewriting.
*
* @return void
*/
public function testParseEncodedBytesInFixedSegment()
{
$routes = new RouteBuilder($this->collection, '/');
$routes->connect('/ден/:day-:month', ['controller' => 'Events', 'action' => 'index']);
$url = '/%D0%B4%D0%B5%D0%BD/15-%D0%BE%D0%BA%D1%82%D0%BE%D0%BC%D0%B2%D1%80%D0%B8?test=foo';
$result = $this->collection->parse($url);
$expected = [
'pass' => [],
'plugin' => null,
'controller' => 'Events',
'action' => 'index',
'day' => '15',
'month' => 'октомври',
'?' => ['test' => 'foo'],
'_matchedRoute' => '/ден/:day-:month',
];
$this->assertEquals($expected, $result);
}
/**
* Test that parsing checks all the related path scopes.
*
* @return void
*/
public function testParseFallback()
{
$routes = new RouteBuilder($this->collection, '/', []);
$routes->resources('Articles');
$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);
$routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
$result = $this->collection->parse('/articles/add');
$expected = [
'controller' => 'Articles',
'action' => 'add',
'plugin' => null,
'pass' => [],
'_matchedRoute' => '/:controller/:action',
];
$this->assertEquals($expected, $result);
}
/**
* Test match() throws an error on unknown routes.
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A route matching "array (
*/
public function testMatchError()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
}
/**
* Test matching routes.
*
* @return void
*/
public function testMatch()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
$result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
$this->assertEquals('b', $result);
$result = $this->collection->match(
['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
$context
);
$this->assertEquals('b/thing', $result);
}
/**
* Test matching routes with names
*
* @return void
*/
public function testMatchNamed()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/', ['controller' => 'Articles']);
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
$result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
$this->assertEquals('/b/2', $result);
$result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
$this->assertEquals('b/2', $result);
}
/**
* Test match() throws an error on named routes that fail to match
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @expectedExceptionMessage A named route was found for "fail", but matching failed
*/
public function testMatchNamedError()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/:lang/articles', ['controller' => 'Articles'], ['_name' => 'fail']);
$this->collection->match(['_name' => 'fail'], $context);
}
/**
* Test matching routes with names and failing
*
* @expectedException \Cake\Routing\Exception\MissingRouteException
* @return void
*/
public function testMatchNamedMissingError()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/b');
$routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
$this->collection->match(['_name' => 'derp'], $context);
}
/**
* Test matching plugin routes.
*
* @return void
*/
public function testMatchPlugin()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
$routes->connect('/', ['controller' => 'Contacts']);
$result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
$this->assertEquals('contacts', $result);
}
/**
* Test that prefixes increase the specificity of a route match.
*
* Connect the admin route after the non prefixed version, this means
* the non-prefix route would have a more specific name (users:index)
*
* @return void
*/
public function testMatchPrefixSpecificity()
{
$context = [
'_base' => '/',
'_scheme' => 'http',
'_host' => 'example.org',
];
$routes = new RouteBuilder($this->collection, '/');
$routes->connect('/:action/*', ['controller' => 'Users']);
$routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
$url = [
'plugin' => null,
'prefix' => 'admin',
'controller' => 'Users',
'action' => 'index'
];
$result = $this->collection->match($url, $context);
$this->assertEquals('admin/Users', $result);
$url = [
'plugin' => null,
'controller' => 'Users',
'action' => 'index'
];
$result = $this->collection->match($url, $context);
$this->assertEquals('index', $result);
}
/**
* Test getting named routes.
*
* @return void
*/
public function testNamed()
{
$routes = new RouteBuilder($this->collection, '/l');
$routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
$routes->connect('/:controller/:action/*');
$all = $this->collection->named();
$this->assertCount(1, $all);
$this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
$this->assertEquals('/l/:controller', $all['cntrl']->template);
}
/**
* Test the add() and routes() method.
*
* @return void
*/
public function testAddingRoutes()
{
$one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
$this->collection->add($one);
$this->collection->add($two);
$routes = $this->collection->routes();
$this->assertCount(2, $routes);
$this->assertSame($one, $routes[0]);
$this->assertSame($two, $routes[1]);
}
/**
* Test the add() with some _name.
*
* @expectedException \Cake\Routing\Exception\DuplicateNamedRouteException
*
* @return void
*/
public function testAddingDuplicateNamedRoutes()
{
$one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
$this->collection->add($one, ['_name' => 'test']);
$this->collection->add($two, ['_name' => 'test']);
}
/**
* Test basic get/set of extensions.
*
* @return void
*/
public function testExtensions()
{
$this->assertEquals([], $this->collection->extensions());
$this->collection->extensions('json');
$this->assertEquals(['json'], $this->collection->extensions());
$this->collection->extensions(['rss', 'xml']);
$this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
$this->collection->extensions(['csv'], false);
$this->assertEquals(['csv'], $this->collection->extensions());
}
}