diff --git a/CHANGELOG.md b/CHANGELOG.md index ffa0613f..c067de58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). #### Router * `Linna\Router\Route->allowed` property * `Linna\Router\Route->getAllowed()` method +* `Linna\Router\Router` chars accepted in route params now are 0-9 A-Z a-z ._- * `Linna\Router\Exception` namespace * `Linna\Router\Exception\RedirectException` exception diff --git a/src/Linna/Router/Exception/RedirectException.php b/src/Linna/Router/Exception/RedirectException.php index d6622f3e..13a82204 100644 --- a/src/Linna/Router/Exception/RedirectException.php +++ b/src/Linna/Router/Exception/RedirectException.php @@ -26,6 +26,18 @@ class RedirectException extends Exception */ private $path = ''; + /** + * Class Constructor. + * + * @param string $path Path on which to be redirected. + */ + public function __construct(string $path = '') + { + parent::__construct(); + + $this->path = $path; + } + /** * Set path. * diff --git a/src/Linna/Router/Router.php b/src/Linna/Router/Router.php index 3cfa57e9..20379d77 100644 --- a/src/Linna/Router/Router.php +++ b/src/Linna/Router/Router.php @@ -55,14 +55,14 @@ class Router * @var array List of regex for find parameter inside passed routes */ private $matchTypes = [ - '`\[[0-9A-Za-z]+\]`', + '`\[[0-9A-Za-z._-]+\]`', ]; /** * @var array List of regex for find type of parameter inside passed routes */ private $types = [ - '[0-9A-Za-z]++', + '[0-9A-Za-z._-]++', ]; /** diff --git a/tests/Router/Exception/RedirectExceptionTest.php b/tests/Router/Exception/RedirectExceptionTest.php new file mode 100644 index 00000000..ff3bb4f5 --- /dev/null +++ b/tests/Router/Exception/RedirectExceptionTest.php @@ -0,0 +1,52 @@ + + * @copyright (c) 2018, Sebastian Rapetti + * @license http://opensource.org/licenses/MIT MIT License + */ +declare(strict_types=1); + +namespace Linna\Tests; + +use Linna\Router\Exception\RedirectException; +use PHPUnit\Framework\TestCase; + +/** + * Redirect Exception test. + */ +class RedirectExceptionTest extends TestCase +{ + /** + * Test new instance. + * + * @return void + */ + public function testCreateInstance(): void + { + $this->assertInstanceOf(RedirectException::class, (new RedirectException())); + } + + /** + * Test Exception. + * + * @return void + */ + public function testException(): void + { + try { + throw new RedirectException('/new/route'); + } catch (RedirectException $e) { + $this->assertEquals('/new/route', $e->getPath()); + } + + try { + $exception = new RedirectException(); + $exception->setPath('/new/route'); + } catch (RedirectException $e) { + $this->assertEquals('/new/route', $e->getPath()); + } + } +} diff --git a/tests/Router/RouterTest.php b/tests/Router/RouterTest.php index c7b7d54e..46e131f1 100644 --- a/tests/Router/RouterTest.php +++ b/tests/Router/RouterTest.php @@ -79,6 +79,13 @@ public static function setUpBeforeClass(): void 'model' => 'UserModel', 'view' => 'UserView', 'controller' => 'UserController', + ]), + new Route([ + 'method' => 'GET', + 'url' => '/paramTest/[test]', + 'model' => 'ParamModel', + 'view' => 'ParamView', + 'controller' => 'ParamController', ]) ])); @@ -127,6 +134,9 @@ public function WrongArgumentsForRouterProvider(): array * * @dataProvider WrongArgumentsForRouterProvider * + * @param mixed $routes + * @param mixed $options + * * @return void */ public function testNewRouterInstanceWithWrongArguments($routes, $options): void @@ -161,6 +171,9 @@ public function WrongArgumentsForValidateRouteProvider(): array * * @dataProvider WrongArgumentsForValidateRouteProvider * + * @param mixed $url + * @param mixed $method + * * @return void */ public function testValidateRouteWithWrongArguments($url, $method): void @@ -202,6 +215,11 @@ public function routeProvider(): array * * @dataProvider routeProvider * + * @param string $url + * @param string $method + * @param array $returneRoute + * @param bool $validate + * * @return void */ public function testRoutes(string $url, string $method, array $returneRoute, bool $validate): void @@ -224,6 +242,11 @@ public function testRoutes(string $url, string $method, array $returneRoute, boo * * @dataProvider routeProvider * + * @param string $url + * @param string $method + * @param array $returneRoute + * @param bool $validate + * * @return void */ public function testRoutesWithOtherBasePath(string $url, string $method, array $returneRoute, bool $validate): void @@ -268,6 +291,9 @@ public function mapMethodRouteProvider(): array * * @dataProvider mapMethodRouteProvider * + * @param string $method + * @param string $url + * * @return void */ public function testMapInToRouterWithMapMethod(string $method, string $url): void @@ -300,6 +326,11 @@ public function fastMapRouteProvider(): array * * @dataProvider fastMapRouteProvider * + * @param string $method + * @param string $url + * @param string $func + * @param array $options + * * @return void */ public function testMapInToRouterWithFastMapRoute(string $method, string $url, string $func, array $options): void @@ -341,6 +372,11 @@ public function fastMapRouteProviderNoOptions(): array * * @dataProvider fastMapRouteProviderNoOptions * + * @param string $method + * @param string $url + * @param string $func + * @param array $options + * * @return void */ public function testMapInToRouterWithFastMapRouteWithoutOptions(string $method, string $url, string $func, array $options): void @@ -464,9 +500,13 @@ public function restRouteProvider(): array * * @dataProvider restRouteProvider * + * @param string $uri + * @param string $method + * @param string $action + * * @return void */ - public function testRESTRouting($uri, $method, $action): void + public function testRESTRouting(string $uri, string $method, string $action): void { $restRoutes = (new RouteCollection([ new Route([ @@ -599,4 +639,37 @@ public function testEqualRouteName(): void $this->assertFalse($router->validate('/user/bad', 'GET')); $this->assertInstanceOf(NullRoute::class, $router->getRoute()); } + + /** + * Route with param provider. + * + * @return array + */ + public function routeWithParamProvider(): array + { + return [ + ['/paramTest/az', 'az'], + ['/paramTest/aZ', 'aZ'], + ['/paramTest/a9', 'a9'], + ['/paramTest/a9.', 'a9.'], + ['/paramTest/a9-', 'a9-'], + ['/paramTest/._-', '._-'], + ]; + } + + /** + * Test allowed chars in route param. + * + * @dataProvider routeWithParamProvider + * + * @return void + */ + public function testAllowedCharsInRouteParam(string $uri, string $result): void + { + self::$router->validate($uri, 'GET'); + + $route = self::$router->getRoute(); + + $this->assertEquals($result, $route->param['test']); + } }