diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-05-15 18:31:47 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-05-15 18:31:47 -0400 |
commit | 67b836164997527b91452b19adbcb8aa3c5decf1 (patch) | |
tree | b5876d311912e97b0592c7e208639f7b52813a75 /tests/units/Core/Http | |
parent | 108e867605dbc7ece4cbcbecc89a674e9c154a9b (diff) |
Refactoring: added controlled middleware and changed response class
Diffstat (limited to 'tests/units/Core/Http')
-rw-r--r-- | tests/units/Core/Http/RouteTest.php | 3 | ||||
-rw-r--r-- | tests/units/Core/Http/RouterTest.php | 326 |
2 files changed, 135 insertions, 194 deletions
diff --git a/tests/units/Core/Http/RouteTest.php b/tests/units/Core/Http/RouteTest.php index 5e44ad00..86920970 100644 --- a/tests/units/Core/Http/RouteTest.php +++ b/tests/units/Core/Http/RouteTest.php @@ -3,6 +3,7 @@ require_once __DIR__.'/../../Base.php'; use Kanboard\Core\Http\Route; +use Kanboard\Core\Http\Router; class RouteTest extends Base { @@ -34,7 +35,7 @@ class RouteTest extends Base ); $this->assertEquals( - array('controller' => 'app', 'action' => 'index', 'plugin' => ''), + array('controller' => Router::DEFAULT_CONTROLLER, 'action' => Router::DEFAULT_METHOD, 'plugin' => ''), $route->findRoute('/notfound') ); diff --git a/tests/units/Core/Http/RouterTest.php b/tests/units/Core/Http/RouterTest.php index 75a3ba4f..6f124d33 100644 --- a/tests/units/Core/Http/RouterTest.php +++ b/tests/units/Core/Http/RouterTest.php @@ -1,207 +1,147 @@ <?php -namespace Kanboard\Controller { +require_once __DIR__.'/../../Base.php'; - class FakeController { - public function beforeAction() {} - public function myAction() {} +use Kanboard\Core\Http\Route; +use Kanboard\Core\Http\Router; +use Kanboard\Core\Http\Request; + +class RouterTest extends Base +{ + public function testSanitize() + { + $dispatcher = new Router($this->container); + + $this->assertEquals('PloP', $dispatcher->sanitize('PloP', 'default')); + $this->assertEquals('default', $dispatcher->sanitize('', 'default')); + $this->assertEquals('default', $dispatcher->sanitize('123-AB', 'default')); + $this->assertEquals('default', $dispatcher->sanitize('R&D', 'default')); + $this->assertEquals('Test123', $dispatcher->sanitize('Test123', 'default')); + $this->assertEquals('Test_123', $dispatcher->sanitize('Test_123', 'default')); + $this->assertEquals('userImport', $dispatcher->sanitize('userImport', 'default')); } -} -namespace Kanboard\Plugin\Myplugin\Controller { + public function testGetPathWithFolder() + { + $router = new Router($this->container); + $this->container['request'] = new Request($this->container, array('PHP_SELF' => '/index.php', 'REQUEST_URI' => '/a/b/c', 'REQUEST_METHOD' => 'GET')); + $this->assertEquals('a/b/c', $router->getPath()); + } - class FakeController { - public function beforeAction() {} - public function myAction() {} + public function testGetPathWithQueryString() + { + $router = new Router($this->container); + $this->container['request'] = new Request($this->container, array('PHP_SELF' => '/index.php', 'REQUEST_URI' => '/a/b/something?test=a', 'QUERY_STRING' => 'test=a', 'REQUEST_METHOD' => 'GET')); + $this->assertEquals('a/b/something', $router->getPath()); + } + + public function testGetPathWithSubFolderAndQueryString() + { + $router = new Router($this->container); + $this->container['request'] = new Request($this->container, array('PHP_SELF' => '/a/index.php', 'REQUEST_URI' => '/a/b/something?test=a', 'QUERY_STRING' => 'test=a', 'REQUEST_METHOD' => 'GET')); + $this->assertEquals('b/something', $router->getPath()); } -} -namespace { + public function testDispatcherWithNoUrlRewrite() + { + $this->container['request'] = new Request($this->container, array( + 'PHP_SELF' => '/kanboard/index.php', + 'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myAction&myvar=value1', + 'QUERY_STRING' => 'controller=FakeController&action=myAction&myvar=value1', + 'REQUEST_METHOD' => 'GET' + ), + array( + 'controller' => 'FakeController', + 'action' => 'myAction', + 'myvar' => 'value1', + ) + ); + + $dispatcher = new Router($this->container); + $dispatcher->dispatch(); + + $this->assertEquals('FakeController', $dispatcher->getController()); + $this->assertEquals('myAction', $dispatcher->getAction()); + $this->assertEquals('', $dispatcher->getPlugin()); + $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); + } - require_once __DIR__.'/../../Base.php'; + public function testDispatcherWithNoUrlRewriteAndPlugin() + { + $this->container['request'] = new Request($this->container, array( + 'PHP_SELF' => '/kanboard/index.php', + 'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myAction&myvar=value1&plugin=myplugin', + 'QUERY_STRING' => 'controller=FakeController&action=myAction&myvar=value1&plugin=myplugin', + 'REQUEST_METHOD' => 'GET' + ), + array( + 'controller' => 'FakeController', + 'action' => 'myAction', + 'myvar' => 'value1', + 'plugin' => 'myplugin', + ) + ); + + $dispatcher = new Router($this->container); + $dispatcher->dispatch(); + + $this->assertEquals('FakeController', $dispatcher->getController()); + $this->assertEquals('myAction', $dispatcher->getAction()); + $this->assertEquals('Myplugin', $dispatcher->getPlugin()); + $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); + } - use Kanboard\Core\Helper; - use Kanboard\Core\Http\Route; - use Kanboard\Core\Http\Router; - use Kanboard\Core\Http\Request; + public function testDispatcherWithUrlRewrite() + { + $this->container['request'] = new Request($this->container, array( + 'PHP_SELF' => '/kanboard/index.php', + 'REQUEST_URI' => '/kanboard/my/route/123?myvar=value1', + 'QUERY_STRING' => 'myvar=value1', + 'REQUEST_METHOD' => 'GET' + ), + array( + 'myvar' => 'value1', + ) + ); + + $this->container['route'] = new Route($this->container); + $this->container['route']->enable(); + $this->container['route']->addRoute('/my/route/:param', 'FakeController', 'myAction'); + + $dispatcher = new Router($this->container); + $dispatcher->dispatch(); + + $this->assertEquals('FakeController', $dispatcher->getController()); + $this->assertEquals('myAction', $dispatcher->getAction()); + $this->assertEquals('', $dispatcher->getPlugin()); + $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); + $this->assertEquals('123', $this->container['request']->getStringParam('param')); + } - class RouterTest extends Base + public function testDispatcherWithUrlRewriteWithPlugin() { - public function testSanitize() - { - $dispatcher = new Router($this->container); - - $this->assertEquals('PloP', $dispatcher->sanitize('PloP', 'default')); - $this->assertEquals('default', $dispatcher->sanitize('', 'default')); - $this->assertEquals('default', $dispatcher->sanitize('123-AB', 'default')); - $this->assertEquals('default', $dispatcher->sanitize('R&D', 'default')); - $this->assertEquals('Test123', $dispatcher->sanitize('Test123', 'default')); - $this->assertEquals('Test_123', $dispatcher->sanitize('Test_123', 'default')); - $this->assertEquals('userImport', $dispatcher->sanitize('userImport', 'default')); - } - - public function testGetPathWithFolder() - { - $router = new Router($this->container); - $this->container['request'] = new Request($this->container, array('PHP_SELF' => '/index.php', 'REQUEST_URI' => '/a/b/c', 'REQUEST_METHOD' => 'GET')); - $this->assertEquals('a/b/c', $router->getPath()); - } - - public function testGetPathWithQueryString() - { - $router = new Router($this->container); - $this->container['request'] = new Request($this->container, array('PHP_SELF' => '/index.php', 'REQUEST_URI' => '/a/b/something?test=a', 'QUERY_STRING' => 'test=a', 'REQUEST_METHOD' => 'GET')); - $this->assertEquals('a/b/something', $router->getPath()); - } - - public function testGetPathWithSubFolderAndQueryString() - { - $router = new Router($this->container); - $this->container['request'] = new Request($this->container, array('PHP_SELF' => '/a/index.php', 'REQUEST_URI' => '/a/b/something?test=a', 'QUERY_STRING' => 'test=a', 'REQUEST_METHOD' => 'GET')); - $this->assertEquals('b/something', $router->getPath()); - } - - public function testDispatcherWithControllerNotFound() - { - $this->container['request'] = new Request($this->container, array( - 'PHP_SELF' => '/kanboard/index.php', - 'REQUEST_URI' => '/kanboard/?controller=FakeControllerNotFound&action=myAction&myvar=value1', - 'QUERY_STRING' => 'controller=FakeControllerNotFound&action=myAction&myvar=value1', - 'REQUEST_METHOD' => 'GET' - ), - array( - 'controller' => 'FakeControllerNotFound', - 'action' => 'myAction', - 'myvar' => 'value1', - ) - ); - - $this->setExpectedException('RuntimeException', 'Controller not found'); - - $dispatcher = new Router($this->container); - $dispatcher->dispatch(); - } - - public function testDispatcherWithActionNotFound() - { - $this->container['request'] = new Request($this->container, array( - 'PHP_SELF' => '/kanboard/index.php', - 'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myActionNotFound&myvar=value1', - 'QUERY_STRING' => 'controller=FakeController&action=myActionNotFound&myvar=value1', - 'REQUEST_METHOD' => 'GET' - ), - array( - 'controller' => 'FakeController', - 'action' => 'myActionNotFound', - 'myvar' => 'value1', - ) - ); - - $this->setExpectedException('RuntimeException', 'Action not implemented'); - - $dispatcher = new Router($this->container); - $dispatcher->dispatch(); - } - - public function testDispatcherWithNoUrlRewrite() - { - $this->container['request'] = new Request($this->container, array( - 'PHP_SELF' => '/kanboard/index.php', - 'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myAction&myvar=value1', - 'QUERY_STRING' => 'controller=FakeController&action=myAction&myvar=value1', - 'REQUEST_METHOD' => 'GET' - ), - array( - 'controller' => 'FakeController', - 'action' => 'myAction', - 'myvar' => 'value1', - ) - ); - - $dispatcher = new Router($this->container); - $this->assertInstanceOf('\Kanboard\Controller\FakeController', $dispatcher->dispatch()); - $this->assertEquals('FakeController', $dispatcher->getController()); - $this->assertEquals('myAction', $dispatcher->getAction()); - $this->assertEquals('', $dispatcher->getPlugin()); - $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); - } - - public function testDispatcherWithNoUrlRewriteAndPlugin() - { - $this->container['request'] = new Request($this->container, array( - 'PHP_SELF' => '/kanboard/index.php', - 'REQUEST_URI' => '/kanboard/?controller=FakeController&action=myAction&myvar=value1&plugin=myplugin', - 'QUERY_STRING' => 'controller=FakeController&action=myAction&myvar=value1&plugin=myplugin', - 'REQUEST_METHOD' => 'GET' - ), - array( - 'controller' => 'FakeController', - 'action' => 'myAction', - 'myvar' => 'value1', - 'plugin' => 'myplugin', - ) - ); - - $dispatcher = new Router($this->container); - $this->assertInstanceOf('\Kanboard\Plugin\Myplugin\Controller\FakeController', $dispatcher->dispatch()); - $this->assertEquals('FakeController', $dispatcher->getController()); - $this->assertEquals('myAction', $dispatcher->getAction()); - $this->assertEquals('Myplugin', $dispatcher->getPlugin()); - $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); - } - - public function testDispatcherWithUrlRewrite() - { - $this->container['request'] = new Request($this->container, array( - 'PHP_SELF' => '/kanboard/index.php', - 'REQUEST_URI' => '/kanboard/my/route/123?myvar=value1', - 'QUERY_STRING' => 'myvar=value1', - 'REQUEST_METHOD' => 'GET' - ), - array( - 'myvar' => 'value1', - ) - ); - - $this->container['route'] = new Route($this->container); - $this->container['route']->enable(); - $dispatcher = new Router($this->container); - - $this->container['route']->addRoute('/my/route/:param', 'FakeController', 'myAction'); - - $this->assertInstanceOf('\Kanboard\Controller\FakeController', $dispatcher->dispatch()); - $this->assertEquals('FakeController', $dispatcher->getController()); - $this->assertEquals('myAction', $dispatcher->getAction()); - $this->assertEquals('', $dispatcher->getPlugin()); - $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); - $this->assertEquals('123', $this->container['request']->getStringParam('param')); - } - - public function testDispatcherWithUrlRewriteWithPlugin() - { - $this->container['request'] = new Request($this->container, array( - 'PHP_SELF' => '/kanboard/index.php', - 'REQUEST_URI' => '/kanboard/my/plugin/route/123?myvar=value1', - 'QUERY_STRING' => 'myvar=value1', - 'REQUEST_METHOD' => 'GET' - ), - array( - 'myvar' => 'value1', - ) - ); - - $this->container['route'] = new Route($this->container); - $this->container['route']->enable(); - $dispatcher = new Router($this->container); - - $this->container['route']->addRoute('/my/plugin/route/:param', 'fakeController', 'myAction', 'Myplugin'); - - $this->assertInstanceOf('\Kanboard\Plugin\Myplugin\Controller\FakeController', $dispatcher->dispatch()); - $this->assertEquals('FakeController', $dispatcher->getController()); - $this->assertEquals('myAction', $dispatcher->getAction()); - $this->assertEquals('Myplugin', $dispatcher->getPlugin()); - $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); - $this->assertEquals('123', $this->container['request']->getStringParam('param')); - } + $this->container['request'] = new Request($this->container, array( + 'PHP_SELF' => '/kanboard/index.php', + 'REQUEST_URI' => '/kanboard/my/plugin/route/123?myvar=value1', + 'QUERY_STRING' => 'myvar=value1', + 'REQUEST_METHOD' => 'GET' + ), + array( + 'myvar' => 'value1', + ) + ); + + $this->container['route'] = new Route($this->container); + $this->container['route']->enable(); + $this->container['route']->addRoute('/my/plugin/route/:param', 'fakeController', 'myAction', 'Myplugin'); + + $dispatcher = new Router($this->container); + $dispatcher->dispatch(); + + $this->assertEquals('FakeController', $dispatcher->getController()); + $this->assertEquals('myAction', $dispatcher->getAction()); + $this->assertEquals('Myplugin', $dispatcher->getPlugin()); + $this->assertEquals('value1', $this->container['request']->getStringParam('myvar')); + $this->assertEquals('123', $this->container['request']->getStringParam('param')); } } |