From 4eec56b5826ec07636c7fb173a2b36abc0484221 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Fri, 14 Aug 2020 07:00:50 -0400 Subject: [PATCH] Mark Routing\Route classes as @internal --- src/Routing/Route/MethodMap.php | 13 ++--- src/Routing/Route/PrefixRoute.php | 3 ++ src/Routing/Route/RegexRoute.php | 3 ++ src/Routing/Route/Route.php | 8 +++ src/Routing/Route/RouteFactory.php | 2 +- src/Routing/Route/RouteFactoryInterface.php | 3 ++ src/Routing/Route/RouteInterface.php | 13 +++-- src/Routing/Route/StaticRoute.php | 3 ++ src/Routing/Route/TemplateRoute.php | 3 ++ .../unit/Routing/Route/MethodMapTest.php | 50 ++++++++++--------- .../unit/Routing/Route/PrefixRouteTest.php | 17 +++---- 11 files changed, 73 insertions(+), 45 deletions(-) diff --git a/src/Routing/Route/MethodMap.php b/src/Routing/Route/MethodMap.php index a27d08e..9fa746d 100644 --- a/src/Routing/Route/MethodMap.php +++ b/src/Routing/Route/MethodMap.php @@ -5,8 +5,12 @@ namespace WellRESTed\Routing\Route; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use WellRESTed\Dispatching\DispatcherInterface; +use WellRESTed\MiddlewareInterface; -class MethodMap +/** + * @internal + */ +class MethodMap implements MiddlewareInterface { /** @var DispatcherInterface */ private $dispatcher; @@ -21,11 +25,8 @@ class MethodMap $this->dispatcher = $dispatcher; } - // ------------------------------------------------------------------------- - // MethodMapInterface - /** - * Register a dispatchable (handler or middleware) with a method. + * Register a dispatchable (e.g.m handler or middleware) with a method. * * $method may be: * - A single verb ("GET"), @@ -41,7 +42,7 @@ class MethodMap * @param string $method * @param mixed $dispatchable */ - public function register($method, $dispatchable): void + public function register(string $method, $dispatchable): void { $methods = explode(',', $method); $methods = array_map('trim', $methods); diff --git a/src/Routing/Route/PrefixRoute.php b/src/Routing/Route/PrefixRoute.php index 75efd99..c958f8b 100644 --- a/src/Routing/Route/PrefixRoute.php +++ b/src/Routing/Route/PrefixRoute.php @@ -2,6 +2,9 @@ namespace WellRESTed\Routing\Route; +/** + * @internal + */ class PrefixRoute extends Route { public function __construct(string $target, MethodMap $methodMap) diff --git a/src/Routing/Route/RegexRoute.php b/src/Routing/Route/RegexRoute.php index 8e2a046..536c3d6 100644 --- a/src/Routing/Route/RegexRoute.php +++ b/src/Routing/Route/RegexRoute.php @@ -4,6 +4,9 @@ namespace WellRESTed\Routing\Route; use RuntimeException; +/** + * @internal + */ class RegexRoute extends Route { /** @var array */ diff --git a/src/Routing/Route/Route.php b/src/Routing/Route/Route.php index 0ba21ce..c62b1b2 100644 --- a/src/Routing/Route/Route.php +++ b/src/Routing/Route/Route.php @@ -5,6 +5,9 @@ namespace WellRESTed\Routing\Route; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +/** + * @internal + */ abstract class Route implements RouteInterface { /** @var string */ @@ -18,6 +21,11 @@ abstract class Route implements RouteInterface $this->methodMap = $methodMap; } + /** + * Path, partial path, or pattern to match request paths against. + * + * @return string + */ public function getTarget(): string { return $this->target; diff --git a/src/Routing/Route/RouteFactory.php b/src/Routing/Route/RouteFactory.php index 6007e44..1e16e94 100644 --- a/src/Routing/Route/RouteFactory.php +++ b/src/Routing/Route/RouteFactory.php @@ -5,7 +5,7 @@ namespace WellRESTed\Routing\Route; use WellRESTed\Dispatching\DispatcherInterface; /** - * Class for creating routes + * @internal */ class RouteFactory implements RouteFactoryInterface { diff --git a/src/Routing/Route/RouteFactoryInterface.php b/src/Routing/Route/RouteFactoryInterface.php index 829b806..c169682 100644 --- a/src/Routing/Route/RouteFactoryInterface.php +++ b/src/Routing/Route/RouteFactoryInterface.php @@ -2,6 +2,9 @@ namespace WellRESTed\Routing\Route; +/** + * @internal + */ interface RouteFactoryInterface { /** diff --git a/src/Routing/Route/RouteInterface.php b/src/Routing/Route/RouteInterface.php index 8eaa017..850a6c5 100644 --- a/src/Routing/Route/RouteInterface.php +++ b/src/Routing/Route/RouteInterface.php @@ -4,19 +4,24 @@ namespace WellRESTed\Routing\Route; use WellRESTed\MiddlewareInterface; +/** + * @internal + */ interface RouteInterface extends MiddlewareInterface { - /** Matches when path is an exact match only */ + /** Matches when request path is an exact match to entire target */ const TYPE_STATIC = 0; - /** Matches when path has the expected beginning */ + /** Matches when request path is an exact match to start of target */ const TYPE_PREFIX = 1; - /** Matches by pattern. Use matchesRequestTarget to test for matches */ + /** Matches by request path by pattern and may extract matched varialbes */ const TYPE_PATTERN = 2; /** + * Path, partial path, or pattern to match request paths against. + * * @return string */ - public function getTarget(); + public function getTarget(): string; /** * Return the RouteInterface::TYPE_ constants that identifies the type. diff --git a/src/Routing/Route/StaticRoute.php b/src/Routing/Route/StaticRoute.php index 6cf9d77..79f6c11 100644 --- a/src/Routing/Route/StaticRoute.php +++ b/src/Routing/Route/StaticRoute.php @@ -2,6 +2,9 @@ namespace WellRESTed\Routing\Route; +/** + * @internal + */ class StaticRoute extends Route { public function getType(): int diff --git a/src/Routing/Route/TemplateRoute.php b/src/Routing/Route/TemplateRoute.php index df2f99c..13f99f8 100644 --- a/src/Routing/Route/TemplateRoute.php +++ b/src/Routing/Route/TemplateRoute.php @@ -2,6 +2,9 @@ namespace WellRESTed\Routing\Route; +/** + * @internal + */ class TemplateRoute extends Route { /** @var array */ diff --git a/test/tests/unit/Routing/Route/MethodMapTest.php b/test/tests/unit/Routing/Route/MethodMapTest.php index ae9f1a7..a94aadc 100644 --- a/test/tests/unit/Routing/Route/MethodMapTest.php +++ b/test/tests/unit/Routing/Route/MethodMapTest.php @@ -1,11 +1,10 @@ dispatcher = new Dispatcher(); } - private function getMethodMap() + private function getMethodMap(): MethodMap { return new MethodMap($this->dispatcher); } // ------------------------------------------------------------------------- - public function testDispatchesMiddlewareWithMatchingMethod() + public function testDispatchesMiddlewareWithMatchingMethod(): void { $this->request = $this->request->withMethod('GET'); @@ -45,7 +44,7 @@ class MethodMapTest extends TestCase $this->assertTrue($this->middleware->called); } - public function testTreatsMethodNamesCaseSensitively() + public function testTreatsMethodNamesCaseSensitively(): void { $this->request = $this->request->withMethod('get'); @@ -60,7 +59,7 @@ class MethodMapTest extends TestCase $this->assertTrue($middlewareLower->called); } - public function testDispatchesWildcardMiddlewareWithNonMatchingMethod() + public function testDispatchesWildcardMiddlewareWithNonMatchingMethod(): void { $this->request = $this->request->withMethod('GET'); @@ -71,7 +70,7 @@ class MethodMapTest extends TestCase $this->assertTrue($this->middleware->called); } - public function testDispatchesGetMiddlewareForHeadByDefault() + public function testDispatchesGetMiddlewareForHeadByDefault(): void { $this->request = $this->request->withMethod('HEAD'); @@ -82,7 +81,7 @@ class MethodMapTest extends TestCase $this->assertTrue($this->middleware->called); } - public function testRegistersMiddlewareForMultipleMethods() + public function testRegistersMiddlewareForMultipleMethods(): void { $map = $this->getMethodMap(); $map->register('GET,POST', $this->middleware); @@ -96,7 +95,7 @@ class MethodMapTest extends TestCase $this->assertEquals(2, $this->middleware->callCount); } - public function testSettingNullDeregistersMiddleware() + public function testSettingNullUnregistersMiddleware(): void { $this->request = $this->request->withMethod('POST'); @@ -108,7 +107,7 @@ class MethodMapTest extends TestCase $this->assertEquals(405, $response->getStatusCode()); } - public function testSetsStatusTo200ForOptions() + public function testSetsStatusTo200ForOptions(): void { $this->request = $this->request->withMethod('OPTIONS'); @@ -119,7 +118,7 @@ class MethodMapTest extends TestCase $this->assertEquals(200, $response->getStatusCode()); } - public function testStopsPropagatingAfterOptions() + public function testStopsPropagatingAfterOptions(): void { $this->request = $this->request->withMethod('OPTIONS'); @@ -130,8 +129,12 @@ class MethodMapTest extends TestCase $this->assertFalse($this->next->called); } - /** @dataProvider allowedMethodProvider */ - public function testSetsAllowHeaderForOptions($methodsDeclared, $methodsAllowed) + /** + * @dataProvider allowedMethodProvider + * @param string[] $methodsDeclared + * @param string[] $methodsAllowed + */ + public function testSetsAllowHeaderForOptions(array $methodsDeclared, array $methodsAllowed): void { $this->request = $this->request->withMethod('OPTIONS'); @@ -144,8 +147,7 @@ class MethodMapTest extends TestCase $this->assertContainsEach($methodsAllowed, $response->getHeaderLine('Allow')); } - /** @dataProvider allowedMethodProvider */ - public function testSetsStatusTo405ForBadMethod() + public function testSetsStatusTo405ForBadMethod(): void { $this->request = $this->request->withMethod('POST'); @@ -156,11 +158,7 @@ class MethodMapTest extends TestCase $this->assertEquals(405, $response->getStatusCode()); } - /** - * @coversNothing - * @dataProvider allowedMethodProvider - */ - public function testStopsPropagatingAfterBadMethod() + public function testStopsPropagatingAfterBadMethod(): void { $this->request = $this->request->withMethod('POST'); @@ -170,8 +168,12 @@ class MethodMapTest extends TestCase $this->assertFalse($this->next->called); } - /** @dataProvider allowedMethodProvider */ - public function testSetsAllowHeaderForBadMethod($methodsDeclared, $methodsAllowed) + /** + * @dataProvider allowedMethodProvider + * @param string[] $methodsDeclared + * @param string[] $methodsAllowed + */ + public function testSetsAllowHeaderForBadMethod(array $methodsDeclared, array $methodsAllowed): void { $this->request = $this->request->withMethod('BAD'); @@ -184,7 +186,7 @@ class MethodMapTest extends TestCase $this->assertContainsEach($methodsAllowed, $response->getHeaderLine('Allow')); } - public function allowedMethodProvider() + public function allowedMethodProvider(): array { return [ [['GET'], ['GET', 'HEAD', 'OPTIONS']], @@ -195,7 +197,7 @@ class MethodMapTest extends TestCase ]; } - private function assertContainsEach($expectedList, $actual) + private function assertContainsEach($expectedList, $actual): void { foreach ($expectedList as $expected) { if (strpos($actual, $expected) === false) { diff --git a/test/tests/unit/Routing/Route/PrefixRouteTest.php b/test/tests/unit/Routing/Route/PrefixRouteTest.php index dad929e..2af3d19 100644 --- a/test/tests/unit/Routing/Route/PrefixRouteTest.php +++ b/test/tests/unit/Routing/Route/PrefixRouteTest.php @@ -1,53 +1,50 @@ prophesize(MethodMap::class); $route = new PrefixRoute('/cats/*', $methodMap->reveal()); $this->assertEquals('/cats/', $route->getTarget()); } - public function testReturnsPrefixType() + public function testReturnsPrefixType(): void { $methodMap = $this->prophesize(MethodMap::class); $route = new PrefixRoute('/*', $methodMap->reveal()); $this->assertSame(RouteInterface::TYPE_PREFIX, $route->getType()); } - public function testReturnsEmptyArrayForPathVariables() + public function testReturnsEmptyArrayForPathVariables(): void { $methodMap = $this->prophesize(MethodMap::class); $route = new PrefixRoute('/*', $methodMap->reveal()); $this->assertSame([], $route->getPathVariables()); } - public function testMatchesExactRequestTarget() + public function testMatchesExactRequestTarget(): void { $methodMap = $this->prophesize(MethodMap::class); $route = new PrefixRoute('/*', $methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget('/')); } - public function testMatchesRequestTargetWithSamePrefix() + public function testMatchesRequestTargetWithSamePrefix(): void { $methodMap = $this->prophesize(MethodMap::class); $route = new PrefixRoute('/*', $methodMap->reveal()); $this->assertTrue($route->matchesRequestTarget('/cats/')); } - public function testDoesNotMatchNonmatchingRequestTarget() + public function testDoesNotMatchNonMatchingRequestTarget(): void { $methodMap = $this->prophesize(MethodMap::class); $route = new PrefixRoute('/animals/cats/', $methodMap->reveal());