Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes explicit route binding with BackedEnum #51525

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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();
}
}