Skip to content

Commit 259c5f6

Browse files
CAAHSCAAHS
andauthored
Fixes explicit route binding with BackedEnum (#51586)
Port a078b1a (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 <[email protected]> Co-authored-by: CAAHS <[email protected]>
1 parent 1d902e9 commit 259c5f6

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

src/Illuminate/Routing/ImplicitRouteBinding.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ protected static function resolveBackedEnumsForRoute($route, $parameters)
8686

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

89-
$backedEnum = $backedEnumClass::tryFrom((string) $parameterValue);
89+
$backedEnum = $parameterValue instanceof $backedEnumClass
90+
? $parameterValue
91+
: $backedEnumClass::tryFrom((string) $parameterValue);
9092

9193
if (is_null($backedEnum)) {
9294
throw new BackedEnumCaseNotFoundException($backedEnumClass, $parameterValue);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Routing;
4+
5+
enum CategoryBackedEnum: string
6+
{
7+
case People = 'people';
8+
case Fruits = 'fruits';
9+
10+
public static function fromCode(string $code)
11+
{
12+
return match ($code) {
13+
'c01' => self::People,
14+
'c02' => self::Fruits,
15+
default => null,
16+
};
17+
}
18+
}

tests/Integration/Routing/Enums.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/Integration/Routing/ImplicitBackedEnumRouteBindingTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Illuminate\Support\Facades\Route;
66
use Orchestra\Testbench\TestCase;
77

8-
include_once 'Enums.php';
9-
108
class ImplicitBackedEnumRouteBindingTest extends TestCase
119
{
1210
protected function defineEnvironment($app): void
@@ -61,14 +59,20 @@ public function testWithoutRouteCachingEnabled()
6159
return $category->value;
6260
})->middleware('web');
6361

62+
Route::bind('categoryCode', fn (string $categoryCode) => CategoryBackedEnum::fromCode($categoryCode) ?? abort(404));
63+
64+
Route::post('/categories-code/{categoryCode}', function (CategoryBackedEnum $categoryCode) {
65+
return $categoryCode->value;
66+
})->middleware(['web']);
67+
6468
$response = $this->post('/categories/fruits');
6569
$response->assertSee('fruits');
6670

6771
$response = $this->post('/categories/people');
6872
$response->assertSee('people');
6973

7074
$response = $this->post('/categories/cars');
71-
$response->assertNotFound(404);
75+
$response->assertNotFound();
7276

7377
$response = $this->post('/categories-default/');
7478
$response->assertSee('fruits');
@@ -78,5 +82,14 @@ public function testWithoutRouteCachingEnabled()
7882

7983
$response = $this->post('/categories-default/fruits');
8084
$response->assertSee('fruits');
85+
86+
$response = $this->post('/categories-code/c01');
87+
$response->assertSee('people');
88+
89+
$response = $this->post('/categories-code/c02');
90+
$response->assertSee('fruits');
91+
92+
$response = $this->post('/categories-code/00');
93+
$response->assertNotFound();
8194
}
8295
}

0 commit comments

Comments
 (0)