Skip to content

Commit e5d6c57

Browse files
minor #61094 [Routing] Add test to validate that default value is allowed to not match requirement (tcoch)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Routing] Add test to validate that default value is allowed to not match requirement | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | #61022 | License | MIT Suppose you have this route definition: ```php $route = new Route('/test/{foo}', ['foo' => 'foo-'], ['foo' => '\w+'], []); ``` With this route definition: - `/test/foo` works - `/test/foo-` fails, because the dash is not permitted by the requirement - `/test` works, thanks to the default value. However, the value of the parameter `foo` does not meet the requirement. Commits ------- f6e91fd [Routing] Add test to validate that default value is allowed to not match requirement
2 parents c50356b + f6e91fd commit e5d6c57

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,30 @@ public function testUtf8VarName()
982982
$this->assertEquals(['_route' => 'foo', 'bär' => 'baz', 'bäz' => 'foo'], $matcher->match('/foo/baz'));
983983
}
984984

985+
public function testParameterWithRequirementWithDefault()
986+
{
987+
$collection = new RouteCollection();
988+
989+
$route = new Route('/test/{foo}', ['foo' => 'foo-'], ['foo' => '\w+']);
990+
$collection->add('test', $route);
991+
992+
$matcher = $this->getUrlMatcher($collection);
993+
994+
$result = $matcher->match('/test/foo');
995+
$this->assertSame('test', $result['_route']);
996+
$this->assertSame('foo', $result['foo']);
997+
998+
try {
999+
$matcher->match('/test/foo-');
1000+
} catch (ResourceNotFoundException $e) {
1001+
$this->assertStringContainsString('No routes found', $e->getMessage());
1002+
}
1003+
1004+
$result = $matcher->match('/test');
1005+
$this->assertSame('test', $result['_route']);
1006+
$this->assertSame('foo-', $result['foo']);
1007+
}
1008+
9851009
protected function getUrlMatcher(RouteCollection $routes, ?RequestContext $context = null)
9861010
{
9871011
return new UrlMatcher($routes, $context ?? new RequestContext());

0 commit comments

Comments
 (0)