-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathRouteTest.php
More file actions
448 lines (360 loc) · 15.6 KB
/
RouteTest.php
File metadata and controls
448 lines (360 loc) · 15.6 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
namespace think\tests;
use Closure;
use Mockery as m;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
use think\helper\Str;
use think\Request;
use think\response\Redirect;
use think\response\View;
use think\Route;
class RouteTest extends TestCase
{
use InteractsWithApp;
/** @var Route|MockInterface */
protected $route;
protected function tearDown(): void
{
m::close();
}
protected function setUp(): void
{
$this->prepareApp();
$this->config->shouldReceive('get')->with('route')->andReturn(['url_route_must' => true]);
$this->route = new Route($this->app);
}
/**
* @param $path
* @param string $method
* @param string $host
* @return m\Mock|Request
*/
protected function makeRequest($path, $method = 'GET', $host = 'localhost')
{
$request = m::mock(Request::class)->makePartial();
$request->shouldReceive('host')->andReturn($host);
$request->shouldReceive('pathinfo')->andReturn($path);
$request->shouldReceive('url')->andReturn('/' . $path);
$request->shouldReceive('method')->andReturn(strtoupper($method));
return $request;
}
public function testSimpleRequest()
{
$this->route->get('foo', function () {
return 'get-foo';
});
$this->route->put('foo', function () {
return 'put-foo';
});
$this->route->group(function () {
$this->route->post('foo', function () {
return 'post-foo';
});
});
$request = $this->makeRequest('foo', 'post');
$response = $this->route->dispatch($request);
$this->assertEquals(200, $response->getCode());
$this->assertEquals('post-foo', $response->getContent());
$request = $this->makeRequest('foo', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals(200, $response->getCode());
$this->assertEquals('get-foo', $response->getContent());
}
public function testGroup()
{
$this->route->group(function () {
$this->route->group('foo', function () {
$this->route->post('bar', function () {
return 'hello,world!';
});
});
});
$request = $this->makeRequest('foo/bar', 'post');
$response = $this->route->dispatch($request);
$this->assertEquals(200, $response->getCode());
$this->assertEquals('hello,world!', $response->getContent());
}
public function testAllowCrossDomain()
{
$this->route->get('foo', function () {
return 'get-foo';
})->allowCrossDomain(['some' => 'bar']);
$request = $this->makeRequest('foo', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('bar', $response->getHeader('some'));
$this->assertArrayHasKey('Access-Control-Allow-Credentials', $response->getHeader());
//$this->expectException(RouteNotFoundException::class);
$request = $this->makeRequest('foo2', 'options');
$this->route->dispatch($request);
}
public function testControllerDispatch()
{
$this->route->get('foo', 'Foo/bar');
$controller = m::Mock(\stdClass::class);
$this->app->shouldReceive('parseClass')->with('controller', 'Foo', '')->andReturn($controller->mockery_getName());
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
$controller->shouldReceive('bar')->andReturn('bar');
$request = $this->makeRequest('foo');
$response = $this->route->dispatch($request);
$this->assertEquals('bar', $response->getContent());
}
public function testEmptyControllerDispatch()
{
$this->route->get('foo', 'Foo/bar');
$controller = m::Mock(\stdClass::class);
$this->app->shouldReceive('parseClass')->with('controller', 'Error', '')->andReturn($controller->mockery_getName());
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
$controller->shouldReceive('bar')->andReturn('bar');
$request = $this->makeRequest('foo');
$response = $this->route->dispatch($request);
$this->assertEquals('bar', $response->getContent());
}
protected function createMiddleware($times = 1)
{
$middleware = m::mock(Str::random(5));
$middleware->shouldReceive('handle')->times($times)->andReturnUsing(function ($request, Closure $next) {
return $next($request);
});
$this->app->shouldReceive('make')->with($middleware->mockery_getName())->andReturn($middleware);
return $middleware;
}
public function testControllerWithMiddleware()
{
$this->route->get('foo', 'Foo/bar');
$controller = m::mock(FooClass::class);
$controller->middleware = [
$this->createMiddleware()->mockery_getName() . ":params1:params2",
$this->createMiddleware(0)->mockery_getName() => ['except' => 'bar'],
$this->createMiddleware()->mockery_getName() => ['only' => 'bar'],
[
'middleware' => [$this->createMiddleware()->mockery_getName(), [new \stdClass()]],
'options' => ['only' => 'bar'],
],
];
$this->app->shouldReceive('parseClass')->with('controller', 'Foo', '')->andReturn($controller->mockery_getName());
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
$controller->shouldReceive('bar')->once()->andReturn('bar');
$request = $this->makeRequest('foo');
$response = $this->route->dispatch($request);
$this->assertEquals('bar', $response->getContent());
}
public function testRedirectDispatch()
{
$this->route->redirect('foo', 'http://localhost', 302);
$request = $this->makeRequest('foo');
$this->app->shouldReceive('make')->with(Request::class)->andReturn($request);
$response = $this->route->dispatch($request);
$this->assertInstanceOf(Redirect::class, $response);
$this->assertEquals(302, $response->getCode());
$this->assertEquals('http://localhost', $response->getData());
}
public function testViewDispatch()
{
$this->route->view('foo', 'index/hello', ['city' => 'shanghai']);
$request = $this->makeRequest('foo');
$response = $this->route->dispatch($request);
$this->assertInstanceOf(View::class, $response);
$this->assertEquals(['city' => 'shanghai'], $response->getVars());
$this->assertEquals('index/hello', $response->getData());
}
public function testDomainBindResponse()
{
$this->route->domain('test', function () {
$this->route->get('/', function () {
return 'Hello,ThinkPHP';
});
});
$request = $this->makeRequest('', 'get', 'test.domain.com');
$response = $this->route->dispatch($request);
$this->assertEquals('Hello,ThinkPHP', $response->getContent());
$this->assertEquals(200, $response->getCode());
}
public function testResourceRouting()
{
// Test basic resource registration (returns ResourceRegister when not lazy)
$resource = $this->route->resource('users', 'Users');
$this->assertTrue($resource instanceof \think\route\Resource || $resource instanceof \think\route\ResourceRegister);
// Test REST methods configuration
$restMethods = $this->route->getRest();
$this->assertIsArray($restMethods);
$this->assertArrayHasKey('index', $restMethods);
$this->assertArrayHasKey('create', $restMethods);
$this->assertArrayHasKey('save', $restMethods);
$this->assertArrayHasKey('read', $restMethods);
$this->assertArrayHasKey('edit', $restMethods);
$this->assertArrayHasKey('update', $restMethods);
$this->assertArrayHasKey('delete', $restMethods);
// Test custom REST method modification
$this->route->rest('custom', ['get', '/custom', 'customAction']);
$customMethod = $this->route->getRest('custom');
$this->assertEquals(['get', '/custom', 'customAction'], $customMethod);
}
public function testUrlGeneration()
{
$this->route->get('user/<id>', 'User/detail')->name('user.detail');
$this->route->post('user', 'User/save')->name('user.save');
$urlBuild = $this->route->buildUrl('user.detail', ['id' => 123]);
$this->assertInstanceOf(\think\route\Url::class, $urlBuild);
$urlBuild = $this->route->buildUrl('user.save');
$this->assertInstanceOf(\think\route\Url::class, $urlBuild);
}
public function testRouteParameterBinding()
{
$this->route->get('user/<id>', function ($id) {
return "User ID: $id";
});
$request = $this->makeRequest('user/123', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('User ID: 123', $response->getContent());
// Test multiple parameters
$this->route->get('post/<year>/<month>', function ($year, $month) {
return "Year: $year, Month: $month";
});
$request = $this->makeRequest('post/2024/12', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('Year: 2024, Month: 12', $response->getContent());
}
public function testRoutePatternValidation()
{
$this->route->get('user/<id>', function ($id) {
return "User ID: $id";
})->pattern(['id' => '\d+']);
// Valid numeric ID
$request = $this->makeRequest('user/123', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('User ID: 123', $response->getContent());
// Test pattern with name validation
$this->route->get('profile/<name>', function ($name) {
return "Profile: $name";
})->pattern(['name' => '[a-zA-Z]+']);
$request = $this->makeRequest('profile/john', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('Profile: john', $response->getContent());
}
public function testMissRoute()
{
$this->route->get('home', function () {
return 'home page';
});
$this->route->miss(function () {
return 'Page not found';
});
// Test existing route
$request = $this->makeRequest('home', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('home page', $response->getContent());
// Test miss route
$request = $this->makeRequest('nonexistent', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('Page not found', $response->getContent());
}
public function testRouteMiddleware()
{
$middleware = $this->createMiddleware();
$this->route->get('protected', function () {
return 'protected content';
})->middleware($middleware->mockery_getName());
$request = $this->makeRequest('protected', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('protected content', $response->getContent());
}
public function testRouteOptions()
{
$this->route->get('api/<version>/users', function ($version) {
return "API Version: $version";
})->option(['version' => '1.0']);
$request = $this->makeRequest('api/v2/users', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('API Version: v2', $response->getContent());
}
public function testRouteCache()
{
// Test route configuration
$config = $this->route->config();
$this->assertIsArray($config);
$caseConfig = $this->route->config('url_case_sensitive');
$this->assertIsBool($caseConfig);
// Test route name management
$this->route->get('test', function () {
return 'test';
})->name('test.route');
$names = $this->route->getName('test.route');
$this->assertIsArray($names);
}
public function testGroupWithVariables()
{
// 测试分组内包含变量的路由
$this->route->group('api/<version>', function () {
// 更具体的路由先注册
$this->route->get('users/<id>', function ($version, $id) {
return "API Version: $version - User ID: $id";
});
$this->route->get('users', function ($version) {
return "API Version: $version - Users List";
});
$this->route->post('users', function ($version) {
return "API Version: $version - Create User";
});
});
// 测试GET请求到 api/v1/users
$request = $this->makeRequest('api/v1/users', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('API Version: v1 - Users List', $response->getContent());
$this->assertEquals(200, $response->getCode());
// 测试GET请求到 api/v2/users/123
$request = $this->makeRequest('api/v2/users/123', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('API Version: v2 - User ID: 123', $response->getContent());
$this->assertEquals(200, $response->getCode());
// 测试POST请求到 api/v1/users
$request = $this->makeRequest('api/v1/users', 'post');
$response = $this->route->dispatch($request);
$this->assertEquals('API Version: v1 - Create User', $response->getContent());
$this->assertEquals(200, $response->getCode());
}
public function testGroupWithVariablesAndMiddleware()
{
// 测试分组内包含变量的路由与中间件
$middleware = $this->createMiddleware();
$this->route->group('api/<version>', function () use ($middleware) {
$this->route->get('data', function ($version) {
return "API $version Data";
})->middleware($middleware->mockery_getName());
});
$request = $this->makeRequest('api/v3/data', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('API v3 Data', $response->getContent());
$this->assertEquals(200, $response->getCode());
}
public function testVariableRouteWithConstraints()
{
// 测试变量路由的参数约束
$this->route->group('api/<version>', function () {
$this->route->get('users', function ($version) {
return "API Version: $version";
})->pattern(['version' => 'v[1-9]']); // 约束版本号为v1-v9
});
// 测试有效版本号
$request = $this->makeRequest('api/v1/users', 'get');
$response = $this->route->dispatch($request);
$this->assertEquals('API Version: v1', $response->getContent());
// 测试无效版本号应该不匹配
$request = $this->makeRequest('api/v10/users', 'get');
try {
$response = $this->route->dispatch($request);
// 如果没有异常,检查是否返回错误页面
$this->assertStringContainsString('系统发生错误', $response->getContent());
} catch (\think\exception\RouteNotFoundException $e) {
// 如果抛出路由未找到异常,这是预期的
$this->assertTrue(true);
}
}
}
class FooClass
{
public $middleware = [];
public function bar()
{
}
}