Fixed routing to class methods

This commit is contained in:
Marcin Kurczewski 2014-08-30 13:02:03 +02:00
parent e81391a29a
commit 10e6c9f11f
2 changed files with 28 additions and 3 deletions

View file

@ -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)
{

View file

@ -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';
}
}