Skip to content
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

[11.x] Allow implicit binding to have optional backed enums #51178

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ protected static function resolveBackedEnumsForRoute($route, $parameters)

$parameterValue = $parameters[$parameterName];

if ($parameterValue === null) {
continue;
}

$backedEnumClass = $parameter->getType()?->getName();

$backedEnum = $backedEnumClass::tryFrom((string) $parameterValue);
Expand Down
18 changes: 18 additions & 0 deletions tests/Routing/ImplicitRouteBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public function test_it_can_resolve_the_implicit_backed_enum_route_bindings_for_
$this->assertSame('fruits', $route->parameter('category')->value);
}

public function test_it_handles_optional_implicit_backed_enum_route_bindings_for_the_given_route_with_optional_parameter()
{
$action = ['uses' => function (?CategoryBackedEnum $category = null) {
return $category->value;
}];

$route = new Route('GET', '/test', $action);
$route->parameters = ['category' => null];

$route->prepareForSerialization();

$container = Container::getInstance();

ImplicitRouteBinding::resolveForRoute($container, $route);

$this->assertNull($route->parameter('category'));
}

public function test_it_does_not_resolve_implicit_non_backed_enum_route_bindings_for_the_given_route()
{
$action = ['uses' => function (CategoryEnum $category) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use UnexpectedValueException;

include_once __DIR__.'/Enums.php';

class RoutingRouteTest extends TestCase
{
public function testBasicDispatchingOfRoutes()
Expand Down Expand Up @@ -1883,6 +1885,21 @@ public function testImplicitBindingsWithOptionalParameterWithExistingKeyInUri()
$this->assertSame('taylor', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
}

public function testOptionalBackedEnumsReturnNullWhenMissing()
{
$router = $this->getRouter();
$router->get('foo/{bar?}', [
'middleware' => SubstituteBindings::class,
'uses' => function (?CategoryBackedEnum $bar = null) {
$this->assertNull($bar);

return 'bar';
},
]);

$router->dispatch(Request::create('foo', 'GET'))->getContent();
}

public function testImplicitBindingsWithMissingModelHandledByMissing()
{
$router = $this->getRouter();
Expand Down