diff --git a/src/Route.php b/src/Route.php index dc870ffe..3ee77b11 100644 --- a/src/Route.php +++ b/src/Route.php @@ -31,7 +31,9 @@ final class Route private function getRouteArguments($matches) { - $reflectionFunction = new \ReflectionFunction($this->route); + $reflectionFunction = is_array($this->route) + ? new \ReflectionMethod($this->route[0], $this->route[1]) + : new \ReflectionFunction($this->route); $arguments = []; foreach ($reflectionFunction->getParameters() as $reflectionParameter) { diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 588cfde7..8fbfdec1 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -25,7 +25,7 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase { $router = new \Szurubooru\Router; $router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); }); - $this->setExpectedException('\DomainException'); + $this->setExpectedException(\DomainException::class); $router->handle('POST', '/test'); } @@ -33,7 +33,7 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase { $router = new \Szurubooru\Router; $router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); }); - $this->setExpectedException('\DomainException'); + $this->setExpectedException(\DomainException::class); $router->handle('GET', '/test2'); } @@ -105,4 +105,27 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase $output = $router->handle('GET', '/test'); $this->assertEquals('ok', $output); } + + public function testRoutingToClassMethods() + { + $router = new \Szurubooru\Router; + $testController = new TestController(); + $router->get('/normal', [$testController, 'normalRoute']); + $router->get('/static', [\Szurubooru\Tests\TestController::class, 'staticRoute']); + $this->assertEquals('normal', $router->handle('GET', '/normal')); + $this->assertEquals('static', $router->handle('GET', '/static')); + } +} + +class TestController +{ + public function normalRoute() + { + return 'normal'; + } + + public static function staticRoute() + { + return 'static'; + } }