From 259c5f6d8dcaf854e5f416e728153d0371be41a0 Mon Sep 17 00:00:00 2001 From: CAAHS <154250652+CAAHS@users.noreply.github.com> Date: Mon, 27 May 2024 15:41:52 +0200 Subject: [PATCH] Fixes explicit route binding with `BackedEnum` (#51586) Port a078b1a97e (Fixes explicit route binding with `BackedEnum` (#51525), 2024-05-21 crynobone) to 10.x (from 11.x). fixes #51583 refs #51514 Signed-off-by: CAAHS Co-authored-by: CAAHS --- .../Routing/ImplicitRouteBinding.php | 4 +++- .../Routing/CategoryBackedEnum.php | 18 ++++++++++++++++++ tests/Integration/Routing/Enums.php | 9 --------- .../ImplicitBackedEnumRouteBindingTest.php | 19 ++++++++++++++++--- 4 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 tests/Integration/Routing/CategoryBackedEnum.php delete mode 100644 tests/Integration/Routing/Enums.php diff --git a/src/Illuminate/Routing/ImplicitRouteBinding.php b/src/Illuminate/Routing/ImplicitRouteBinding.php index d3590de1d707..f8352e3d57e1 100644 --- a/src/Illuminate/Routing/ImplicitRouteBinding.php +++ b/src/Illuminate/Routing/ImplicitRouteBinding.php @@ -86,7 +86,9 @@ protected static function resolveBackedEnumsForRoute($route, $parameters) $backedEnumClass = $parameter->getType()?->getName(); - $backedEnum = $backedEnumClass::tryFrom((string) $parameterValue); + $backedEnum = $parameterValue instanceof $backedEnumClass + ? $parameterValue + : $backedEnumClass::tryFrom((string) $parameterValue); if (is_null($backedEnum)) { throw new BackedEnumCaseNotFoundException($backedEnumClass, $parameterValue); diff --git a/tests/Integration/Routing/CategoryBackedEnum.php b/tests/Integration/Routing/CategoryBackedEnum.php new file mode 100644 index 000000000000..2ff3cf6e7f80 --- /dev/null +++ b/tests/Integration/Routing/CategoryBackedEnum.php @@ -0,0 +1,18 @@ + self::People, + 'c02' => self::Fruits, + default => null, + }; + } +} diff --git a/tests/Integration/Routing/Enums.php b/tests/Integration/Routing/Enums.php deleted file mode 100644 index 5a2ba0f5a8b1..000000000000 --- a/tests/Integration/Routing/Enums.php +++ /dev/null @@ -1,9 +0,0 @@ -value; })->middleware('web'); + Route::bind('categoryCode', fn (string $categoryCode) => CategoryBackedEnum::fromCode($categoryCode) ?? abort(404)); + + Route::post('/categories-code/{categoryCode}', function (CategoryBackedEnum $categoryCode) { + return $categoryCode->value; + })->middleware(['web']); + $response = $this->post('/categories/fruits'); $response->assertSee('fruits'); @@ -68,7 +72,7 @@ public function testWithoutRouteCachingEnabled() $response->assertSee('people'); $response = $this->post('/categories/cars'); - $response->assertNotFound(404); + $response->assertNotFound(); $response = $this->post('/categories-default/'); $response->assertSee('fruits'); @@ -78,5 +82,14 @@ public function testWithoutRouteCachingEnabled() $response = $this->post('/categories-default/fruits'); $response->assertSee('fruits'); + + $response = $this->post('/categories-code/c01'); + $response->assertSee('people'); + + $response = $this->post('/categories-code/c02'); + $response->assertSee('fruits'); + + $response = $this->post('/categories-code/00'); + $response->assertNotFound(); } }