Skip to content

Commit e804379

Browse files
Add tests for custom guard sync (#25)
* Add tests for custom guard sync * Apply fixes from StyleCI (#26) Co-authored-by: StyleCI Bot <[email protected]> * Adjust the sync logic * Apply fixes from StyleCI (#27) Co-authored-by: StyleCI Bot <[email protected]> * Adjust the test to include php8.5 --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 35d3ed7 commit e804379

File tree

6 files changed

+41
-24
lines changed

6 files changed

+41
-24
lines changed

.github/workflows/run-tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: run-tests
22

33
on:
4-
push:
4+
pull_request:
5+
types: [ready_for_review, synchronize]
56
paths:
67
- '**.php'
78
- '.github/workflows/run-tests.yml'
@@ -17,7 +18,7 @@ jobs:
1718
fail-fast: true
1819
matrix:
1920
os: [ubuntu-latest]
20-
php: [8.4, 8.3, 8.2]
21+
php: [8.5, 8.4, 8.3, 8.2]
2122
laravel: ["^12.0", "^11.0"]
2223
stability: [prefer-lowest, prefer-stable]
2324
include:

src/Actions/SyncDefinedRole.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
namespace BinaryCats\LaravelRbac\Actions;
44

55
use BackedEnum;
6+
use Illuminate\Support\Facades\Artisan;
67
use Lorisleiva\Actions\Action;
7-
use Spatie\Permission\Contracts\Role;
8+
use Spatie\Permission\Commands\CreateRole;
89

910
class SyncDefinedRole extends Action
1011
{
11-
public function __construct(
12-
protected readonly Role $role
13-
) {
14-
}
15-
1612
/**
1713
* Handle syncing a defined role.
1814
*/
1915
public function handle(string $name, string $guard, array $permissions): void
2016
{
2117
$permissions = collect($permissions)
22-
->map(fn ($permission) => match (true) {
18+
->map(fn ($permission): string => match (true) {
2319
$permission instanceof BackedEnum => $permission->value,
2420
default => (string) $permission
25-
});
21+
})->implode('|');
2622

27-
$this->role::findOrCreate($name, $guard)
28-
->syncPermissions($permissions);
23+
Artisan::call(CreateRole::class, [
24+
'name' => $name,
25+
'guard' => $guard,
26+
'permissions' => $permissions,
27+
]);
2928
}
3029
}

src/Commands/AbilityMakeCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ protected function getStub()
4040
*/
4141
protected function getDefaultNamespace($rootNamespace)
4242
{
43-
return $rootNamespace.'\Abilities';
43+
$path = config('rbac.path', 'Abilities');
44+
45+
return str($path)
46+
->after(app()->path())
47+
->trim('/')
48+
->prepend($rootNamespace, '\\');
4449
}
4550

4651
/**

src/Commands/DefinedRoleMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DefinedRoleMakeCommand extends GeneratorCommand
2525
*/
2626
protected function getStub()
2727
{
28-
return file_exists($customPath = base_path('/stubs/defined-role.stub'))
28+
return file_exists($customPath = $this->laravel->basePath('/stubs/defined-role.stub'))
2929
? $customPath
3030
: __DIR__.'/../../stubs/defined-role.stub';
3131
}

src/Jobs/ResetPermissions.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ class ResetPermissions
1818
use Queueable;
1919
use SerializesModels;
2020

21-
protected string $guard;
21+
protected readonly string $guard;
2222

23-
/**
24-
* @param string|null $guard
25-
*/
2623
public function __construct(?string $guard = null)
2724
{
2825
$this->guard = $guard ?? config('auth.defaults.guard');
2926
}
3027

3128
/**
32-
* @return void
29+
* Handle resetting permissions.
3330
*/
3431
public function handle(): void
3532
{

tests/Actions/SyncDefinedRoleTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use BinaryCats\LaravelRbac\Tests\Fixtures\Abilities\FooAbility;
88
use BinaryCats\LaravelRbac\Tests\TestCase;
99
use PHPUnit\Framework\Attributes\Test;
10-
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
1110

1211
class SyncDefinedRoleTest extends TestCase
1312
{
@@ -29,13 +28,29 @@ public function it_will_defer_syncing_defined_role_to_artisan(): void
2928
}
3029

3130
#[Test]
32-
public function it_will_throw_an_exception_on_missing_permission(): void
31+
public function it_will_defer_syncing_defined_role_to_artisan_with_custom_guard(): void
3332
{
34-
$this->expectException(PermissionDoesNotExist::class);
35-
$this->expectExceptionMessage('There is no permission named `bar` for guard `web`');
33+
StorePermission::run('bar', 'admin');
34+
StorePermission::run(FooAbility::One, 'admin');
3635

37-
SyncDefinedRole::run('foo role', 'web', [
36+
SyncDefinedRole::run('foo role', 'admin', [
3837
'bar',
38+
FooAbility::One,
39+
'this-permission-is-new-and-will-be-created',
3940
]);
41+
42+
$this->assertDatabaseHas(config('permission.table_names.roles'), [
43+
'name' => 'foo role',
44+
'guard_name' => 'admin',
45+
]);
46+
47+
$role = app(config('permission.models.role'))->where([
48+
'name' => 'foo role',
49+
'guard_name' => 'admin',
50+
])->firstOrFail();
51+
52+
$this->assertTrue($role->hasPermissionTo('bar', 'admin'));
53+
$this->assertTrue($role->hasPermissionTo(FooAbility::One, 'admin'));
54+
$this->assertTrue($role->hasPermissionTo('this-permission-is-new-and-will-be-created', 'admin'));
4055
}
4156
}

0 commit comments

Comments
 (0)