From 2839fb62a6556985cbe2c24457da6f016e48885d Mon Sep 17 00:00:00 2001 From: rxwyun <1120309488@qq.com> Date: Wed, 28 Dec 2022 13:43:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0route=20option=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/RouteConfigTest.php | 122 +++++++++++++++++++++++++++++++------- 1 file changed, 100 insertions(+), 22 deletions(-) diff --git a/tests/RouteConfigTest.php b/tests/RouteConfigTest.php index 6c94dcf8..86b18989 100644 --- a/tests/RouteConfigTest.php +++ b/tests/RouteConfigTest.php @@ -1,7 +1,13 @@ + * + * document http://s.w7.cc/index.php?c=wiki&do=view&id=317&list=2284 + * + * visited https://www.rangine.com/ for more details */ namespace W7\Tests; @@ -15,22 +21,30 @@ use W7\Http\Message\Server\Request; class TestMiddleware extends MiddlewareAbstract { - } class Test1Middleware extends MiddlewareAbstract { - } class RouteConfigTest extends TestCase { public function testFuncAdd() { - Router::post('/user', function () {return '/user';}); - Router::name('user1')->middleware('AppCheckMiddleware')->get('/user/{name}', function () {return '/user/{name}';}); - Router::post('/user/get', function () {return '/user';}); + Router::post('/user', function () { + return '/user'; + }); + Router::name('user1')->middleware('AppCheckMiddleware')->get('/user/{name}', function () { + return '/user/{name}'; + }); + Router::post('/user/get', function () { + return '/user'; + }); Router::middleware('AppCheckMiddleware')->name('test2')->group('/module1', function (\W7\Core\Route\Router $route) { - $route->post('/info', function () {return '/module1/info';}); - $route->name('test-colsure')->post('/build', function () {return '/module1/build';}); + $route->post('/info', function () { + return '/module1/info'; + }); + $route->name('test-colsure')->post('/build', function () { + return '/module1/build'; + }); }); Router::name('test3')->group('/module3', function (\W7\Core\Route\Router $route) { @@ -39,28 +53,44 @@ public function testFuncAdd() { }); Router::name('group-name')->middleware(['AppCheckMiddleware', 'GatewayCheckSiteMiddleware'])->group('/module2', function (\W7\Core\Route\Router $route) { - $route->get('/info', function () {return '/module2/info';}); + $route->get('/info', function () { + return '/module2/info'; + }); $route->get('/info1', 'Module\InfoController@build'); $route->name('test-info1')->get('/info2', 'Module\InfoController@build'); - $route->options('/info', function () {return '/module2/build';}); + $route->options('/info', function () { + return '/module2/build'; + }); $route->name('test4')->group('/module3', function (\W7\Core\Route\Router $route) { $route->name('test4.info')->post('/info', 'Module\InfoController@info'); $route->name('test-build')->post('/build', 'Module\InfoController@build'); - $route->name('test-handle')->post('/handle', function () {return 'Module\InfoController@build';}); - $route->post('/handle1', function () {return 'Module\InfoController@build';}); + $route->name('test-handle')->post('/handle', function () { + return 'Module\InfoController@build'; + }); + $route->post('/handle1', function () { + return 'Module\InfoController@build'; + }); $route->middleware('CheckAccessTokenMiddleware')->name('test5')->group('/module4', function (\W7\Core\Route\Router $route) { $route->post('/info', 'Module\InfoController@info'); $route->name('test-build')->post('/build', 'Module\InfoController@build'); - $route->name('test-handle')->post('/handle', function () {return 'Module\InfoController@build';}); - $route->post('/handle1', function () {return 'Module\InfoController@build';}); + $route->name('test-handle')->post('/handle', function () { + return 'Module\InfoController@build'; + }); + $route->post('/handle1', function () { + return 'Module\InfoController@build'; + }); }); $route->group('/module5', function (\W7\Core\Route\Router $route) { $route->name('test-info')->post('/info/{info}', 'Module\InfoController@info'); $route->post('/info1/{info}', 'Module\InfoController@info'); $route->name('test-build')->post('/build', 'Module\InfoController@build'); - $route->name('test-handle')->post('/handle', function () {return 'Module\InfoController@build';}); - $route->post('/handle1', function () {return 'Module\InfoController@build';}); + $route->name('test-handle')->post('/handle', function () { + return 'Module\InfoController@build'; + }); + $route->post('/handle1', function () { + return 'Module\InfoController@build'; + }); }); }); }); @@ -164,6 +194,48 @@ public function testGroup() { $this->assertSame('index', $result[1]['handler'][1]); } + public function testRouteOption() { + Router::option([ + 'test' => 1 + ])->post('/option', function () { + return '/user'; + }); + + Router::option([ + 'test1' => 1 + ])->name('group-option')->group('/option-g', function (\W7\Core\Route\Router $route) { + $route->option([ + 'test' => 1 + ])->get('/info', function () { + return '/module2/info'; + }); + $route->option([ + 'test2' => 1 + ])->name('test4')->group('/info1', function (\W7\Core\Route\Router $route) { + $route->option([ + 'test3' => 1 + ])->get('/info', function () { + return '/module2/info'; + }); + }); + }); + + $routeInfo = Router::getData(); + $dispatch = new GroupCountBased($routeInfo); + + $result = $dispatch->dispatch('POST', '/option'); + $this->assertArrayHasKey('test', $result[1]['option']); + + $result = $dispatch->dispatch('GET', '/option-g/info'); + $this->assertArrayHasKey('test1', $result[1]['option']); + $this->assertArrayHasKey('test', $result[1]['option']); + + $result = $dispatch->dispatch('GET', '/option-g/info1/info'); + $this->assertArrayHasKey('test1', $result[1]['option']); + $this->assertArrayHasKey('test2', $result[1]['option']); + $this->assertArrayHasKey('test3', $result[1]['option']); + } + public function testMulti() { Router::add('GET', '/multi', function () { return 'success'; @@ -179,7 +251,7 @@ public function testMulti() { } public function testStaticRoute() { - try{ + try { Router::get('/static', 'static/index.html'); } catch (\Throwable $e) { $this->assertSame('route handler static/index.html error', $e->getMessage()); @@ -195,9 +267,15 @@ public function testStaticRoute() { } public function testRouteUrl() { - Router::name("url_name_test")->post('/url/user/get', function () {return '/user';}); - Router::name("url_name_test_params")->post('/url/user/get/{name}', function () {return '/user';}); - Router::name("url_name_test1")->post('/url/user/get1', function () {return '/user';}); + Router::name('url_name_test')->post('/url/user/get', function () { + return '/user'; + }); + Router::name('url_name_test_params')->post('/url/user/get/{name}', function () { + return '/user'; + }); + Router::name('url_name_test1')->post('/url/user/get1', function () { + return '/user'; + }); $routeCollector = new RouteCollector(new Std(), new \FastRoute\DataGenerator\GroupCountBased()); foreach (Router::getData()[0] as $routes) { @@ -231,4 +309,4 @@ public function testRouteUrl() { $this->assertSame('http://test.domain.com/test/re', $generator->to('/test/re')); $this->assertSame('https://test.domain.com/test/re', $generator->secure('/test/re')); } -} \ No newline at end of file +}