Skip to content

Commit 831d4dd

Browse files
committed
add findRouteByName, modify max_params, and add RouterTests
1 parent 0e48987 commit 831d4dd

4 files changed

Lines changed: 151 additions & 54 deletions

File tree

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<file>tests/ContainerTests.php</file>
55
<file>tests/BagTests.php</file>
66
<file>tests/HookTests.php</file>
7-
<file>tests/AppTests.php</file>
7+
<file>tests/RouterTests.php</file>
88
<file>tests/RunAppTests.php</file>
99
</testsuite>
1010
</testsuites>

src/Router/Router.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Router {
2525
* digunakan juga untuk acuan dummy group pada method toRegex()
2626
* @var int
2727
*/
28-
protected $max_params = 5;
28+
protected $max_params = 10;
2929

3030
/**
3131
* Banyak route maksimum di dalam sebuah regex
@@ -246,6 +246,21 @@ public function findMatch($path, $method)
246246
return $this->dispatch($method, $path);
247247
}
248248

249+
/**
250+
* Get route by given name
251+
*
252+
* @param string $route_name
253+
* @return null|Route
254+
*/
255+
public function findRouteByName($name)
256+
{
257+
$routes = $this->getRoutes();
258+
foreach($routes as $route) {
259+
if($route->getName() == $name) return $route;
260+
}
261+
return null;
262+
}
263+
249264
/**
250265
* Tranfrom route path into Regex
251266
*

tests/AppTests.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/RouterTests.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
use Rakit\Framework\App;
4+
use Rakit\Framework\Router\Route;
5+
6+
class RouterTests extends PHPUnit_Framework_TestCase {
7+
8+
protected $app;
9+
10+
public function setUp() {
11+
$this->app = new App('router-test', ['app' => ['debug' => true]]);
12+
$this->router = $this->app->router;
13+
}
14+
15+
public function tearDown() {
16+
$this->app = null;
17+
}
18+
19+
public function testDispatchRouteGet()
20+
{
21+
$this->router->post('/route/:param', 'handler');
22+
$this->router->get('/route/:param', 'handler');
23+
$this->router->put('/route/:param', 'handler');
24+
25+
$route = $this->router->dispatch('GET', '/route/value');
26+
27+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
28+
$this->assertEquals('GET', $route->getMethod());
29+
}
30+
31+
public function testDispatchRoutePost()
32+
{
33+
$this->router->get('/route/:param', 'handler');
34+
$this->router->post('/route/:param', 'handler');
35+
$this->router->put('/route/:param', 'handler');
36+
37+
$route = $this->router->dispatch('POST', '/route/value');
38+
39+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
40+
$this->assertEquals('POST', $route->getMethod());
41+
}
42+
43+
public function testDispatchRoutePut()
44+
{
45+
$this->router->get('/route/:param', 'handler');
46+
$this->router->put('/route/:param', 'handler');
47+
$this->router->post('/route/:param', 'handler');
48+
49+
$route = $this->router->dispatch('PUT', '/route/value');
50+
51+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
52+
$this->assertEquals('PUT', $route->getMethod());
53+
}
54+
55+
public function testDispatchRoutePatch()
56+
{
57+
$this->router->get('/route/:param', 'handler');
58+
$this->router->patch('/route/:param', 'handler');
59+
$this->router->post('/route/:param', 'handler');
60+
61+
$route = $this->router->dispatch('PATCH', '/route/value');
62+
63+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
64+
$this->assertEquals('PATCH', $route->getMethod());
65+
}
66+
67+
public function testDispatchRouteDelete()
68+
{
69+
$this->router->get('/route/:param', 'handler');
70+
$this->router->delete('/route/:param', 'handler');
71+
$this->router->post('/route/:param', 'handler');
72+
73+
$route = $this->router->dispatch('DELETE', '/route/value');
74+
75+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
76+
$this->assertEquals('DELETE', $route->getMethod());
77+
}
78+
79+
public function testDispatchParameters()
80+
{
81+
$this->router->get('/route/:foo/:bar', 'handler');
82+
83+
$route = $this->router->dispatch('GET', '/route/param1/param2');
84+
85+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
86+
87+
$params = $route->params;
88+
$this->assertEquals(2, count($params));
89+
$this->assertEquals($params['foo'], 'param1');
90+
$this->assertEquals($params['bar'], 'param2');
91+
}
92+
93+
public function testDispatchOptionalParameter()
94+
{
95+
$this->router->get('/route/:foo(/:bar(/:baz))', 'handler');
96+
97+
foreach(['/route/one' => 1, '/route/one/two' => 2, '/route/one/two/three' => 3] as $path => $expected_count) {
98+
$route = $this->router->dispatch('GET', $path);
99+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
100+
$this->assertEquals($expected_count, count($route->params));
101+
}
102+
}
103+
104+
public function testDispatchCustomRegexParameter()
105+
{
106+
$this->router->get('/edit/:id', 'handler')->where('id', '\d{2}');
107+
108+
$tests = [
109+
'/edit/foo' => false,
110+
'/edit/12foo' => false,
111+
'/edit/foo99' => false,
112+
'/edit/1' => false,
113+
'/edit/123' => false,
114+
'/edit/11' => true,
115+
];
116+
117+
foreach($tests as $path => $expected) {
118+
$route = $this->router->dispatch('GET', $path);
119+
$this->assertEquals($expected, !is_null($route));
120+
}
121+
}
122+
123+
public function testRouteNaming()
124+
{
125+
$this->router->get('/login', 'handler')->name('form-login');
126+
$this->router->post('/login', 'handler')->name('post-login');
127+
128+
$route = $this->router->findRouteByName('post-login');
129+
130+
$this->assertInstanceOf('Rakit\Framework\Router\Route', $route);
131+
$this->assertEquals('POST', $route->getMethod());
132+
}
133+
134+
}

0 commit comments

Comments
 (0)