Skip to content

Commit

Permalink
Add RouteCanBackedEnumTest and related enum file
Browse files Browse the repository at this point in the history
  • Loading branch information
Omegadela committed Sep 14, 2024
1 parent 3c97183 commit cdb5098
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/Integration/Routing/AbilityBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Illuminate\Tests\Integration\Routing;

enum AbilityBackedEnum: string
{
case AccessRoute = 'access-route';
case NotAccessRoute = 'not-access-route';
}
42 changes: 42 additions & 0 deletions tests/Integration/Routing/RouteCanBackedEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Tests\Integration\Routing;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;
use User;

class RouteCanBackedEnumTest extends TestCase
{

public function testSimpleRouteWithStringBackedEnumCanAbilityGuestForbiddenThroughTheFramework()
{
$gate = Gate::define(AbilityBackedEnum::NotAccessRoute, fn(?User $user) => false);
$this->assertArrayHasKey('not-access-route', $gate->abilities());

$route = Route::get('/', function () {
return 'Hello World';
})->can(AbilityBackedEnum::NotAccessRoute);
$this->assertEquals(['can:not-access-route'], $route->middleware());

$response = $this->get('/');
$response->assertForbidden();
}

public function testSimpleRouteWithStringBackedEnumCanAbilityGuestAllowedThroughTheFramework()
{
$gate = Gate::define(AbilityBackedEnum::AccessRoute, fn(?User $user) => true);
$this->assertArrayHasKey('access-route', $gate->abilities());

$route = Route::get('/', function () {
return 'Hello World';
})->can(AbilityBackedEnum::AccessRoute);
$this->assertEquals(['can:access-route'], $route->middleware());

$response = $this->get('/');
$response->assertOk();
$response->assertContent('Hello World');
}

}

0 comments on commit cdb5098

Please sign in to comment.