forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterTest.php
More file actions
208 lines (142 loc) · 5.46 KB
/
Copy pathRouterTest.php
File metadata and controls
208 lines (142 loc) · 5.46 KB
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
<?php namespace CodeIgniter\Router;
class RouterTest extends \CIUnitTestCase
{
/**
* @var \CodeIgniter\Router\RouteCollection $collection
*/
protected $collection;
/**
* vfsStream root directory
* @var
*/
protected $root;
public function setUp()
{
$this->collection = new RouteCollection();
$routes = [
'users' => 'Users::index',
'posts' => 'Blog::posts',
'pages' => 'App\Pages::list_all',
'posts/(:num)' => 'Blog::show/$1',
'posts/(:num)/edit' => 'Blog::edit/$1',
'books/(:num)/(:alpha)/(:num)' => 'Blog::show/$3/$1',
'closure/(:num)/(:alpha)' => function ($num, $str) { return $num.'-'.$str; },
'{locale}/pages' => 'App\Pages::list_all',
'Admin/Admins' => 'App\Admin\Admins::list_all',
];
$this->collection->map($routes);
}
//--------------------------------------------------------------------
public function tearDown()
{
}
//--------------------------------------------------------------------
public function testEmptyURIMatchesDefaults()
{
$router = new Router($this->collection);
$router->handle('');
$this->assertEquals($this->collection->getDefaultController(), $router->controllerName());
$this->assertEquals($this->collection->getDefaultMethod(), $router->methodName());
}
//--------------------------------------------------------------------
public function testURIMapsToController()
{
$router = new Router($this->collection);
$router->handle('users');
$this->assertEquals('\Users', $router->controllerName());
$this->assertEquals('index', $router->methodName());
}
//--------------------------------------------------------------------
public function testURIMapsToControllerAltMethod()
{
$router = new Router($this->collection);
$router->handle('posts');
$this->assertEquals('\Blog', $router->controllerName());
$this->assertEquals('posts', $router->methodName());
}
//--------------------------------------------------------------------
public function testURIMapsToNamespacedController()
{
$router = new Router($this->collection);
$router->handle('pages');
$this->assertEquals('\App\Pages', $router->controllerName());
$this->assertEquals('list_all', $router->methodName());
}
//--------------------------------------------------------------------
public function testURIMapsParamsToBackReferences()
{
$router = new Router($this->collection);
$router->handle('posts/123');
$this->assertEquals('show', $router->methodName());
$this->assertEquals([123], $router->params());
}
//--------------------------------------------------------------------
public function testURIMapsParamsToRearrangedBackReferences()
{
$router = new Router($this->collection);
$router->handle('posts/123/edit');
$this->assertEquals('edit', $router->methodName());
$this->assertEquals([123], $router->params());
}
//--------------------------------------------------------------------
public function testURIMapsParamsToBackReferencesWithUnused()
{
$router = new Router($this->collection);
$router->handle('books/123/sometitle/456');
$this->assertEquals('show', $router->methodName());
$this->assertEquals([456, 123], $router->params());
}
//--------------------------------------------------------------------
public function testClosures()
{
$router = new Router($this->collection);
$router->handle('closure/123/alpha');
$closure = $router->controllerName();
$expects = call_user_func_array($closure, $router->params());
$this->assertTrue(is_callable($router->controllerName()));
$this->assertEquals($expects, '123-alpha');
}
//--------------------------------------------------------------------
public function testAutoRouteFindsControllerWithFileAndMethod()
{
$router = new Router($this->collection);
$router->autoRoute('myController/someMethod');
$this->assertEquals('MyController', $router->controllerName());
$this->assertEquals('someMethod', $router->methodName());
}
//--------------------------------------------------------------------
public function testAutoRouteFindsControllerWithFile()
{
$router = new Router($this->collection);
$router->autoRoute('myController');
$this->assertEquals('MyController', $router->controllerName());
$this->assertEquals('index', $router->methodName());
}
//--------------------------------------------------------------------
public function testAutoRouteFindsControllerWithSubfolder()
{
$router = new Router($this->collection);
mkdir(APPPATH.'Controllers/Subfolder');
$router->autoRoute('subfolder/myController/someMethod');
rmdir(APPPATH.'Controllers/Subfolder');
$this->assertEquals('MyController', $router->controllerName());
$this->assertEquals('someMethod', $router->methodName());
}
//--------------------------------------------------------------------
public function testDetectsLocales()
{
$router = new Router($this->collection);
$router->handle('fr/pages');
$this->assertTrue($router->hasLocale());
$this->assertEquals('fr', $router->getLocale());
}
//--------------------------------------------------------------------
public function testRouteResource()
{
$router = new Router($this->collection);
$router->handle('Admin/Admins');
$this->assertEquals('\App\Admin\Admins', $router->controllerName());
$this->assertEquals('list_all', $router->methodName());
}
//--------------------------------------------------------------------
}