Modify router to accept variables and do test
This commit is contained in:
parent
d51baaf2c2
commit
6666d674a6
@ -65,7 +65,11 @@ class Router
|
|||||||
$action = $this->convertToCamelCaps($action);
|
$action = $this->convertToCamelCaps($action);
|
||||||
|
|
||||||
if (is_callable([$object, $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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,14 +20,14 @@ class RouterTest extends \PHPUnit\Framework\TestCase
|
|||||||
[
|
[
|
||||||
'{controller}/{action}',
|
'{controller}/{action}',
|
||||||
[],
|
[],
|
||||||
'home/index',
|
'posts/new',
|
||||||
['controller' => 'home', 'action' => 'index']
|
['controller' => 'posts', 'action' => 'new']
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'{controller}/{id:\d+}/{action}',
|
'{controller}/{id:\d+}/{action}',
|
||||||
[],
|
[],
|
||||||
'home/150/index',
|
'posts/3/edit',
|
||||||
['controller' => 'home', 'action' => 'index', 'id' => '150']
|
['controller' => 'posts', 'action' => 'edit', 'id' => '3']
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -51,19 +51,18 @@ class RouterTest extends \PHPUnit\Framework\TestCase
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @test
|
* @test
|
||||||
* @dataProvider routeProvider
|
|
||||||
*/
|
*/
|
||||||
public function dispatchingRouteSuccess($route, $params, $url)
|
public function dispatchingRouteSuccess()
|
||||||
{
|
{
|
||||||
// $url = 'home/index';
|
$url = 'posts/113/edit';
|
||||||
|
|
||||||
$router = new Router();
|
$router = new Router();
|
||||||
|
|
||||||
// $router->add('{controller}/{action}');
|
$router->add('{controller}/{id:\d+}/{action}');
|
||||||
$router->add($route, $params);
|
// $router->add($route, $params);
|
||||||
|
|
||||||
$this->assertTrue($router->match($url));
|
$this->assertTrue($router->match($url));
|
||||||
|
|
||||||
$this->assertFalse($router->dispatch($url));
|
$this->assertTrue($router->dispatch($url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user