From e22d9ae162630f6a7914348639d6cba54d9ce249 Mon Sep 17 00:00:00 2001 From: Remy Bos Date: Sun, 8 Aug 2021 12:46:14 +0200 Subject: [PATCH] DISPATCHER TEST: Add tests for extending dispatcher --- src/Route.php | 1 - tests/DispatcherTest.php | 237 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 tests/DispatcherTest.php diff --git a/src/Route.php b/src/Route.php index 9c8e809..e17b564 100644 --- a/src/Route.php +++ b/src/Route.php @@ -4,7 +4,6 @@ namespace League\Route; -use FastRoute\RouteParser\Std; use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait, StrategyInterface}; use Psr\Container\ContainerInterface; diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php new file mode 100644 index 0000000..983eed1 --- /dev/null +++ b/tests/DispatcherTest.php @@ -0,0 +1,237 @@ +routeCollector = new RouteCollector( + new RouteParser\Std(), + new DataGenerator\GroupCountBased() + ); + } + + public function testExtendDispatcherNoStrategySetRouteNotFoundExpectRuntimeExceptionThrown(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Cannot determine strategy to use for dispatch of not found route'); + + $request = $this->createMock(ServerRequestInterface::class); + $uri = $this->createMock(UriInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $get = 'GET'; + $path = '/example'; + $handler = static function ($request, $args) use ($response) { + return $response; + }; + + $route = new Route($get, $path, $handler); + + $this->routeCollector->addRoute($route->getMethod(), $route->getPath(), $route); + $someRoutesData = $this->routeCollector->getData(); + + $myDispatcher = new class ($someRoutesData) extends Dispatcher { + }; + + $request + ->method('getMethod') + ->willReturn($get) + ; + + $request + ->method('getUri') + ->willReturn($uri) + ; + + $uri + ->method('getPath') + ->willReturn('/different-path') + ; + + $myDispatcher->dispatchRequest($request); + } + + public function testExtendDispatcherNoStrategySetMethodNotAllowedExpectRuntimeExceptionThrown(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Cannot determine strategy to use for dispatch of method not allowed route'); + + $request = $this->createMock(ServerRequestInterface::class); + $uri = $this->createMock(UriInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $get = 'GET'; + $path = '/example'; + $handler = static function ($request, $args) use ($response) { + return $response; + }; + + $route = new Route($get, $path, $handler); + + $this->routeCollector->addRoute($route->getMethod(), $route->getPath(), $route); + $someRoutesData = $this->routeCollector->getData(); + + $myDispatcher = new class ($someRoutesData) extends Dispatcher { + }; + + $request + ->method('getMethod') + ->willReturn('NOT ALLOWED') + ; + + $request + ->method('getUri') + ->willReturn($uri) + ; + + $uri + ->method('getPath') + ->willReturn($path) + ; + + $myDispatcher->dispatchRequest($request); + } + + public function testExtendDispatcherNoStrategySetRouteFoundExpectRuntimeExceptionThrown(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Cannot determine strategy to use for dispatch of found route'); + + $request = $this->createMock(ServerRequestInterface::class); + $uri = $this->createMock(UriInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $get = 'GET'; + $path = '/example'; + $handler = static function ($request, $args) use ($response) { + return $response; + }; + + $route = new Route($get, $path, $handler); + + $this->routeCollector->addRoute($route->getMethod(), $route->getPath(), $route); + $someRoutesData = $this->routeCollector->getData(); + + $myDispatcher = new class ($someRoutesData) extends Dispatcher { + }; + + $request + ->method('getMethod') + ->willReturn($get) + ; + + $request + ->method('getUri') + ->willReturn($uri) + ; + + $uri + ->method('getPath') + ->willReturn($path) + ; + + $myDispatcher->dispatchRequest($request); + } + + public function testExtendDispatcherIsExtraConditionSchemeMismatchExpectNotFoundExceptionThrown(): void + { + $this->expectException(NotFoundException::class); + + $request = $this->createMock(ServerRequestInterface::class); + $uri = $this->createMock(UriInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $get = 'GET'; + $path = '/example'; + $handler = static function ($request, $args) use ($response) { + return $response; + }; + + $route = new Route($get, $path, $handler); + + $this->routeCollector->addRoute($route->getMethod(), $route->getPath(), $route); + $someRoutesData = $this->routeCollector->getData(); + + $myDispatcher = new class ($someRoutesData) extends Dispatcher { + }; + + $myDispatcher->setStrategy(new ApplicationStrategy()); + + $request + ->method('getMethod') + ->willReturn($get) + ; + + $request + ->method('getUri') + ->willReturn($uri) + ; + + $uri + ->method('getPath') + ->willReturn($path) + ; + + $uri + ->method('getScheme') + ->willReturn('https') + ; + + $route->setScheme('http'); + + $myDispatcher->dispatchRequest($request); + } + + public function testExtendDispatcherEnsureHandlerConvertedToRouteExpectHandlerResponseReturned(): void + { + $request = $this->createMock(ServerRequestInterface::class); + $uri = $this->createMock(UriInterface::class); + $response = $this->createMock(ResponseInterface::class); + + $get = 'GET'; + $path = '/example'; + $handler = static function ($request, $args) use ($response) { + return $response; + }; + + $this->routeCollector->addRoute($get, $path, $handler); + $someRoutesData = $this->routeCollector->getData(); + + $myDispatcher = new class ($someRoutesData) extends Dispatcher { + }; + + $myDispatcher->setStrategy(new ApplicationStrategy()); + + $request + ->method('getMethod') + ->willReturn($get) + ; + + $request + ->method('getUri') + ->willReturn($uri) + ; + + $uri + ->method('getPath') + ->willReturn($path) + ; + + $actualResponse = $myDispatcher->dispatchRequest($request); + + $this->assertSame($response, $actualResponse); + } +}