Skip to content

Commit 9d81537

Browse files
committed
up: some update for router. and add unit testing for router
1 parent ae7dbed commit 9d81537

File tree

7 files changed

+279
-20
lines changed

7 files changed

+279
-20
lines changed

src/Router/AbstractRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ public static function getGlobalParams()
635635
/**
636636
* @return array
637637
*/
638-
public static function getSupportedMethods()
638+
public static function getSupportedMethods(): array
639639
{
640640
return self::SUPPORTED_METHODS;
641641
}

src/Router/HandlerMapping.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@
2525
*/
2626
class HandlerMapping extends AbstractRouter implements HandlerMappingInterface
2727
{
28-
/**
29-
* @var string
30-
*/
31-
public $defaultRouter = '/index/index';
32-
33-
/**
34-
* default action
35-
*
36-
* @var string
37-
*/
38-
public $defaultAction = 'index';
39-
4028
/** @var int */
4129
private $routeCounter = 0;
4230
private $cacheCounter = 0;
@@ -130,7 +118,7 @@ public function map($methods, $route, $handler, array $opts = [])
130118
* @param string $path
131119
* @return array
132120
*/
133-
public function match($path, $method = 'GET')
121+
public function match($path, $method = 'GET'): array
134122
{
135123
// if enable 'matchAll'
136124
if ($matchAll = $this->matchAll) {

src/Router/RouterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public function map($methods, $route, $handler, array $opts = []);
8686
* [self::FOUND, $path, array () // routeData ]
8787
*
8888
*/
89-
public function match($path, $method = 'GET');
89+
public function match($path, $method = 'GET'): array;
9090

9191
/**
9292
* @return array
9393
*/
94-
public static function getSupportedMethods();
94+
public static function getSupportedMethods(): array;
9595
}

test/Cases/DemoTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace SwoftTest\HttpServer;
44

5+
use PHPUnit\Framework\TestCase;
6+
57
/**
68
*
79
*
@@ -11,10 +13,10 @@
1113
* @copyright Copyright 2010-2016 swoft software
1214
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
1315
*/
14-
class DemoTest extends AbstractTestCase
16+
class DemoTest extends TestCase
1517
{
1618
public function testDemo()
1719
{
1820
$this->assertTrue(true);
1921
}
20-
}
22+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018/3/19
6+
* Time: 上午12:14
7+
*/
8+
9+
namespace SwoftTest\HttpServer;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use Swoft\Http\Server\Router\AbstractRouter;
13+
14+
/**
15+
* @covers AbstractRouter
16+
* @author inhere <[email protected]>
17+
*/
18+
class AbstractRouterTest extends TestCase
19+
{
20+
public function testValidateArguments()
21+
{
22+
$stub = $this->getMockForAbstractClass(AbstractRouter::class);
23+
$ret = $stub->validateArguments('get', 'handler0');
24+
25+
$this->assertEquals($ret, ['GET']);
26+
27+
$this->expectException(\InvalidArgumentException::class);
28+
$stub->validateArguments(null, null);
29+
$stub->validateArguments('get', null);
30+
}
31+
32+
public function testStaticRouteCheck()
33+
{
34+
$ret = AbstractRouter::isStaticRoute('/abc');
35+
$this->assertTrue($ret);
36+
37+
$ret = AbstractRouter::isStaticRoute('/hi/{name}');
38+
$this->assertFalse($ret);
39+
40+
$ret = AbstractRouter::isStaticRoute('/hi/[tom]');
41+
$this->assertFalse($ret);
42+
}
43+
44+
public function testParseParamRoute()
45+
{
46+
$stub = $this->getMockForAbstractClass(AbstractRouter::class);
47+
48+
// 抽象方法才需要配置
49+
// $stub->expects($this->any())
50+
// ->method('parseParamRoute')
51+
// ->will($this->returnValue('foo'));
52+
53+
$conf = [
54+
'handler' => 'some_handler'
55+
];
56+
57+
$ret = $stub->parseParamRoute('/im/{name}/{age}', [], $conf);
58+
$this->assertCount(2, $ret);
59+
$this->assertEquals('im', $ret[0]);// first node
60+
$this->assertArrayHasKey('start', $ret[1]);
61+
$this->assertEquals('/im/', $ret[1]['start']);
62+
$this->assertArrayNotHasKey('include', $ret[1]);
63+
64+
$ret = $stub->parseParamRoute('/path/to/{name}', [], $conf);
65+
$this->assertCount(2, $ret);
66+
$this->assertEquals('path', $ret[0]);
67+
$this->assertArrayHasKey('start', $ret[1]);
68+
$this->assertEquals('/path/to/', $ret[1]['start']);
69+
$this->assertArrayNotHasKey('include', $ret[1]);
70+
71+
$ret = $stub->parseParamRoute('/hi/{name}', [], $conf);
72+
$this->assertCount(2, $ret);
73+
$this->assertEquals('hi', $ret[0]);
74+
$this->assertArrayHasKey('start', $ret[1]);
75+
$this->assertArrayNotHasKey('include', $ret[1]);
76+
77+
$ret = $stub->parseParamRoute('/hi[/{name}]', [], $conf);
78+
$this->assertNull($ret[0]);
79+
$this->assertArrayHasKey('include', $ret[1]);
80+
$this->assertArrayNotHasKey('start', $ret[1]);
81+
82+
$ret = $stub->parseParamRoute('/hi[/tom]', [], $conf);
83+
$this->assertNull($ret[0]);
84+
$this->assertArrayHasKey('include', $ret[1]);
85+
$this->assertArrayNotHasKey('start', $ret[1]);
86+
87+
$ret = $stub->parseParamRoute('/hi/[tom]', [], $conf);
88+
$this->assertEquals('hi', $ret[0]);
89+
$this->assertArrayHasKey('start', $ret[1]);
90+
$this->assertArrayNotHasKey('include', $ret[1]);
91+
92+
$ret = $stub->parseParamRoute('/{category}', [], $conf);
93+
$this->assertNull($ret[0]);
94+
$this->assertNull($ret[1]['include']);
95+
$this->assertArrayHasKey('include', $ret[1]);
96+
$this->assertArrayNotHasKey('start', $ret[1]);
97+
98+
$ret = $stub->parseParamRoute('/blog-{category}', [], $conf);
99+
$this->assertNull($ret[0]);
100+
$this->assertEquals('/blog-', $ret[1]['include']);
101+
$this->assertArrayHasKey('include', $ret[1]);
102+
$this->assertArrayNotHasKey('start', $ret[1]);
103+
104+
// var_dump($ret);die;
105+
}
106+
}
107+
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018/3/19
6+
* Time: 上午12:12
7+
*/
8+
9+
namespace SwoftTest\HttpServer;
10+
11+
use PHPUnit\Framework\TestCase;
12+
use Swoft\Http\Server\Router\HandlerMapping;
13+
14+
/**
15+
* @covers HandlerMapping
16+
* @author inhere <[email protected]>
17+
*/
18+
class HandlerMappingTest extends TestCase
19+
{
20+
private function createRouter()
21+
{
22+
$r = new HandlerMapping();
23+
$r->get('/', 'handler0');
24+
$r->get('/test', 'handler1');
25+
26+
$r->get('/test1[/optional]', 'handler');
27+
28+
$r->get('/{name}', 'handler2');
29+
30+
$r->get('/hi/{name}', 'handler3', [
31+
'params' => [
32+
'name' => '\w+',
33+
]
34+
]);
35+
36+
$r->post('/hi/{name}', 'handler4');
37+
$r->put('/hi/{name}', 'handler5');
38+
39+
return $r;
40+
}
41+
42+
public function testAddRoutes()
43+
{
44+
$router = $this->createRouter();
45+
46+
$this->assertTrue(4 < $router->count());
47+
$this->assertCount(2, $router->getStaticRoutes());
48+
}
49+
50+
51+
public function testStaticRoute()
52+
{
53+
$router = $this->createRouter();
54+
55+
// 1
56+
$ret = $router->match('/', 'GET');
57+
58+
$this->assertCount(3, $ret);
59+
60+
list($status, $path, $route) = $ret;
61+
62+
$this->assertSame(HandlerMapping::FOUND, $status);
63+
$this->assertSame('/', $path);
64+
$this->assertSame('handler0', $route['handler']);
65+
66+
}
67+
68+
public function testOptionalParamRoute()
69+
{
70+
$router = $this->createRouter();
71+
72+
// route: '/test1[/optional]'
73+
$ret = $router->match('/test1', 'GET');
74+
75+
$this->assertCount(3, $ret);
76+
77+
list($status, , $route) = $ret;
78+
79+
$this->assertSame(HandlerMapping::FOUND, $status);
80+
$this->assertSame('handler', $route['handler']);
81+
82+
// route: '/test1[/optional]'
83+
$ret = $router->match('/test1/optional', 'GET');
84+
85+
$this->assertCount(3, $ret);
86+
87+
list($status, , $route) = $ret;
88+
89+
$this->assertSame(HandlerMapping::FOUND, $status);
90+
$this->assertSame('handler', $route['handler']);
91+
92+
}
93+
94+
public function testParamRoute()
95+
{
96+
$router = $this->createRouter();
97+
98+
// route: /{name}
99+
$ret = $router->match('/tom', 'GET');
100+
101+
$this->assertCount(3, $ret);
102+
103+
list($status, $path, $route) = $ret;
104+
105+
$this->assertSame(HandlerMapping::FOUND, $status);
106+
$this->assertSame('/tom', $path);
107+
$this->assertSame('/{name}', $route['original']);
108+
$this->assertSame('handler2', $route['handler']);
109+
110+
// route: /hi/{name}
111+
$ret = $router->match('/hi/tom', 'GET');
112+
113+
$this->assertCount(3, $ret);
114+
// var_dump($ret, $router->getRegularRoutes());die;
115+
list($status, $path, $route) = $ret;
116+
117+
$this->assertSame(HandlerMapping::FOUND, $status);
118+
$this->assertSame('/hi/tom', $path);
119+
$this->assertSame('/hi/{name}', $route['original']);
120+
$this->assertSame('handler3', $route['handler']);
121+
}
122+
123+
public function testMethods()
124+
{
125+
$router = $this->createRouter();
126+
127+
// route: /hi/{name}
128+
$ret = $router->match('/hi/tom', 'post');
129+
130+
$this->assertCount(3, $ret);
131+
132+
list($status, , $route) = $ret;
133+
$this->assertSame(HandlerMapping::FOUND, $status);
134+
$this->assertArrayHasKey('name', $route['matches']);
135+
$this->assertSame('handler4', $route['handler']);
136+
137+
// route: /hi/{name}
138+
$ret = $router->match('/hi/tom', 'put');
139+
140+
list($status, , $route) = $ret;
141+
$this->assertCount(3, $ret);
142+
$this->assertSame(HandlerMapping::FOUND, $status);
143+
$this->assertArrayHasKey('name', $route['matches']);
144+
$this->assertSame('handler5', $route['handler']);
145+
146+
// route: /hi/{name}
147+
$ret = $router->match('/hi/tom', 'delete');
148+
149+
list($status, , $methods) = $ret;
150+
$this->assertCount(3, $ret);
151+
$this->assertSame(HandlerMapping::METHOD_NOT_ALLOWED, $status);
152+
$this->assertCount(3, $methods);
153+
}
154+
}

test/bootstrap.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<?php
2-
require_once dirname(__FILE__, 2) . '/vendor/autoload.php';
3-
require_once dirname(__FILE__, 2) . '/test/config/define.php';
2+
3+
if (file_exists($file = dirname(__DIR__, 3) . '/autoload.php')) {
4+
require $file;
5+
} elseif (file_exists($file = dirname(__DIR__) . '/vendor/autoload.php')) {
6+
require $file;
7+
} else {
8+
exit('OO, The composer autoload file is not found!');
9+
}
10+
11+
require_once __DIR__ . '/config/define.php';
412

513
// init
614
\Swoft\App::$isInTest = true;

0 commit comments

Comments
 (0)