From 4cc8e7d52bbd139469ca863a7ea178beb8b886ff Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 28 Feb 2016 15:04:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Route=E7=B1=BB=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Route.php | 16 ++-- tests/thinkphp/library/think/routeTest.php | 89 ++++++++++++++++++++++ 2 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 tests/thinkphp/library/think/routeTest.php diff --git a/library/think/Route.php b/library/think/Route.php index 6af2aec4..63a6e4c9 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -15,13 +15,13 @@ class Route { // 路由规则 private static $rules = [ - 'GET' => [], - 'POST' => [], - 'PUT' => [], - 'DELETE' => [], - 'HEAD' => [], - 'OPTIONS'=> [], - '*' => [], + 'GET' => [], + 'POST' => [], + 'PUT' => [], + 'DELETE' => [], + 'HEAD' => [], + 'OPTIONS' => [], + '*' => [], ]; // REST路由操作方法定义 @@ -137,8 +137,6 @@ class Route if (0 === strpos($rule, '[')) { $rule = substr($rule, 1, -1); $result = ['routes' => $route, 'option' => $option, 'pattern' => $pattern]; - } elseif (is_array($route)) { - $result = ['route' => !empty($route[0]) ? $route[0] : '', 'option' => !empty($route[1]) ? $route[1] : '', 'pattern' => !empty($route[2]) ? $route[2] : '']; } else { $result = ['route' => $route, 'option' => $option, 'pattern' => $pattern]; } diff --git a/tests/thinkphp/library/think/routeTest.php b/tests/thinkphp/library/think/routeTest.php new file mode 100644 index 00000000..76381db1 --- /dev/null +++ b/tests/thinkphp/library/think/routeTest.php @@ -0,0 +1,89 @@ + +// +---------------------------------------------------------------------- + +/** + * Route测试 + * @author liu21st + */ + +namespace tests\thinkphp\library\think; + +use think\Route; + +class routeTest extends \PHPUnit_Framework_TestCase +{ + + public function testRegister() + { + Route::get('hello/:name', 'index/hello'); + Route::get(['hello/:name' => 'index/hello']); + $this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check('hello/thinkphp')); + } + + public function testParseUrl() + { + $this->assertEquals(['type' => 'module', 'module' => ['hello', null, null]], Route::parseUrl('hello')); + $this->assertEquals(['type' => 'module', 'module' => ['index', 'hello', null]], Route::parseUrl('index/hello')); + $this->assertEquals(['type' => 'module', 'module' => ['index', 'user', 'hello']], Route::parseUrl('index/user/hello')); + $this->assertEquals(['type' => 'module', 'module' => ['index', 'index', 'hello']], Route::parseUrl('index-index-hello', '-')); + } + + public function testCheckRoute() + { + Route::get('hello/:name', 'index/hello'); + Route::get('blog/:id', 'blog/read', [], ['id' => '\d+']); + + $this->assertEquals(false, Route::check('test/thinkphp')); + $this->assertEquals(false, Route::check('blog/thinkphp')); + $this->assertEquals(['type' => 'module', 'module' => [null, 'blog', 'read']], Route::check('blog/5')); + $this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check('hello/thinkphp')); + } + + public function testCheckRouteGroup() + { + Route::pattern(['id' => '\d+', 'name' => '\w{6,25}']); + Route::group('group', [':id' => 'index/hello', ':name' => 'index/say']); + $this->assertEquals(false, Route::check('group/think')); + $this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check('group/10')); + $this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'say']], Route::check('group/thinkphp')); + } + + public function testRouteToModule() + { + Route::get('hello/:name', 'index/hello'); + Route::get('blog/:id', 'blog/read', [], ['id' => '\d+']); + $this->assertEquals(false, Route::check('test/thinkphp')); + $this->assertEquals(false, Route::check('blog/thinkphp')); + $this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check('hello/thinkphp')); + $this->assertEquals(['type' => 'module', 'module' => [null, 'blog', 'read']], Route::check('blog/5')); + } + + public function testRouteToController() + { + Route::get('say/:name', '@app\index\controller\index\hello'); + $this->assertEquals(['type' => 'controller', 'controller' => 'app\index\controller\index\hello', 'params' => ['name' => 'thinkphp']], Route::check('say/thinkphp')); + } + + public function testRouteToMethod() + { + Route::get('user/:name', '\app\index\service\User::get', [], ['name' => '\w+']); + Route::get('info/:name', ['\app\index\model\Info', 'getInfo'], [], ['name' => '\w+']); + $this->assertEquals(['type' => 'method', 'method' => '\app\index\service\User::get', 'params' => ['name' => 'thinkphp']], Route::check('user/thinkphp')); + $this->assertEquals(['type' => 'method', 'method' => ['\app\index\model\Info', 'getInfo'], 'params' => ['name' => 'thinkphp']], Route::check('info/thinkphp')); + } + + public function testRouteToRedirect() + { + Route::get('art/:id', '/article/read/id/:id', [], ['id' => '\d+']); + $this->assertEquals(['type' => 'redirect', 'url' => '/article/read/id/8', 'status' => 301], Route::check('art/8')); + } + +}