-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Enum support for Role and WithoutRole scopes
Ref #2391
- Loading branch information
Showing
2 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,37 @@ public function it_can_assign_and_remove_a_role_using_enums() | |
$this->assertFalse($this->testUser->hasRole($enum1)); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @requires PHP >= 8.1 | ||
*/ | ||
public function it_can_scope_a_role_using_enums() | ||
{ | ||
$enum1 = TestModels\TestRolePermissionsEnum::USERMANAGER; | ||
$enum2 = TestModels\TestRolePermissionsEnum::WRITER; | ||
$role1 = app(Role::class)->findOrCreate($enum1->value, 'web'); | ||
$role2 = app(Role::class)->findOrCreate($enum2->value, 'web'); | ||
|
||
User::all()->each(fn ($item) => $item->delete()); | ||
$user1 = User::create(['email' => '[email protected]']); | ||
$user2 = User::create(['email' => '[email protected]']); | ||
$user3 = User::create(['email' => '[email protected]']); | ||
|
||
// assign only one user to a role | ||
$user2->assignRole($enum1); | ||
$this->assertTrue($user2->hasRole($enum1)); | ||
$this->assertFalse($user2->hasRole($enum2)); | ||
|
||
$scopedUsers1 = User::role($enum1)->get(); | ||
$scopedUsers2 = User::role($enum2)->get(); | ||
$scopedUsers3 = User::withoutRole($enum2)->get(); | ||
|
||
$this->assertEquals(1, $scopedUsers1->count()); | ||
$this->assertEquals(0, $scopedUsers2->count()); | ||
$this->assertEquals(3, $scopedUsers3->count()); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_assign_and_remove_a_role() | ||
{ | ||
|