Skip to content

Commit

Permalink
Merge pull request #1018 from spatie/1017-tests
Browse files Browse the repository at this point in the history
Add tests for #1017
  • Loading branch information
drbyte authored Feb 4, 2019
2 parents 447de23 + 0889a29 commit a6bd8c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function roles(): MorphToMany
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string|array|\Spatie\Permission\Contracts\Role|\Illuminate\Support\Collection $roles
* @param string $guard
*
* @return \Illuminate\Database\Eloquent\Builder
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,32 @@ public function it_can_scope_users_using_an_object()
$this->assertEquals($scopedUsers3->count(), 1);
}

/** @test */
public function it_can_scope_against_a_specific_guard()
{
$user1 = User::create(['email' => '[email protected]']);
$user2 = User::create(['email' => '[email protected]']);
$user1->assignRole('testRole');
$user2->assignRole('testRole2');

$scopedUsers1 = User::role('testRole', 'web')->get();

$this->assertEquals($scopedUsers1->count(), 1);

$user3 = Admin::create(['email' => '[email protected]']);
$user4 = Admin::create(['email' => '[email protected]']);
$user5 = Admin::create(['email' => '[email protected]']);
$testAdminRole2 = app(Role::class)->create(['name' => 'testAdminRole2', 'guard_name' => 'admin']);
$user3->assignRole($this->testAdminRole);
$user4->assignRole($this->testAdminRole);
$user5->assignRole($testAdminRole2);
$scopedUsers2 = Admin::role('testAdminRole', 'admin')->get();
$scopedUsers3 = Admin::role('testAdminRole2', 'admin')->get();

$this->assertEquals($scopedUsers2->count(), 2);
$this->assertEquals($scopedUsers3->count(), 1);
}

/** @test */
public function it_throws_an_exception_when_trying_to_scope_a_role_from_another_guard()
{
Expand Down

0 comments on commit a6bd8c2

Please sign in to comment.