public function testSuccessfulMatchResultIsReturned() { $expectedMatchResult = MatchResult::fromSuccess([]); $matchResult = RouteCollectionMatcher::matchRouteCollection($this->buildRouteCollection(['foo' => null, 'bar' => $expectedMatchResult]), $this->prophesize(ServerRequestInterface::class)->reveal(), 0, []); $this->assertInstanceOf(MatchResult::class, $matchResult); $this->assertTrue($matchResult->isSuccess()); $this->assertSame('bar', $matchResult->getRouteName()); }
public function testChildMatchMergingWithRouteName() { $matchResult = MatchResult::fromChildMatch(MatchResult::fromChildMatch(MatchResult::fromSuccess([]), [], 'bar'), [], 'foo'); $this->assertTrue($matchResult->isSuccess()); $this->assertSame('foo/bar', $matchResult->getRouteName()); }
public function testIncompletePathMatchWithChildMatch() { $matchResult = $this->buildRoute(['path_parser' => $this->getIncompletePathParser(), 'children' => $this->buildChildren([MatchResult::fromSuccess(['baz' => 'bat'])])])->match($this->buildRequest(), 4); $this->assertInstanceOf(MatchResult::class, $matchResult); $this->assertTrue($matchResult->isSuccess()); $this->assertEquals(['foo' => 'bar', 'baz' => 'bat'], $matchResult->getParams()); }
/** * {@inheritdoc} * * @throws Exception\UnexpectedValueException */ public function match(ServerRequestInterface $request, $pathOffset) { $uri = $request->getUri(); $params = $this->defaults; // Verify scheme first, if set. if (true === $this->secure && 'https' !== $uri->getScheme()) { return MatchResult::fromSchemeFailure($uri->withScheme('https')); } elseif (false === $this->secure && 'http' !== $uri->getScheme()) { return MatchResult::fromSchemeFailure($uri->withScheme('http')); } // Then match hostname, if parser is set. if (null !== $this->hostnameParser) { $hostnameResult = $this->hostnameParser->parse($uri->getHost(), 0); if (null === $hostnameResult || strlen($uri->getHost()) !== $hostnameResult->getMatchLength()) { return null; } $params = $hostnameResult->getParams() + $params; } // Then match port, if set. if (null !== $this->port) { $port = $uri->getPort() ?: ('http' === $uri->getScheme() ? 80 : 443); if ($port !== $this->port) { return null; } } // Next match the path. $completePathMatched = false; if (null !== $this->pathParser) { if (null === ($pathResult = $this->pathParser->parse($uri->getPath(), $pathOffset))) { return null; } $pathOffset += $pathResult->getMatchLength(); $completePathMatched = $pathOffset === strlen($uri->getPath()); $params = $pathResult->getParams() + $params; } if ($completePathMatched) { if (null === $this->methods || in_array($request->getMethod(), $this->methods)) { return MatchResult::fromSuccess($params); } if (empty($this->methods)) { // Special case: when no methods are defined at all, this route may simply not terminate. return null; } return MatchResult::fromMethodFailure($this->methods); } // The path was not completely matched yet, so we check the children. if (null === $this->children) { return null; } return RouteCollectionMatcher::matchRouteCollection($this->children, $request, $pathOffset, $params); }