Fixed routing to class methods
This commit is contained in:
parent
e81391a29a
commit
10e6c9f11f
2 changed files with 28 additions and 3 deletions
|
@ -31,7 +31,9 @@ final class Route
|
||||||
|
|
||||||
private function getRouteArguments($matches)
|
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 = [];
|
$arguments = [];
|
||||||
foreach ($reflectionFunction->getParameters() as $reflectionParameter)
|
foreach ($reflectionFunction->getParameters() as $reflectionParameter)
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,7 +25,7 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$router = new \Szurubooru\Router;
|
$router = new \Szurubooru\Router;
|
||||||
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
|
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
|
||||||
$this->setExpectedException('\DomainException');
|
$this->setExpectedException(\DomainException::class);
|
||||||
$router->handle('POST', '/test');
|
$router->handle('POST', '/test');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$router = new \Szurubooru\Router;
|
$router = new \Szurubooru\Router;
|
||||||
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
|
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
|
||||||
$this->setExpectedException('\DomainException');
|
$this->setExpectedException(\DomainException::class);
|
||||||
$router->handle('GET', '/test2');
|
$router->handle('GET', '/test2');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,4 +105,27 @@ final class PostDaoTest extends \PHPUnit_Framework_TestCase
|
||||||
$output = $router->handle('GET', '/test');
|
$output = $router->handle('GET', '/test');
|
||||||
$this->assertEquals('ok', $output);
|
$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';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue