From 10e6c9f11fd59ca57dc003aa6acb47cc82a62167 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Aug 2014 13:02:03 +0200 Subject: [PATCH] Fixed routing to class methods --- src/Route.php | 4 +++- tests/RouterTest.php | 27 +++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) 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'; + } }