Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 9b98581

Browse files
committed
Update routes and their tests
Add couple more tests and change the way routes are setup in data provider. Any code executed in data provider IS NOT counted towards code coverage
1 parent 65cb815 commit 9b98581

17 files changed

+1339
-438
lines changed

src/Route/Hostname.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ protected function buildHost(array $parts, array $mergedParams, $isOptional) : s
282282

283283
public function partialMatch(Request $request, int $pathOffset = 0, array $options = []): PartialRouteResult
284284
{
285+
if ($pathOffset < 0) {
286+
throw new Exception\InvalidArgumentException('Route path offset cannot be negative');
287+
}
285288
$uri = $request->getUri();
286289
$host = $uri->getHost();
287290

@@ -318,7 +321,7 @@ public function assemble(array $params = [], array $options = []) : UriInterface
318321
{
319322
$uri = $options['uri'] ?? new Uri();
320323
if (! $uri instanceof UriInterface) {
321-
throw new Exception\DomainException(\sprintf(
324+
throw new Exception\InvalidArgumentException(\sprintf(
322325
'Route assemble option \'uri\' must be instance of %s, got %s',
323326
UriInterface::class,
324327
(\is_object($uri) ? \get_class($uri) : \gettype($uri))

src/Route/Literal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static function factory($options = []) : RouteInterface
8585
public function partialMatch(Request $request, int $pathOffset = 0, array $options = []) : PartialRouteResult
8686
{
8787
if ($pathOffset < 0) {
88-
throw new Exception\DomainException('Route path offset cannot be negative');
88+
throw new Exception\InvalidArgumentException('Route path offset cannot be negative');
8989
}
9090
$uri = $request->getUri();
9191
$path = $uri->getPath();
@@ -121,7 +121,7 @@ public function assemble(array $params = [], array $options = []) : UriInterface
121121
}
122122
$uri = $options['uri'];
123123
if (! $uri instanceof UriInterface) {
124-
throw new Exception\DomainException(\sprintf(
124+
throw new Exception\InvalidArgumentException(\sprintf(
125125
'Route assemble option \'uri\' must be instance of %s, got %s',
126126
UriInterface::class,
127127
(is_object($uri) ? get_class($uri) : gettype($uri))

src/Route/Method.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public static function factory($options = []) : RouteInterface
8383

8484
public function partialMatch(Request $request, int $pathOffset = 0, array $options = []) : PartialRouteResult
8585
{
86+
if ($pathOffset < 0) {
87+
throw new Exception\InvalidArgumentException('Route path offset cannot be negative');
88+
}
8689
$requestVerb = strtoupper($request->getMethod());
8790
$matchVerbs = explode(',', strtoupper($this->verb));
8891
$matchVerbs = array_map('trim', $matchVerbs);
@@ -114,7 +117,7 @@ public function assemble(array $params = [], array $options = []) : UriInterface
114117
}
115118
$uri = $options['uri'];
116119
if (! $uri instanceof UriInterface) {
117-
throw new Exception\DomainException(\sprintf(
120+
throw new Exception\InvalidArgumentException(\sprintf(
118121
'Route assemble option \'uri\' must be instance of %s, got %s',
119122
UriInterface::class,
120123
(\is_object($uri) ? \get_class($uri) : \gettype($uri))

src/Route/PartialRouteTrait.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Zend\Router\Route;
1010

1111
use Psr\Http\Message\ServerRequestInterface as Request;
12+
use Zend\Router\Exception\InvalidArgumentException;
1213
use Zend\Router\PartialRouteInterface;
1314
use Zend\Router\RouteResult;
1415

@@ -24,9 +25,15 @@ trait PartialRouteTrait
2425
*/
2526
public function match(Request $request, int $pathOffset = 0, array $options = []) : RouteResult
2627
{
28+
if ($pathOffset < 0) {
29+
throw new InvalidArgumentException('Route path offset cannot be negative');
30+
}
2731
/** @var PartialRouteInterface $this */
2832
$result = $this->partialMatch($request, $pathOffset, $options);
29-
if ($result->isSuccess() && $result->isFullPathMatch($request->getUri())) {
33+
if (! $result->isFullPathMatch($request->getUri())) {
34+
return RouteResult::fromRouteFailure();
35+
}
36+
if ($result->isSuccess()) {
3037
return RouteResult::fromRouteMatch($result->getMatchedParams(), $result->getMatchedRouteName())
3138
->withAllowedMethods($result->getAllowedMethods());
3239
}

src/Route/Regex.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public static function factory($options = []) : RouteInterface
105105

106106
public function partialMatch(Request $request, int $pathOffset = 0, array $options = []): PartialRouteResult
107107
{
108+
if ($pathOffset < 0) {
109+
throw new Exception\InvalidArgumentException('Route path offset cannot be negative');
110+
}
108111
$uri = $request->getUri();
109112
$path = $uri->getPath();
110113

@@ -161,7 +164,7 @@ public function assemble(array $params = [], array $options = []) : UriInterface
161164
}
162165
$uri = $options['uri'];
163166
if (! $uri instanceof UriInterface) {
164-
throw new Exception\DomainException(\sprintf(
167+
throw new Exception\InvalidArgumentException(\sprintf(
165168
'Route assemble option \'uri\' must be instance of %s, got %s',
166169
UriInterface::class,
167170
(is_object($uri) ? get_class($uri) : gettype($uri))

src/Route/Scheme.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public static function factory($options = []) : RouteInterface
8383

8484
public function partialMatch(Request $request, int $pathOffset = 0, array $options = []): PartialRouteResult
8585
{
86+
if ($pathOffset < 0) {
87+
throw new Exception\InvalidArgumentException('Route path offset cannot be negative');
88+
}
8689
$uri = $request->getUri();
8790
$scheme = $uri->getScheme();
8891

@@ -110,13 +113,12 @@ public function assemble(array $params = [], array $options = []) : UriInterface
110113
{
111114
$uri = $options['uri'] ?? new Uri();
112115
if (! $uri instanceof UriInterface) {
113-
throw new Exception\DomainException(\sprintf(
116+
throw new Exception\InvalidArgumentException(\sprintf(
114117
'Route assemble option \'uri\' must be instance of %s, got %s',
115118
UriInterface::class,
116119
(\is_object($uri) ? \get_class($uri) : \gettype($uri))
117120
));
118121
}
119-
$this->assembledParams = [];
120122

121123
return $uri->withScheme($this->scheme);
122124
}

src/Route/Segment.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ protected function buildPath(array $parts, array $mergedParams, $isOptional, $ha
356356

357357
public function partialMatch(Request $request, int $pathOffset = 0, array $options = []): PartialRouteResult
358358
{
359+
if ($pathOffset < 0) {
360+
throw new Exception\InvalidArgumentException('Route path offset cannot be negative');
361+
}
359362
$path = $request->getUri()->getPath();
360363

361364
$regex = $this->regex;
@@ -374,11 +377,7 @@ public function partialMatch(Request $request, int $pathOffset = 0, array $optio
374377
}
375378
}
376379

377-
if ($pathOffset !== null) {
378-
$result = preg_match('(\G' . $regex . ')', $path, $matches, 0, $pathOffset);
379-
} else {
380-
$result = preg_match('(^' . $regex . '$)', $path, $matches);
381-
}
380+
$result = preg_match('(\G' . $regex . ')', $path, $matches, 0, $pathOffset);
382381

383382
if (! $result) {
384383
return PartialRouteResult::fromRouteFailure();
@@ -424,7 +423,7 @@ public function assemble(array $params = [], array $options = []) : UriInterface
424423
}
425424
$uri = $options['uri'];
426425
if (! $uri instanceof UriInterface) {
427-
throw new Exception\DomainException(\sprintf(
426+
throw new Exception\InvalidArgumentException(\sprintf(
428427
'Route assemble option \'uri\' must be instance of %s, got %s',
429428
UriInterface::class,
430429
(is_object($uri) ? get_class($uri) : gettype($uri))
@@ -452,12 +451,11 @@ public function getLastAssembledParams() : array
452451
*/
453452
protected function encode(string $value)
454453
{
455-
$key = (string) $value;
456-
if (! isset(static::$cacheEncode[$key])) {
457-
static::$cacheEncode[$key] = rawurlencode($value);
458-
static::$cacheEncode[$key] = strtr(static::$cacheEncode[$key], static::$urlencodeCorrectionMap);
454+
if (! isset(static::$cacheEncode[$value])) {
455+
static::$cacheEncode[$value] = rawurlencode($value);
456+
static::$cacheEncode[$value] = strtr(static::$cacheEncode[$value], static::$urlencodeCorrectionMap);
459457
}
460-
return static::$cacheEncode[$key];
458+
return static::$cacheEncode[$value];
461459
}
462460

463461
/**
@@ -466,7 +464,7 @@ protected function encode(string $value)
466464
* @param string $value
467465
* @return string
468466
*/
469-
protected function decode($value)
467+
protected function decode(string $value)
470468
{
471469
return rawurldecode($value);
472470
}

src/TreeRouteStack.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public function addRoute($name, $route, $priority = null) : void
104104
protected function routeFromArray($specs) : RouteInterface
105105
{
106106
if (is_string($specs)) {
107-
if (null === ($route = $this->getPrototype($specs))) {
107+
$route = $this->getPrototype($specs);
108+
if (null === $route) {
108109
throw new Exception\RuntimeException(sprintf('Could not find prototype with name %s', $specs));
109110
}
110111

@@ -159,7 +160,7 @@ protected function routeFromArray($specs) : RouteInterface
159160
/**
160161
* Add multiple prototypes at once.
161162
*
162-
* @param Traversable $routes
163+
* @param array|Traversable $routes
163164
* @return void
164165
* @throws Exception\InvalidArgumentException
165166
*/

0 commit comments

Comments
 (0)