From a078b1a97e9e21b7f17d113575b9895572249539 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 22 May 2024 01:42:05 +0800 Subject: [PATCH] Fixes explicit route binding with `BackedEnum` (#51525) fixes #51514 Signed-off-by: Mior Muhammad Zaki --- .../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 288c7a7b6564..7d92ec9757f9 100644 --- a/src/Illuminate/Routing/ImplicitRouteBinding.php +++ b/src/Illuminate/Routing/ImplicitRouteBinding.php @@ -90,7 +90,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(); } }