Skip to content

Commit

Permalink
[11.x] Allow implicit binding to have optional backed enums (#51178)
Browse files Browse the repository at this point in the history
* Allow implicit binding to have optional backed enums

* Add route-based test
  • Loading branch information
Neol3108 authored Apr 22, 2024
1 parent ca54cb6 commit 7042fec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
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

0 comments on commit 7042fec

Please sign in to comment.