Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Bramus/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
namespace Bramus\Router;

class InvalidControllerException extends \Exception {}

/**
* Class Router.
*/
Expand Down Expand Up @@ -468,6 +470,8 @@ private function invoke($fn, $params = array())
$controller = $this->getNamespace() . '\\' . $controller;
}

$invalidControllerMsg="controller class '$controller' with method '$method' is not invokable, the class and method should be non-abstract, and the method public";

try {
$reflectedMethod = new \ReflectionMethod($controller, $method);
// Make sure it's callable
Expand All @@ -481,9 +485,11 @@ private function invoke($fn, $params = array())
}
call_user_func_array(array($controller, $method), $params);
}
} else {
throw new InvalidControllerException($invalidControllerMsg,1001);
}
} catch (\ReflectionException $reflectionException) {
// The controller class is not available or the class does not have the method $method
throw new InvalidControllerException($invalidControllerMsg,1002);
}
}
}
Expand Down
59 changes: 56 additions & 3 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

namespace {

class Handler
class Handler {
public function notFound() {
echo "route not found";
}
}

abstract class AbstractTestController {
public abstract function doSomething1();
}

class TestController extends AbstractTestController
{
public function notfound()
public function doSomething1()
{
echo 'route not found';
echo 'called';
}

private function doSomething2() {
echo 'called';
}
}

Expand Down Expand Up @@ -127,6 +141,45 @@ public function testStaticRoute()
ob_end_clean();
}

private function invokeRoute($classRoute) {
// Create Router
$router = new \Bramus\Router\Router();

$router->get('/invokeMe', $classRoute);

$_SERVER['REQUEST_URI'] = '/invokeMe';
$router->run();
}

/**
* @expectedException \Bramus\Router\InvalidControllerException
*/
public function testControllerClassExists()
{
$this->invokeRoute('NotExistingClass@doSomething1');
}

/**
* @expectedException \Bramus\Router\InvalidControllerException
*/
public function testControllerMethodNonAbstract()
{
$this->invokeRoute('AbstractTestController@doSomething1');
}

/**
* @expectedException \Bramus\Router\InvalidControllerException
*/
public function testControllerMethodPublic()
{
$this->invokeRoute('AbstractTestController@doSomething2');
}

public function testControllerMethodInvokable()
{
$this->invokeRoute('TestController@doSomething1');
}

public function testStaticRouteUsingShorthand()
{
// Create Router
Expand Down