From 4efa10c3e0200a152fbc04995f35e887b1e28b88 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 12 Sep 2020 22:12:00 +0200 Subject: [PATCH] impl. tests for routing --- Router/SocketRouter.php | 16 ------- Router/WebRouter.php | 22 ++++------ tests/Router/WebRouterTest.php | 78 ++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 33 deletions(-) diff --git a/Router/SocketRouter.php b/Router/SocketRouter.php index bfcc1b163..22c0a41ba 100644 --- a/Router/SocketRouter.php +++ b/Router/SocketRouter.php @@ -135,22 +135,6 @@ final class SocketRouter implements RouterInterface } } } - - $temp = ['dest' => $d['dest']]; - - // fill data - if (isset($d['data'])) { - $data = []; - foreach ($d['data'] as $name => $destination) { - if (isset($data[$name])) { - $data[$destination] = $data[$name]; - } - } - - $temp['data'] = $data; - } - - $bound[] = $temp; } } diff --git a/Router/WebRouter.php b/Router/WebRouter.php index 81b726757..7934c76e8 100644 --- a/Router/WebRouter.php +++ b/Router/WebRouter.php @@ -76,21 +76,25 @@ final class WebRouter implements RouterInterface * @param mixed $destination Destination e.g. Module:function string or callback * @param int $verb Request verb * @param bool $csrf Is CSRF token required + * @param array $validation Validation patterns + * @param string $dataPattern Data patterns * * @return void * * @since 1.0.0 */ - public function add(string $route, $destination, int $verb = RouteVerb::GET, bool $csrf = false) : void + public function add(string $route, $destination, int $verb = RouteVerb::GET, bool $csrf = false, array $validation = [], string $dataPattern = '') : void { if (!isset($this->routes[$route])) { $this->routes[$route] = []; } $this->routes[$route][] = [ - 'dest' => $destination, - 'verb' => $verb, - 'csrf' => $csrf, + 'dest' => $destination, + 'verb' => $verb, + 'csrf' => $csrf, + 'validation' => empty($validation) ? null : $validation, + 'pattern' => empty($dataPattern) ? null : $dataPattern, ]; } @@ -147,10 +151,6 @@ final class WebRouter implements RouterInterface } // if validation check is invalid - /** - * @todo Orange-Management/phpOMS#251 - * [WebRouter] Implement test to validate the provided data - */ if (isset($d['validation'])) { foreach ($d['validation'] as $name => $pattern) { if (!isset($data[$name]) || \preg_match($pattern, $data[$name]) !== 1) { @@ -162,12 +162,8 @@ final class WebRouter implements RouterInterface $temp = ['dest' => $d['dest']]; // fill data - /** - * @todo Orange-Management/phpOMS#252 - * [WebRouter] Implement test for defining data from route - */ if (isset($d['pattern'])) { - \preg_match($d['pattern'], $route, $matches); + \preg_match($d['pattern'], $uri, $matches); $temp['data'] = $matches; } diff --git a/tests/Router/WebRouterTest.php b/tests/Router/WebRouterTest.php index 266c12b47..a0cf14083 100644 --- a/tests/Router/WebRouterTest.php +++ b/tests/Router/WebRouterTest.php @@ -199,7 +199,7 @@ class WebRouterTest extends \PHPUnit\Framework\TestCase */ public function testWithValidPermissions() : void { - if (!Autoloader::exists('\Modules\Admin\Controller')) { + if (!Autoloader::exists('\Modules\Admin\Controller\Controller')) { self::markTestSkipped(); } @@ -239,7 +239,7 @@ class WebRouterTest extends \PHPUnit\Framework\TestCase */ public function testWithInvalidPermissions() : void { - if (!Autoloader::exists('\Modules\Admin\Controller')) { + if (!Autoloader::exists('\Modules\Admin\Controller\Controller')) { self::markTestSkipped(); } @@ -296,13 +296,83 @@ class WebRouterTest extends \PHPUnit\Framework\TestCase ); } + /** + * @testdox A data validation pattern validates matches correctly + * @covers phpOMS\Router\WebRouter + * @group framework + */ public function testDataValidation() : void { - self::markTestIncomplete(); + $this->router->add( + '^.*/backends/admin/settings/general.*$', + 'Controller:test', + RouteVerb::GET | RouteVerb::SET, + false, + ['test_pattern' => '/^[a-z]*$/'] + ); + + self::assertEquals( + [['dest' => 'Controller:test']], + $this->router->route( + (new HttpRequest( + new HttpUri('http://test.com/backends/admin/settings/general/something?test') + ))->getUri()->getRoute(), null, RouteVerb::ANY, null, null, null, ['test_pattern' => 'abcdef']) + ); } + /** + * @testdox A data validation pattern invalidates missmatches + * @covers phpOMS\Router\WebRouter + * @group framework + */ + public function testInvalidDataValidation() : void + { + $this->router->add( + '^.*/backends/admin/settings/general.*$', + 'Controller:test', + RouteVerb::GET | RouteVerb::SET, + false, + ['test_pattern' => '/^[a-z]*$/'] + ); + + self::assertNotEquals( + [['dest' => 'Controller:test']], + $this->router->route( + (new HttpRequest( + new HttpUri('http://test.com/backends/admin/settings/general/something?test') + ))->getUri()->getRoute(), null, RouteVerb::ANY, null, null, null, ['test_pattern' => '123']) + ); + } + + /** + * @testdox A uri can be used for data population + * @covers phpOMS\Router\WebRouter + * @group framework + */ public function testDataFromPattern() : void { - self::markTestIncomplete(); + $this->router->add( + '^.*/backends/admin.*$', + 'Controller:test', + RouteVerb::GET | RouteVerb::SET, + false, + [], + '/^.*?(something)=(\d*).*?$/' + ); + + self::assertEquals( + [[ + 'dest' => 'Controller:test', + 'data' => [ + '/backends/admin?something=123&sd=asdf', + 'something', + '123', + ] + ]], + $this->router->route( + (new HttpRequest( + new HttpUri('http://test.com/backends/admin?something=123&sd=asdf') + ))->getUri()->getRoute(), null, RouteVerb::ANY) + ); } }