Skip to content

[Routing] backport tests from 4.1 #30012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,95 @@ public function testSchemeAndMethodMismatch()
$matcher->match('/');
}

public function testSiblingRoutes()
{
$coll = new RouteCollection();
$coll->add('a', (new Route('/a{a}'))->setMethods('POST'));
$coll->add('b', (new Route('/a{a}'))->setMethods('PUT'));
$coll->add('c', new Route('/a{a}'));
$coll->add('d', (new Route('/b{a}'))->setCondition('false'));
$coll->add('e', (new Route('/{b}{a}'))->setCondition('false'));
$coll->add('f', (new Route('/{b}{a}'))->setRequirements(['b' => 'b']));

$matcher = $this->getUrlMatcher($coll);
$this->assertEquals(['_route' => 'c', 'a' => 'a'], $matcher->match('/aa'));
$this->assertEquals(['_route' => 'f', 'b' => 'b', 'a' => 'a'], $matcher->match('/ba'));
}

public function testRequirementWithCapturingGroup()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/{a}/{b}', [], ['a' => '(a|b)']));

$matcher = $this->getUrlMatcher($coll);
$this->assertEquals(['_route' => 'a', 'a' => 'a', 'b' => 'b'], $matcher->match('/a/b'));
}

public function testDotAllWithCatchAll()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/{id}.html', [], ['id' => '.+']));
$coll->add('b', new Route('/{all}', [], ['all' => '.+']));

$matcher = $this->getUrlMatcher($coll);
$this->assertEquals(['_route' => 'a', 'id' => 'foo/bar'], $matcher->match('/foo/bar.html'));
}

public function testHostPattern()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/{app}/{action}/{unused}', [], [], [], '{host}'));

$expected = [
'_route' => 'a',
'app' => 'an_app',
'action' => 'an_action',
'unused' => 'unused',
'host' => 'foo',
];
$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'foo'));
$this->assertEquals($expected, $matcher->match('/an_app/an_action/unused'));
}

public function testHostWithDot()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/foo', [], [], [], 'foo.example.com'));
$coll->add('b', new Route('/bar/{baz}'));

$matcher = $this->getUrlMatcher($coll);
$this->assertEquals('b', $matcher->match('/bar/abc.123')['_route']);
}

public function testSlashVariant()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/foo/{bar}', [], ['bar' => '.*']));

$matcher = $this->getUrlMatcher($coll);
$this->assertEquals('a', $matcher->match('/foo/')['_route']);
}

public function testSlashWithVerb()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/{foo}', [], [], [], '', [], ['put', 'delete']));
$coll->add('b', new Route('/bar/'));

$matcher = $this->getUrlMatcher($coll);
$this->assertSame(['_route' => 'b'], $matcher->match('/bar/'));

$coll = new RouteCollection();
$coll->add('a', new Route('/dav/{foo}', [], ['foo' => '.*'], [], '', [], ['GET', 'OPTIONS']));

$matcher = $this->getUrlMatcher($coll, new RequestContext('', 'OPTIONS'));
$expected = [
'_route' => 'a',
'foo' => 'files/bar/',
];
$this->assertEquals($expected, $matcher->match('/dav/files/bar/'));
}

public function testSlashAndVerbPrecedence()
{
$coll = new RouteCollection();
Expand All @@ -527,6 +616,17 @@ public function testSlashAndVerbPrecedence()
$this->assertEquals($expected, $matcher->match('/api/customers/123/contactpersons'));
}

public function testGreedyTrailingRequirement()
{
$coll = new RouteCollection();
$coll->add('a', new Route('/{a}', [], ['a' => '.+']));

$matcher = $this->getUrlMatcher($coll);

$this->assertEquals(['_route' => 'a', 'a' => 'foo'], $matcher->match('/foo'));
$this->assertEquals(['_route' => 'a', 'a' => 'foo/'], $matcher->match('/foo/'));
}

protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?: new RequestContext());
Expand Down