Modify router to accept variables and do test

This commit is contained in:
Gregorio Chiko Putra 2017-08-31 10:21:55 +07:00
parent d51baaf2c2
commit 6666d674a6
2 changed files with 14 additions and 11 deletions

View File

@ -65,7 +65,11 @@ class Router
$action = $this->convertToCamelCaps($action);
if (is_callable([$object, $action])) {
$object->$action();
if (array_key_exists('id', $this->params)) {
$var = $this->params['id'];
return $object->$action($var);
}
return $object->$action();
}
}
}

View File

@ -20,14 +20,14 @@ class RouterTest extends \PHPUnit\Framework\TestCase
[
'{controller}/{action}',
[],
'home/index',
['controller' => 'home', 'action' => 'index']
'posts/new',
['controller' => 'posts', 'action' => 'new']
],
[
'{controller}/{id:\d+}/{action}',
[],
'home/150/index',
['controller' => 'home', 'action' => 'index', 'id' => '150']
'posts/3/edit',
['controller' => 'posts', 'action' => 'edit', 'id' => '3']
]
];
}
@ -51,19 +51,18 @@ class RouterTest extends \PHPUnit\Framework\TestCase
/**
*
* @test
* @dataProvider routeProvider
*/
public function dispatchingRouteSuccess($route, $params, $url)
public function dispatchingRouteSuccess()
{
// $url = 'home/index';
$url = 'posts/113/edit';
$router = new Router();
// $router->add('{controller}/{action}');
$router->add($route, $params);
$router->add('{controller}/{id:\d+}/{action}');
// $router->add($route, $params);
$this->assertTrue($router->match($url));
$this->assertFalse($router->dispatch($url));
$this->assertTrue($router->dispatch($url));
}
}