bug #9760 [Routing] Fix router matching pattern against multiple hosts (karolsojko)

This PR was merged into the 2.3 branch.

Discussion
----------

[Routing] Fix router matching pattern against multiple hosts

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8811, #6744
| License       | MIT
| Doc PR        |

When you had a pattern that matched on multiple host then only the first one was displayed as "almost matching". Fixed router matching against the same pattern on multiple hosts so now it shows every "almost match" on different hosts.

Commits
-------

f727b22 [Routing] Fix router matching pattern against multiple hosts
This commit is contained in:
Fabien Potencier 2013-12-15 18:21:48 +01:00
commit f056ac1c22
2 changed files with 32 additions and 1 deletions

View File

@ -75,7 +75,7 @@ class TraceableUrlMatcher extends UrlMatcher
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
$this->addTrace(sprintf('Host "%s" does not match the requirement ("%s")', $this->context->getHost(), $route->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
return true;
continue;
}
// check HTTP method requirement

View File

@ -54,6 +54,37 @@ class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(0, 1, 2), $this->getLevels($traces));
}
public function testMatchRouteOnMultipleHosts()
{
$routes = new RouteCollection();
$routes->add('first', new Route(
'/mypath/',
array('_controller' => 'MainBundle:Info:first'),
array(),
array(),
'some.example.com'
));
$routes->add('second', new Route(
'/mypath/',
array('_controller' => 'MainBundle:Info:second'),
array(),
array(),
'another.example.com'
));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($routes, $context);
$traces = $matcher->getTraces('/mypath/');
$this->assertEquals(
array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
$this->getLevels($traces)
);
}
public function getLevels($traces)
{
$levels = array();