Permalink
Browse files

Lysine\MVC\Router测试脚本

1 parent 48f8702 commit 82772d47ce702d69a82cedb22c60fd87b97b60cc @yeaha committed Aug 23, 2012
Showing with 138 additions and 19 deletions.
  1. +16 −19 src/mvc.php
  2. +8 −0 tests/Fixture/Controller.php
  3. +16 −0 tests/Mock/Router.php
  4. +98 −0 tests/RouterTest.php
View
@@ -37,7 +37,7 @@ public function execute($uri = null, $method = null) {
if (!req()->isGET() && ($params = post() ?: put()))
\Lysine\logger()->debug('Parameters: '. http_build_query($params));
- $response = $this->getRouter()->dispatch($uri, $method);
+ $response = $this->getRouter()->execute($uri, $method);
return $response instanceof \Lysine\HTTP\Response
? $response
@@ -70,10 +70,7 @@ class Router {
public function __construct(array $config = null) {
if (isset($config['namespace']))
- foreach ($config['namespace'] as $path => $ns) {
- $path = $this->normalizePath($path);
- $this->namespace[$path] = $ns;
- }
+ $this->namespace = $config['namespace'];
if (isset($config['rewrite']))
$this->rewrite = $config['rewrite'];
@@ -82,18 +79,8 @@ public function __construct(array $config = null) {
$this->base_uri = $this->normalizePath($config['base_uri']);
}
- public function dispatch($uri, $method) {
- $path = parse_url(strtolower($uri), PHP_URL_PATH);
- $path = $this->normalizePath($path);
-
- if ($base_uri = $this->base_uri) {
- if (strpos($path.'/', $base_uri.'/') !== 0)
- throw HTTP\Error::factory(HTTP::NOT_FOUND);
-
- $path = $this->normalizePath(substr($path, strlen($base_uri)));
- }
-
- list($class, $params) = $this->matchClass($path);
+ public function execute($uri, $method) {
+ list($class, $params) = $this->dispatch($uri);
\Lysine\logger()->debug('Dispatch to controller: '. $class);
@@ -130,16 +117,24 @@ public function dispatch($uri, $method) {
return $response;
}
- //////////////////// protected method ////////////////////
+ public function dispatch($uri) {
+ $path = $this->normalizePath( parse_url($uri, PHP_URL_PATH) );
+
+ if ($base_uri = $this->base_uri) {
+ if (strpos($path.'/', $base_uri.'/') !== 0)
+ throw HTTP\Error::factory(HTTP::NOT_FOUND);
+
+ $path = $this->normalizePath(substr($path, strlen($base_uri)));
+ }
- protected function matchClass($path) {
foreach ($this->rewrite as $re => $class) {
if (preg_match($re, $path, $match))
return array($class, array_slice($match, 1));
}
// 路径对应的controller namespace
foreach ($this->namespace as $ns_path => $ns) {
+ $ns_path = $this->normalizePath($ns_path);
if ($ns_path != '/' && strpos($path.'/', $ns_path.'/') !== 0)
continue;
@@ -155,6 +150,8 @@ protected function matchClass($path) {
throw HTTP\Error::factory(HTTP::NOT_FOUND);
}
+ //////////////////// protected method ////////////////////
+
protected function normalizePath($path) {
return '/'. trim(strtolower($path), '/');
}
@@ -0,0 +1,8 @@
+<?php
+namespace Test\Fixture;
+
+class Controller {
+ public function GET() {
+ return 'GET';
+ }
+}
View
@@ -0,0 +1,16 @@
+<?php
+namespace Test\Mock;
+
+class Router extends \Lysine\MVC\Router {
+ public function setNamespace(array $namespace) {
+ $this->namespace = $namespace;
+ }
+
+ public function setRewrite(array $rules) {
+ $this->rewrite = array_merge($this->rewrite, $rules);
+ }
+
+ public function setBaseUri($base_uri) {
+ $this->base_uri = $base_uri;
+ }
+}
View
@@ -0,0 +1,98 @@
+<?php
+namespace Test;
+
+class RouterTest extends \PHPUnit_Framework_TestCase {
+ protected $router;
+
+ protected function setUp() {
+ $config = array(
+ 'namespace' => array(
+ '/' => '\Controller',
+ ),
+ );
+
+ $this->router = new \Test\Mock\Router($config);
+ }
+
+ public function testNamespace() {
+ $this->router->setNamespace(array(
+ '/admin' => '\Admin\Controller',
+ '/blog/' => '\Blog\Controller',
+ '/' => '\Action',
+ ));
+
+ list($class,) = $this->router->dispatch('/blog');
+ $this->assertEquals('\Blog\Controller\Index', $class);
+
+ list($class,) = $this->router->dispatch('/blogs');
+ $this->assertEquals('\Action\Blogs', $class);
+
+ list($class,) = $this->router->dispatch('/admin');
+ $this->assertEquals('\Admin\Controller\Index', $class);
+
+ list($class,) = $this->router->dispatch('/admin/user');
+ $this->assertEquals('\Admin\Controller\User', $class);
+
+ list($class,) = $this->router->dispatch('/admin/user/');
+ $this->assertEquals('\Admin\Controller\User', $class);
+
+ list($class,) = $this->router->dispatch('/aDmin/User');
+ $this->assertEquals('\Admin\Controller\User', $class);
+
+ list($class,) = $this->router->dispatch('/');
+ $this->assertEquals('\Action\Index', $class);
+ }
+
+ public function testBaseUri() {
+ $this->router->setBaseUri('/admin');
+
+ list($class,) = $this->router->dispatch('/admin/');
+ $this->assertEquals('\Controller\Index', $class);
+
+ list($class,) = $this->router->dispatch('/admin/test');
+ $this->assertEquals('\Controller\Test', $class);
+ }
+
+ /**
+ * @expectedException \Lysine\HTTP\Error
+ * @expectedExceptionCode 404
+ */
+ public function testBaseUriException() {
+ $this->router->setBaseUri('/admin');
+ $this->router->dispatch('/admina');
+ }
+
+ public function testRewrite() {
+ $this->router->setRewrite(array(
+ '#^/topic/(\d+)#' => '\Controller\Topic',
+ ));
+
+ list($class, $params) = $this->router->dispatch('/topic/123');
+ $this->assertEquals('\Controller\Topic', $class);
+ $this->assertContains(123, $params);
+
+ list($class, $params) = $this->router->dispatch('/topic/abc');
+ $this->assertEquals('\Controller\Topic\Abc', $class);
+ }
+
+ /**
+ * @expectedException \Lysine\HTTP\Error
+ * @expectedExceptionCode 405
+ */
+ public function testMethodNotAllowedException() {
+ $this->router->setRewrite(array(
+ '#^/#' => '\Test\Fixture\Controller',
+ ));
+
+ $this->router->execute('/', 'POST');
+ }
+
+ public function testExecute() {
+ $this->router->setRewrite(array(
+ '#^/#' => '\Test\Fixture\Controller',
+ ));
+
+ $response = $this->router->execute('/', 'GET');
+ $this->assertEquals('GET', $response);
+ }
+}

0 comments on commit 82772d4

Please sign in to comment.