Clean up tests for routes

This commit is contained in:
PJ Dietz 2020-08-14 07:38:38 -04:00
parent fec5a4d405
commit 79c4799a7b
5 changed files with 121 additions and 54 deletions

View File

@ -21,7 +21,7 @@ class RegexRoute extends Route
* Examines a request target to see if it is a match for the route.
*
* @param string $requestTarget
* @return boolean
* @return bool
*/
public function matchesRequestTarget(string $requestTarget): bool
{

View File

@ -7,11 +7,6 @@ namespace WellRESTed\Routing\Route;
*/
class TemplateRoute extends Route
{
/** @var array */
private $pathVariables = [];
/** @var array */
private $explosions = [];
/** Regular expression matching a URI template variable (e.g., {id}) */
public const URI_TEMPLATE_EXPRESSION_RE = '/{([+.\/]?[a-zA-Z0-9_,]+\*?)}/';
/**
@ -20,6 +15,11 @@ class TemplateRoute extends Route
*/
private const RE_UNRESERVED = '[0-9a-zA-Z\-._\~%]*';
/** @var array */
private $pathVariables = [];
/** @var array */
private $explosions = [];
public function getType(): int
{
return Route::TYPE_PATTERN;

View File

@ -17,35 +17,48 @@ class RegexRouteTest extends TestCase
$this->methodMap = $this->prophesize(MethodMap::class);
}
public function testReturnsPatternType()
public function testReturnsPatternType(): void
{
$route = new RegexRoute('/', $this->methodMap->reveal());
$this->assertSame(Route::TYPE_PATTERN, $route->getType());
}
/** @dataProvider matchingRouteProvider */
public function testMatchesTarget($pattern, $path)
/**
* @dataProvider matchingRouteProvider
* @param string $pattern
* @param string $path
*/
public function testMatchesTarget(string $pattern, string $path): void
{
$route = new RegexRoute($pattern, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($path));
}
/** @dataProvider matchingRouteProvider */
public function testMatchesTargetByRegex($pattern, $target)
/**
* @dataProvider matchingRouteProvider
* @param string $pattern
* @param string $path
*/
public function testMatchesTargetByRegex(string $pattern, string $path): void
{
$route = new RegexRoute($pattern, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($target));
$this->assertTrue($route->matchesRequestTarget($path));
}
/** @dataProvider matchingRouteProvider */
public function testExtractsPathVariablesByRegex($pattern, $target, $expectedCaptures)
/**
* @dataProvider matchingRouteProvider
* @param string $pattern
* @param string $path
* @param array $expectedCaptures
*/
public function testExtractsPathVariablesByRegex(string $pattern, string $path, array $expectedCaptures): void
{
$route = new RegexRoute($pattern, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$route->matchesRequestTarget($path);
$this->assertEquals($expectedCaptures, $route->getPathVariables());
}
public function matchingRouteProvider()
public function matchingRouteProvider(): array
{
return [
['~/cat/[0-9]+~', '/cat/2', [0 => '/cat/2']],
@ -62,14 +75,18 @@ class RegexRouteTest extends TestCase
];
}
/** @dataProvider mismatchingRouteProvider */
public function testDoesNotMatchNonmatchingTarget($pattern, $path)
/**
* @dataProvider mismatchingRouteProvider
* @param string $pattern
* @param string $path
*/
public function testDoesNotMatchNonMatchingTarget(string $pattern, string $path): void
{
$route = new RegexRoute($pattern, $this->methodMap->reveal());
$this->assertFalse($route->matchesRequestTarget($path));
}
public function mismatchingRouteProvider()
public function mismatchingRouteProvider(): array
{
return [
['~/cat/[0-9]+~', '/cat/molly'],
@ -80,8 +97,9 @@ class RegexRouteTest extends TestCase
/**
* @dataProvider invalidRouteProvider
* @param string $pattern
*/
public function testThrowsExceptionOnInvalidPattern($pattern)
public function testThrowsExceptionOnInvalidPattern(string $pattern): void
{
$this->expectException(RuntimeException::class);
$route = new RegexRoute($pattern, $this->methodMap->reveal());

View File

@ -9,28 +9,28 @@ class StaticRouteTest extends TestCase
{
use ProphecyTrait;
public function testReturnsStaticType()
public function testReturnsStaticType(): void
{
$methodMap = $this->prophesize(MethodMap::class);
$route = new StaticRoute('/', $methodMap->reveal());
$this->assertSame(Route::TYPE_STATIC, $route->getType());
}
public function testMatchesExactRequestTarget()
public function testMatchesExactRequestTarget(): void
{
$methodMap = $this->prophesize(MethodMap::class);
$route = new StaticRoute('/', $methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget('/'));
}
public function testReturnsEmptyArrayForPathVariables()
public function testReturnsEmptyArrayForPathVariables(): void
{
$methodMap = $this->prophesize(MethodMap::class);
$route = new StaticRoute('/', $methodMap->reveal());
$this->assertSame([], $route->getPathVariables());
}
public function testDoesNotMatchNonmatchingRequestTarget()
public function testDoesNotMatchNonMatchingRequestTarget(): void
{
$methodMap = $this->prophesize(MethodMap::class);
$route = new StaticRoute('/', $methodMap->reveal());

View File

@ -16,7 +16,7 @@ class TemplateRouteTest extends TestCase
$this->methodMap = $this->prophesize(MethodMap::class);
}
private function getExpectedValues($keys)
private function getExpectedValues(array $keys): array
{
$expectedValues = [
'var' => 'value',
@ -33,7 +33,7 @@ class TemplateRouteTest extends TestCase
return array_intersect_key($expectedValues, array_flip($keys));
}
private function assertArrayHasSameContents($expected, $actual)
private function assertArrayHasSameContents($expected, $actual): void
{
ksort($expected);
ksort($actual);
@ -42,7 +42,7 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
public function testReturnsPatternType()
public function testReturnsPatternType(): void
{
$route = new TemplateRoute('/', $this->methodMap->reveal());
$this->assertSame(Route::TYPE_PATTERN, $route->getType());
@ -51,8 +51,12 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
// Matching
/** @dataProvider nonMatchingTargetProvider */
public function testFailsToMatchNonMatchingTarget($template, $target)
/**
* @dataProvider nonMatchingTargetProvider
* @param string $template
* @param string $target
*/
public function testFailsToMatchNonMatchingTarget(string $template, string $target): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$this->assertFalse($route->matchesRequestTarget($target));
@ -71,22 +75,31 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
// Matching :: Simple Strings
/** @dataProvider simpleStringProvider */
public function testMatchesSimpleStrings($template, $target)
/**
* @dataProvider simpleStringProvider
* @param string $template
* @param string $target
*/
public function testMatchesSimpleStrings(string $template, string $target): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($target));
}
/** @dataProvider simpleStringProvider */
public function testCapturesFromSimpleStrings($template, $target, $variables)
/**
* @dataProvider simpleStringProvider
* @param string $template
* @param string $target
* @param string[] $variables
*/
public function testCapturesFromSimpleStrings(string $template, string $target, array $variables): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables());
}
public function simpleStringProvider()
public function simpleStringProvider(): array
{
return [
['/foo', '/foo', []],
@ -100,22 +113,31 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
// Matching :: Reserved
/** @dataProvider reservedStringProvider */
public function testMatchesReservedStrings($template, $target)
/**
* @dataProvider reservedStringProvider
* @param string $template
* @param string $target
*/
public function testMatchesReservedStrings(string $template, string $target): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($target));
}
/** @dataProvider reservedStringProvider */
public function testCapturesFromReservedStrings($template, $target, $variables)
/**
* @dataProvider reservedStringProvider
* @param string $template
* @param string $target
* @param array $variables
*/
public function testCapturesFromReservedStrings(string $template, string $target, array $variables): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$this->assertSame($this->getExpectedValues($variables), $route->getPathVariables());
}
public function reservedStringProvider()
public function reservedStringProvider(): array
{
return [
['/{+var}', '/value', ['var']],
@ -127,22 +149,31 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
// Matching :: Label Expansion
/** @dataProvider labelWithDotPrefixProvider */
public function testMatchesLabelWithDotPrefix($template, $target)
/**
* @dataProvider labelWithDotPrefixProvider
* @param string $template
* @param string $target
*/
public function testMatchesLabelWithDotPrefix(string $template, string $target): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($target));
}
/** @dataProvider labelWithDotPrefixProvider */
public function testCapturesFromLabelWithDotPrefix($template, $target, $variables)
/**
* @dataProvider labelWithDotPrefixProvider
* @param string $template
* @param string $target
* @param array $variables
*/
public function testCapturesFromLabelWithDotPrefix(string $template, string $target, array $variables): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables());
}
public function labelWithDotPrefixProvider()
public function labelWithDotPrefixProvider(): array
{
return [
['/{.who}', '/.fred', ['who']],
@ -154,22 +185,31 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
// Matching :: Path Segments
/** @dataProvider pathSegmentProvider */
public function testMatchesPathSegments($template, $target)
/**
* @dataProvider pathSegmentProvider
* @param string $template
* @param string $target
*/
public function testMatchesPathSegments(string $template, string $target): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($target));
}
/** @dataProvider pathSegmentProvider */
public function testCapturesFromPathSegments($template, $target, $variables)
/**
* @dataProvider pathSegmentProvider
* @param string $template
* @param string $target
* @param array $variables
*/
public function testCapturesFromPathSegments(string $template, string $target, array $variables): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables());
}
public function pathSegmentProvider()
public function pathSegmentProvider(): array
{
return [
['{/who}', '/fred', ['who']],
@ -181,22 +221,31 @@ class TemplateRouteTest extends TestCase
// -------------------------------------------------------------------------
// Matching :: Explosion
/** @dataProvider pathExplosionProvider */
public function testMatchesExplosion($template, $target)
/**
* @dataProvider pathExplosionProvider
* @param string $template
* @param string $target
*/
public function testMatchesExplosion(string $template, string $target): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$this->assertTrue($route->matchesRequestTarget($target));
}
/** @dataProvider pathExplosionProvider */
public function testCapturesFromExplosion($template, $target, $variables)
/**
* @dataProvider pathExplosionProvider
* @param string $template
* @param string $target
* @param array $variables
*/
public function testCapturesFromExplosion(string $template, string $target, array $variables): void
{
$route = new TemplateRoute($template, $this->methodMap->reveal());
$route->matchesRequestTarget($target);
$this->assertArrayHasSameContents($this->getExpectedValues($variables), $route->getPathVariables());
}
public function pathExplosionProvider()
public function pathExplosionProvider(): array
{
return [
['/{count*}', '/one,two,three', ['count']],