Skip to content

Commit

Permalink
allowed chars in route param updated, tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
s3b4stian committed Aug 3, 2019
1 parent 119eaea commit 831d30b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/Linna/Router/Exception/RedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Linna/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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._-]++',
];

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/Router/Exception/RedirectExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Linna Framework.
*
* @author Sebastian Rapetti <[email protected]>
* @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());
}
}
}
75 changes: 74 additions & 1 deletion tests/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
])
]));

Expand Down Expand Up @@ -127,6 +134,9 @@ public function WrongArgumentsForRouterProvider(): array
*
* @dataProvider WrongArgumentsForRouterProvider
*
* @param mixed $routes
* @param mixed $options
*
* @return void
*/
public function testNewRouterInstanceWithWrongArguments($routes, $options): void
Expand Down Expand Up @@ -161,6 +171,9 @@ public function WrongArgumentsForValidateRouteProvider(): array
*
* @dataProvider WrongArgumentsForValidateRouteProvider
*
* @param mixed $url
* @param mixed $method
*
* @return void
*/
public function testValidateRouteWithWrongArguments($url, $method): void
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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']);
}
}

0 comments on commit 831d30b

Please sign in to comment.