Skip to content

Commit 7042fec

Browse files
authored
[11.x] Allow implicit binding to have optional backed enums (#51178)
* Allow implicit binding to have optional backed enums * Add route-based test
1 parent ca54cb6 commit 7042fec

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Illuminate/Routing/ImplicitRouteBinding.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ protected static function resolveBackedEnumsForRoute($route, $parameters)
8484

8585
$parameterValue = $parameters[$parameterName];
8686

87+
if ($parameterValue === null) {
88+
continue;
89+
}
90+
8791
$backedEnumClass = $parameter->getType()?->getName();
8892

8993
$backedEnum = $backedEnumClass::tryFrom((string) $parameterValue);

tests/Routing/ImplicitRouteBindingTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ public function test_it_can_resolve_the_implicit_backed_enum_route_bindings_for_
4949
$this->assertSame('fruits', $route->parameter('category')->value);
5050
}
5151

52+
public function test_it_handles_optional_implicit_backed_enum_route_bindings_for_the_given_route_with_optional_parameter()
53+
{
54+
$action = ['uses' => function (?CategoryBackedEnum $category = null) {
55+
return $category->value;
56+
}];
57+
58+
$route = new Route('GET', '/test', $action);
59+
$route->parameters = ['category' => null];
60+
61+
$route->prepareForSerialization();
62+
63+
$container = Container::getInstance();
64+
65+
ImplicitRouteBinding::resolveForRoute($container, $route);
66+
67+
$this->assertNull($route->parameter('category'));
68+
}
69+
5270
public function test_it_does_not_resolve_implicit_non_backed_enum_route_bindings_for_the_given_route()
5371
{
5472
$action = ['uses' => function (CategoryEnum $category) {

tests/Routing/RoutingRouteTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
4343
use UnexpectedValueException;
4444

45+
include_once __DIR__.'/Enums.php';
46+
4547
class RoutingRouteTest extends TestCase
4648
{
4749
public function testBasicDispatchingOfRoutes()
@@ -1883,6 +1885,21 @@ public function testImplicitBindingsWithOptionalParameterWithExistingKeyInUri()
18831885
$this->assertSame('taylor', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
18841886
}
18851887

1888+
public function testOptionalBackedEnumsReturnNullWhenMissing()
1889+
{
1890+
$router = $this->getRouter();
1891+
$router->get('foo/{bar?}', [
1892+
'middleware' => SubstituteBindings::class,
1893+
'uses' => function (?CategoryBackedEnum $bar = null) {
1894+
$this->assertNull($bar);
1895+
1896+
return 'bar';
1897+
},
1898+
]);
1899+
1900+
$router->dispatch(Request::create('foo', 'GET'))->getContent();
1901+
}
1902+
18861903
public function testImplicitBindingsWithMissingModelHandledByMissing()
18871904
{
18881905
$router = $this->getRouter();

0 commit comments

Comments
 (0)