forked from bcit-ci/CodeIgniter
/
Router_test.php
411 lines (340 loc) · 11.1 KB
/
Router_test.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
class Router_test extends CI_TestCase {
private $ci;
/**
* Set up before each test
*/
public function set_up()
{
// Get instance object
$this->ci = $this->ci_instance();
// Create router
$this->router = new Mock_Core_Router();
}
/**
* Test validating route
*
* @covers CI_Router::validate_route
*/
public function test_validate_route()
{
// Mock loader with package paths
$this->_mock_loader();
$alt_dir = 'alternative';
$alt_root = $this->ci_vfs_mkdir($alt_dir);
$alt_path = $this->ci_vfs_path($alt_dir);
$this->ci->load->paths = array($alt_path, $this->ci_app_path);
// Create controller in app path
$dir = 'controllers';
$ctlr = 'app_ctlr';
$this->ci_vfs_create($ctlr, '', $this->ci_app_root, $dir);
// Is the default function routed?
$expect = array($this->ci_app_path, '', $ctlr, 'index');
$this->assertEquals($expect, $this->router->validate_route($ctlr));
// Create a controller in a subdirectory of the alternate path
$sub = 'bass';
$ctlr2 = 'sub_ctlr';
$func2 = 'drop';
$this->ci_vfs_create($ctlr2, '', $alt_root, array($dir, $sub));
// Do the alternate path and subdirectory work?
$expect = array($alt_path, $sub, $ctlr2, $func2);
$this->assertEquals($expect, $this->router->validate_route(array($sub, $ctlr2, $func2)));
// Set default controller with argument
$def_ctlr = 'foo';
$def_func = 'bar';
$def_arg = 'baz';
$this->ci_vfs_create($def_ctlr, '', $this->ci_app_root, $dir);
$this->router->default_ctlr($def_ctlr.'/'.$def_func.'/'.$def_arg);
// Do the default and argument work?
$expect = array($this->ci_app_path, '', $def_ctlr, $def_func, $def_arg);
$this->assertEquals($expect, $this->router->validate_route(''));
// Set route
$route3 = 'remap/request';
$ctlr3 = 'real_ctlr';
$func3 = 'action';
$this->router->routes = array($route3 => $ctlr3.'/'.$func3);
// Create actual controller
$this->ci_vfs_create($ctlr3, '', $this->ci_app_root, $dir);
// Does the request get remapped?
$expect = array($this->ci_app_path, '', $ctlr3, $func3);
$this->assertEquals($expect, $this->router->validate_route($route3));
// Set route with number arg
$rctlr = 'remap';
$rfunc = 'request';
$route4 = $rctlr.'/'.$rfunc;
$ctlr4 = 'real_ctlr';
$func4 = 'action';
$this->router->routes = array($route4.'/(:num)' => $ctlr4.'/'.$func4.'/$1');
// Create actual controller
$this->ci_vfs_create($ctlr4, '', $this->ci_app_root, $dir);
// Does the argument get passed?
$arg4 = '42';
$expect = array($this->ci_app_path, '', $ctlr4, $func4, $arg4);
$this->assertEquals($expect, $this->router->validate_route($route4.'/'.$arg4));
// Create remap controller
$this->ci_vfs_create($rctlr, '', $this->ci_app_root, $dir);
// Is the route ignored for a non-number?
$arg = 'foo';
$expect = array($this->ci_app_path, '', $rctlr, $rfunc, $arg);
$this->assertEquals($expect, $this->router->validate_route($route4.'/'.$arg));
// Does a non-existent route fail?
$this->assertFalse($this->router->validate_route('non_ctlr'));
}
/**
* Test default routing
*
* @covers CI_Router::_set_routing
*/
public function test_default_routing()
{
// Load mock Config
$this->ci_set_config();
// Mock up URI
$this->_mock_uri();
// Mock loader with package paths
$this->_mock_loader();
$this->ci->load->paths = array($this->ci_app_path);
// Set up routes config with default
$stack = array('main', 'handler');
$default = implode('/', $stack);
$routes = array('default_controller' => $default);
$this->ci->config->to_get($routes);
// Create default controller
$this->ci_vfs_create($stack[0], '', $this->ci_app_root, 'controllers');
// Set routing
$this->router->_set_routing();
// Did the default get set?
$this->assertEquals($default, $this->router->default_ctlr());
// Did the right URI methods get called?
$expect = array('_fetch_uri_string', '_remove_url_suffix', '_explode_segments', '_reindex_segments');
$this->assertObjectHasAttribute('called', $this->ci->uri);
$this->assertEquals($expect, $this->ci->uri->called);
// Did the route stack get set correctly?
$expect = array($this->ci_app_path, '', $stack[0], $stack[1]);
$this->assertEquals($expect, $this->router->fetch_route());
// Are the routed segments correct?
$this->assertEquals($stack, $this->ci->uri->rsegments);
}
/**
* Test query string routing
*
* @covers CI_Router::_set_routing
*/
public function test_query_routing()
{
// Load mock Config with triggers
$ctrigger = 'ctlr';
$dtrigger = 'dir';
$ftrigger = 'func';
$config = array(
'enable_query_strings' => TRUE,
'controller_trigger' => $ctrigger,
'directory_trigger' => $dtrigger,
'function_trigger' => $ftrigger
);
$this->ci_set_config($config);
// Mock up URI
$this->_mock_uri();
// Mock loader with package paths
$this->_mock_loader();
$this->ci->load->paths = array($this->ci_app_path);
// Set up routes config
$routes = array('default_controller' => '');
$this->ci->config->to_get($routes);
// Set query args
$dir = 'qdir';
$ctlr = 'query_ctlr';
$func = 'get';
$oldget = $_GET;
$_GET = array($dtrigger => $dir, $ctrigger => $ctlr, $ftrigger => $func);
// Create controller
$this->ci_vfs_create($ctlr, '', $this->ci_app_root, array('controllers', $dir));
// Set routing
$this->router->_set_routing();
// Did the route stack get set?
$expect = array($this->ci_app_path, $dir.'/', $ctlr, $func);
$this->assertEquals($expect, $this->router->fetch_route());
// Reset _GET
$_GET = $oldget;
}
/**
* Test route remapping
*
* @covers CI_Router::_set_routing
*/
public function test_remap_routing()
{
// Load mock Config
$this->ci_set_config();
// Mock up URI
$this->_mock_uri();
// Mock loader with package paths
$this->_mock_loader();
$this->ci->load->paths = array($this->ci_app_path);
// Set up routes config
$route = 'main/path';
$arg = 'switch';
$ctlr = 'other';
$func = 'handler';
$routes = array(
'default_controller' => '',
$route.'/(:any)' => $ctlr.'/'.$func.'/$1',
);
$this->ci->config->to_get($routes);
// Create controller and set segments
$this->ci_vfs_create($ctlr, '', $this->ci_app_root, 'controllers');
$this->ci->uri->segments = array($ctlr, $func, $arg);
// Set routing
$this->router->_set_routing();
// Did the route stack get set correctly?
$expect = array($this->ci_app_path, '', $ctlr, $func, $arg);
$this->assertEquals($expect, $this->router->fetch_route());
}
/**
* Test getting error route
*
* @covers CI_Router::get_error_route
*/
public function test_error_route()
{
// Mock loader with package paths
$this->_mock_loader();
$this->ci->load->paths = array($this->ci_app_path);
// Do we get FALSE with no error route?
$this->assertFalse($this->router->get_error_route());
// Set up error override
$dir = 'controllers';
$ectlr = 'error_ctlr';
$efunc = 'error_func';
$this->ci_vfs_create($ectlr, '', $this->ci_app_root, $dir);
$this->router->routes = array('error_override' => $ectlr.'/'.$efunc);
// Do we get the error route when set?
$expect = array($this->ci_app_path, '', $ectlr, $efunc);
$this->assertEquals($expect, $this->router->get_error_route());
// Set up 404 override
$ctlr = 'e404_ctlr';
$func = 'e404_func';
$this->ci_vfs_create($ctlr, '', $this->ci_app_root, $dir);
$this->router->routes = array('404_override' => $ctlr.'/'.$func);
// Do we get the error route when set?
$expect = array($this->ci_app_path, '', $ctlr, $func);
$this->assertEquals($expect, $this->router->get_error_route(TRUE));
}
/**
* Test set/fetch methods
*
* @covers CI_Router::set_path
* @covers CI_Router::fetch_path
* @covers CI_Router::set_directory
* @covers CI_Router::fetch_directory
* @covers CI_Router::set_class
* @covers CI_Router::fetch_class
* @covers CI_Router::set_method
* @covers CI_Router::fetch_method
* @covers CI_Router::fetch_route
*/
public function test_set_fetch()
{
// Set/fetch path
$path = $this->ci_vfs_path('application/');
$this->router->set_path($path);
$this->assertEquals($path, $this->router->fetch_path());
// Set/fetch directory - should add slash
$directory = 'custom';
$this->router->set_directory($directory);
$this->assertEquals($directory.'/', $this->router->fetch_directory());
// Set/fetch class
$class = 'my_ctlr';
$this->router->set_class($class);
$this->assertEquals($class, $this->router->fetch_class());
// Set/fetch method
$method = 'unusual';
$this->router->set_method($method);
$this->assertEquals($method, $this->router->fetch_method());
// Do we get all the parts in the stack?
$expect = array($path, $directory.'/', $class, $method);
$this->assertEquals($expect, $this->router->fetch_route());
}
/**
* Test setting overrides
*
* @covers CI_Router::_set_overrides
*/
public function test_set_overrides()
{
// Load mock Config
$this->ci_set_config();
// Mock up URI
$this->_mock_uri();
// Mock loader with package paths
$this->_mock_loader();
$alt_dir = 'alternative';
$alt_root = $this->ci_vfs_mkdir($alt_dir);
$alt_path = $this->ci_vfs_path($alt_dir);
$this->ci->load->paths = array($alt_path, $this->ci_app_path);
// Set up routes config
$routes = array('default_controller' => '');
$this->ci->config->to_get($routes);
// Create controller and set segments
$ctlr = 'orig_ctlr';
$func = 'orig_func';
$this->ci_vfs_create($ctlr, '', $this->ci_app_root, 'controllers');
$this->ci->uri->segments = array($ctlr, $func);
// Set routing
$this->router->_set_routing();
// Did the route stack get set correctly?
$expect = array($this->ci_app_path, '', $ctlr, $func);
$this->assertEquals($expect, $this->router->fetch_route());
// Set overrides
$opath = $alt_path;
$odir = 'new_dir';
$octlr = 'new_ctlr';
$ofunc = 'new_func';
$override = array(
'path' => $opath,
'directory' => $odir,
'controller' => $octlr,
'function' => $ofunc
);
$this->router->_set_overrides($override);
// Did the route stack get updated?
$expect = array($opath, $odir.'/', $octlr, $ofunc);
$this->assertEquals($expect, $this->router->fetch_route());
}
/**
* Mock up URI
*/
private function _mock_uri()
{
// Create mock URI class
$class = 'Router_URI';
if ( ! class_exists($class))
{
$code = 'class '.$class.' { public $segments = array(); public $rsegments = array(); '.
'public function _filter_uri($arg1) { return $arg1; } '.
'public function _fetch_uri_string() { $this->called[] = __FUNCTION__; } '.
'public function _remove_url_suffix() { $this->called[] = __FUNCTION__; } '.
'public function _explode_segments() { $this->called[] = __FUNCTION__; } '.
'public function _reindex_segments() { $this->called[] = __FUNCTION__; } '.
'}';
eval($code);
}
// Attach mock URI
$this->ci->uri = new $class();
}
/**
* Mock up Loader
*/
private function _mock_loader()
{
// Create VFS tree
$this->ci_vfs_setup();
// Create mock Loader class
$class = 'Router_Loader';
if ( ! class_exists($class))
{
eval('class '.$class.' { public $paths; public function get_package_paths() { return $this->paths; } }');
}
$this->ci->load = new $class();
}
}