Skip to content

Commit

Permalink
Fixes explicit route binding with BackedEnum
Browse files Browse the repository at this point in the history
Port a078b1a (Fixes explicit route binding with `BackedEnum` (laravel#51525),
2024-05-21 crynobone) to 10.x (from 11.x).

fixes laravel#51583
refs laravel#51514

Signed-off-by: CAAHS <[email protected]>
  • Loading branch information
CAAHS authored and CAAHS committed May 27, 2024
1 parent 1d902e9 commit 5edd68e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Routing/CategoryBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Illuminate\Tests\Integration\Routing;

enum CategoryBackedEnum: string
{
case People = 'people';
case Fruits = 'fruits';

public static function fromCode(string $code)
{
return match ($code) {
'c01' => self::People,
'c02' => self::Fruits,
default => null,
};
}
}
9 changes: 0 additions & 9 deletions tests/Integration/Routing/Enums.php

This file was deleted.

19 changes: 16 additions & 3 deletions tests/Integration/Routing/ImplicitBackedEnumRouteBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;

include_once 'Enums.php';

class ImplicitBackedEnumRouteBindingTest extends TestCase
{
protected function defineEnvironment($app): void
Expand Down Expand Up @@ -61,14 +59,20 @@ public function testWithoutRouteCachingEnabled()
return $category->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');

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

$response = $this->post('/categories/cars');
$response->assertNotFound(404);
$response->assertNotFound();

$response = $this->post('/categories-default/');
$response->assertSee('fruits');
Expand All @@ -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();
}
}

0 comments on commit 5edd68e

Please sign in to comment.