diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0ebad66..8b4c4f1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,17 +1,13 @@ - - - - ./test - - - - - - ./src - - - \ No newline at end of file + + + + ./src + + + + + ./test + + + diff --git a/src/Route.php b/src/Route.php index f7ea575..312cf3d 100644 --- a/src/Route.php +++ b/src/Route.php @@ -21,12 +21,12 @@ class Route /** * @var string */ - private $request; + private string $request; /** * @var string */ - private $controller; + private string $controller; /** * @var array|string @@ -36,31 +36,22 @@ class Route /** * @var string */ - private $type; + private string $type; /** * @var array */ - private $requestParams = []; - - /** - * @var - */ - private $spec; + private array $requestParams = []; /** * @var string */ - private $matchedAction; + private string $spec; /** - * @var array + * @var string */ - private $allowedTypes - = [ - self::TYPE_LITERAL, - self::TYPE_REGEXP, - ]; + private string $matchedAction; /** * Route constructor. @@ -81,26 +72,11 @@ public function __construct( string $spec = '' ) { $this->request = $request; - if (!in_array($type, $this->allowedTypes)) { - throw new Exception\InvalidArgumentException( - sprintf( - 'Route type should be one of: %s', - implode(', ', $this->allowedTypes) - ) - ); - } - $this->type = $type; $this->controller = $controller; $this->actionList = $actionList; $this->requestParams = $requestParams; $this->spec = $spec; - - if (!$this->isLiteral() && !$this->spec) { - throw new Exception\InvalidArgumentException( - 'The regexp route must be provided with `spec` for assembling requests' - ); - } } /** @@ -111,6 +87,18 @@ public function getRequest(): string return $this->request; } + /** + * @param string $request + * + * @return $this + */ + public function setRequest(string $request): self + { + $this->request = $request; + + return $this; + } + /** * @return string */ @@ -119,6 +107,18 @@ public function getController(): string return $this->controller; } + /** + * @param string $controller + * + * @return $this + */ + public function setController(string $controller): self + { + $this->controller = $controller; + + return $this; + } + /** * @return array|string */ @@ -127,6 +127,38 @@ public function getActionList() return $this->actionList; } + /** + * @param array|string $actionList + * + * @return $this + */ + public function setActionList($actionList): self + { + $this->actionList = $actionList; + + return $this; + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @param string $type + * + * @return $this + */ + public function setType(string $type): self + { + $this->type = $type; + + return $this; + } + /** * @return bool */ @@ -143,6 +175,18 @@ public function getRequestParams(): array return $this->requestParams; } + /** + * @param array $requestParams + * + * @return $this + */ + public function setRequestParams(array $requestParams): self + { + $this->requestParams = $requestParams; + + return $this; + } + /** * @return string */ @@ -151,6 +195,18 @@ public function getSpec(): string return $this->spec; } + /** + * @param string $spec + * + * @return $this + */ + public function setSpec(string $spec): self + { + $this->spec = $spec; + + return $this; + } + /** * @return string */ @@ -160,11 +216,15 @@ public function getMatchedAction(): string } /** - * @param string $action + * @param string $action + * + * @return $this */ - public function setMatchedAction(string $action) + public function setMatchedAction(string $action): self { $this->matchedAction = $action; + + return $this; } } diff --git a/src/Router.php b/src/Router.php index 627727a..4ba6bba 100644 --- a/src/Router.php +++ b/src/Router.php @@ -19,37 +19,37 @@ class Router /** * @var Request */ - private $request; + private Request $request; /** - * @var Route + * @var Route|null */ - private $defaultRoute; + private ?Route $defaultRoute = null; /** * @var Route[] */ - private $allRoutes = []; + private array $allRoutes = []; /** * @var array */ - private $assembleRoutesMap = []; + private array $assembleRoutesMap = []; /** * @var string */ - private $assembleParamDivider = '%'; + private string $assembleParamDivider = '%'; /** * @var array */ - private $literalRoutesMap = []; + private array $literalRoutesMap = []; /** * @var array */ - private $regexpRoutesMap = []; + private array $regexpRoutesMap = []; /** * Router constructor. diff --git a/test/RouteTest.php b/test/RouteTest.php index 74b96ba..bad90b4 100644 --- a/test/RouteTest.php +++ b/test/RouteTest.php @@ -12,27 +12,11 @@ namespace TinyTest\Router; use PHPUnit\Framework\TestCase; -use Tiny\Router\Exception\InvalidArgumentException; use Tiny\Router\Route; class RouteTest extends TestCase { - public function testIncorrectType() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage( - 'Route type should be one of: literal, regexp' - ); - - new Route( - '/test', - 'TestController', - [], - 'test' - ); - } - public function testIsLiteralMethod() { $instance = new Route( @@ -44,7 +28,7 @@ public function testIsLiteralMethod() $this->assertTrue($instance->isLiteral()); } - public function testGetters() + public function testSettersAndGetters() { $request = '|^/test/(?P\d+)$|i'; $controller = 'TestController'; @@ -56,38 +40,28 @@ public function testGetters() ]; $spec = '/test/%id%'; $matchedAction = 'test'; + $instance = new Route( - $request, - $controller, - $actionList, - Route::TYPE_REGEXP, - $requestParams, - $spec + '', + '', + '' ); - $instance->setMatchedAction($matchedAction); + $instance->setController($controller) + ->setActionList($actionList) + ->setMatchedAction($matchedAction) + ->setRequest($request) + ->setSpec($spec) + ->setType(Route::TYPE_REGEXP) + ->setRequestParams($requestParams); + $this->assertEquals($request, $instance->getRequest()); $this->assertEquals($controller, $instance->getController()); $this->assertEquals($actionList, $instance->getActionList()); $this->assertEquals($requestParams, $instance->getRequestParams()); $this->assertEquals($spec, $instance->getSpec()); $this->assertEquals($matchedAction, $instance->getMatchedAction()); - } - - public function testRegexRouteWithoutSpec() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage( - 'The regexp route must be provided with `spec` for assembling requests' - ); - - new Route( - '|^/test/(?P\d+)$|i', - 'TestController', - 'test', - Route::TYPE_REGEXP, - [] - ); + $this->assertEquals(Route::TYPE_REGEXP, $instance->getType()); } }