Skip to content

Commit

Permalink
add getters in the Route class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Ermashev committed Aug 13, 2020
1 parent 85cb10d commit f176dd0
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 97 deletions.
28 changes: 12 additions & 16 deletions phpunit.xml.dist
@@ -1,17 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="tiny-router Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="tiny-router Test Suite">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
126 changes: 93 additions & 33 deletions src/Route.php
Expand Up @@ -21,12 +21,12 @@ class Route
/**
* @var string
*/
private $request;
private string $request;

/**
* @var string
*/
private $controller;
private string $controller;

/**
* @var array|string
Expand All @@ -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.
Expand All @@ -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'
);
}
}

/**
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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;
}

}
16 changes: 8 additions & 8 deletions src/Router.php
Expand Up @@ -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.
Expand Down
54 changes: 14 additions & 40 deletions test/RouteTest.php
Expand Up @@ -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(
Expand All @@ -44,7 +28,7 @@ public function testIsLiteralMethod()
$this->assertTrue($instance->isLiteral());
}

public function testGetters()
public function testSettersAndGetters()
{
$request = '|^/test/(?P<id>\d+)$|i';
$controller = 'TestController';
Expand All @@ -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<id>\d+)$|i',
'TestController',
'test',
Route::TYPE_REGEXP,
[]
);
$this->assertEquals(Route::TYPE_REGEXP, $instance->getType());
}

}

0 comments on commit f176dd0

Please sign in to comment.