diff --git a/src/Router.php b/src/Router.php index e782ad7..b979a22 100644 --- a/src/Router.php +++ b/src/Router.php @@ -152,7 +152,7 @@ public function prepareRoutes(ServerRequestInterface $request): void $options = []; /** @var Route $route */ - foreach ($routes as $key => $route) { + foreach ($routes as $route) { if ($route->getStrategy() === null) { $route->setStrategy($this->getStrategy()); } @@ -164,7 +164,7 @@ public function prepareRoutes(ServerRequestInterface $request): void continue; } - // need a messy but useful identifier for to determine what methods to respond with on OPTIONS + // need a messy but useful identifier to determine what methods to respond with on OPTIONS $identifier = $route->getScheme() . static::IDENTIFIER_SEPARATOR . $route->getHost() . static::IDENTIFIER_SEPARATOR . $route->getPort() . static::IDENTIFIER_SEPARATOR . $route->getPath(); @@ -228,7 +228,7 @@ protected function buildOptionsRoutes(array $options): void protected function collectGroupRoutes(): void { - foreach ($this->groups as $key => $group) { + foreach ($this->groups as $group) { $group(); } } diff --git a/tests/DispatchIntegrationTest.php b/tests/DispatchIntegrationTest.php index a2ffb37..f7bea7a 100644 --- a/tests/DispatchIntegrationTest.php +++ b/tests/DispatchIntegrationTest.php @@ -5,6 +5,7 @@ namespace League\Route; use Exception; +use League\Route\Fixture\Controller; use League\Route\Fixture\Middleware; use League\Route\Http\Exception\{BadRequestException, MethodNotAllowedException, NotFoundException}; use League\Route\Strategy\JsonStrategy; @@ -913,4 +914,37 @@ public function process( $router->dispatch($request); } + + public function testCanMapSameRoutePathOnDifferentConditions(): void + { + $router = new Router(); + + $router + ->map('GET', '/', [Controller::class, 'action']) + ->setHost('test1.com') + ; + + $router + ->map('GET', '/', [Controller::class, 'action']) + ->setHost('test2.com') + ; + + $requestOne = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); + $requestTwo = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); + + $uriOne = $this->getMockBuilder(UriInterface::class)->getMock(); + $uriTwo = $this->getMockBuilder(UriInterface::class)->getMock(); + + $uriOne->method('getHost')->willReturn('test1.com'); + $uriTwo->method('getHost')->willReturn('test2.com'); + + $requestOne->method('getUri')->willReturn($uriOne); + $requestTwo->method('getUri')->willReturn($uriTwo); + + $responseOne = $router->dispatch($requestOne); + self::assertSame($responseOne->getHeader('action'), 'true'); + + $responseTwo = $router->dispatch($requestTwo); + self::assertSame($responseTwo->getHeader('action'), 'true'); + } }