From e68e7258e5f23a77b3c49a0e44788b4a6bfa363e Mon Sep 17 00:00:00 2001 From: Kevin Redman Date: Fri, 1 May 2020 10:03:36 -0700 Subject: [PATCH 1/3] Fixes duplicate check --- src/Traits/HasRole.php | 2 +- tests/Feature/UserRoleTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Traits/HasRole.php b/src/Traits/HasRole.php index 301b04f..4c5db00 100644 --- a/src/Traits/HasRole.php +++ b/src/Traits/HasRole.php @@ -117,7 +117,7 @@ public function hasRole( $role ) { * @return bool */ public function attachRole( $role ) { - if ( ! collect( $this->getRoles() )->contains( $role ) ) { + if ( ! $this->getRoles()->contains( $role ) ) { $this->clearCached(); $this->roles()->attach( $role ); } diff --git a/tests/Feature/UserRoleTest.php b/tests/Feature/UserRoleTest.php index cadc232..4dd390c 100644 --- a/tests/Feature/UserRoleTest.php +++ b/tests/Feature/UserRoleTest.php @@ -21,4 +21,18 @@ public function testUserHasRoleOnAttach() $this->assertEquals($role->slug, $adminRole->slug); } + + public function testUserHasRoleOnAttachDupilcate() + { + $admin = factory(User::class)->create(); + $role = factory(Role::class)->create(); + + $adminRole = Role::findBySlug('admin'); + $user = User::find(1); + $admin->detachAllRoles(); + $admin->attachRole($adminRole); + $admin->attachRole($adminRole); + + $this->assertCount(1, $user->roles); + } } From dd3c83db6fc4741ed28893477944c818ae093c55 Mon Sep 17 00:00:00 2001 From: Oscar Mwanandimai Date: Fri, 23 Apr 2021 23:00:04 +0200 Subject: [PATCH 2/3] :broom: --- tests/Feature/UserRoleTest.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/Feature/UserRoleTest.php b/tests/Feature/UserRoleTest.php index 4dd390c..1b103de 100644 --- a/tests/Feature/UserRoleTest.php +++ b/tests/Feature/UserRoleTest.php @@ -15,24 +15,20 @@ public function testUserHasRoleOnAttach() $role = Role::factory()->create(); $adminRole = Role::findBySlug('admin'); - $user = User::find(1); $admin->detachAllRoles(); $admin->attachRole($adminRole); $this->assertEquals($role->slug, $adminRole->slug); } - public function testUserHasRoleOnAttachDupilcate() + public function testUserHasOneRoleOnAttachDuplicate() { - $admin = factory(User::class)->create(); - $role = factory(Role::class)->create(); + $admin = User::factory()->create(); + $role = Role::factory()->create(); - $adminRole = Role::findBySlug('admin'); - $user = User::find(1); - $admin->detachAllRoles(); - $admin->attachRole($adminRole); - $admin->attachRole($adminRole); + $admin->attachRole($role); + $admin->attachRole($role); - $this->assertCount(1, $user->roles); + $this->assertCount(1, $admin->roles); } } From fb0f65374e94ff9383617ee897700577ba09182b Mon Sep 17 00:00:00 2001 From: Oscar Mwanandimai Date: Fri, 23 Apr 2021 23:08:09 +0200 Subject: [PATCH 3/3] :lipstick: clean ups on touched files and funcs --- src/Contracts/HasRole.php | 12 +++++++----- src/Traits/HasRole.php | 22 ++++++++++++---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Contracts/HasRole.php b/src/Contracts/HasRole.php index 249a8e3..009a0f1 100644 --- a/src/Contracts/HasRole.php +++ b/src/Contracts/HasRole.php @@ -2,6 +2,8 @@ namespace HttpOz\Roles\Contracts; +use HttpOz\Roles\Models\Role; + interface HasRole { /** @@ -54,15 +56,15 @@ public function hasRole($role); /** * Attach role to a user. * - * @param int|\HttpOz\Roles\Models\Role $role - * @return null|bool + * @param int|Role $role + * @return bool */ - public function attachRole($role); + public function attachRole(int|Role $role): bool; /** * Detach role from a user. * - * @param int|\HttpOz\Roles\Models\Role $role + * @param int|Role $role * @return int */ public function detachRole($role); @@ -70,7 +72,7 @@ public function detachRole($role); /** * Sync roles for a user. * - * @param array|\HttpOz\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles + * @param array|Role[]|\Illuminate\Database\Eloquent\Collection $roles * @return array */ public function syncRoles($roles); diff --git a/src/Traits/HasRole.php b/src/Traits/HasRole.php index 4c5db00..b0aa8b2 100644 --- a/src/Traits/HasRole.php +++ b/src/Traits/HasRole.php @@ -2,6 +2,7 @@ namespace HttpOz\Roles\Traits; +use HttpOz\Roles\Models\Role; use Illuminate\Support\Str; use Illuminate\Support\Facades\Cache; @@ -109,14 +110,15 @@ public function hasRole( $role ) { } } - /** - * Attach role to a user. - * - * @param int|\HttpOz\Roles\Models\Role $role - * - * @return bool - */ - public function attachRole( $role ) { + /** + * Attach role to a user. + * + * @param int|Role $role + * + * @return bool + */ + public function attachRole(int|Role $role): bool + { if ( ! $this->getRoles()->contains( $role ) ) { $this->clearCached(); $this->roles()->attach( $role ); @@ -128,7 +130,7 @@ public function attachRole( $role ) { /** * Detach role from a user. * - * @param int|\HttpOz\Roles\Models\Role $role + * @param int|Role $role * * @return int */ @@ -153,7 +155,7 @@ public function detachAllRoles() { /** * Sync roles for a user. * - * @param array|\HttpOz\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles + * @param array|Role[]|\Illuminate\Database\Eloquent\Collection $roles * * @return array */