impl. tests for routing

This commit is contained in:
Dennis Eichhorn 2020-09-12 22:12:00 +02:00
parent bd39681b54
commit 4efa10c3e0
3 changed files with 83 additions and 33 deletions

View File

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

View File

@ -76,12 +76,14 @@ 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] = [];
@ -91,6 +93,8 @@ final class WebRouter implements RouterInterface
'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;
}

View File

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