Skip to content

Commit

Permalink
Improved test coverage and added exception in the getIdFor method
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Feb 14, 2017
1 parent 9f7d507 commit 2e4fe7b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/Laratrust/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,22 @@ public function flushCache()
}

/**
* @param $object
* @return mixed
* Gets the it from an array or object
* @param mixed $object
* @return int
*/
private function getIdFor($object)
{
if (is_object($object)) {
$object = $object->getKey();
return $object->getKey();
} elseif (is_array($object)) {
return $object['id'];
} elseif (is_int($object)) {
return $object;
}

if (is_array($object)) {
$object = $object['id'];
}

return $object;
throw new InvalidArgumentException(
'getIdFor function only accepts an integer, a Model object or an array with an "id" key'
);
}
}
1 change: 1 addition & 0 deletions tests/LaratrustUserAbilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,5 +711,6 @@ function isExceptionThrown(
$this->assertFalse(isExceptionThrown($user, ['RoleA'], ['manage_a'], ['return_type' => 'array']));
$this->assertFalse(isExceptionThrown($user, ['RoleA'], ['manage_a'], ['return_type' => 'both']));
$this->assertTrue(isExceptionThrown($user, ['RoleA'], ['manage_a'], ['return_type' => 'potato']));
$this->assertTrue(isExceptionThrown($user, ['RoleA'], ['manage_a'], ['validate_all' => 'potato']));
}
}
96 changes: 93 additions & 3 deletions tests/LaratrustUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

use Laratrust\Contracts\LaratrustUserInterface;
use Laratrust\Traits\LaratrustUserTrait;
use Illuminate\Cache\ArrayStore;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cache;
use Laratrust\Permission;
use Laratrust\Role;
use Mockery as m;

class LaratrustUserTest extends UserTest
Expand Down Expand Up @@ -112,6 +109,7 @@ public function testHasRole()
| Assertion
|------------------------------------------------------------
*/
$this->assertTrue($user->hasRole([]));
$this->assertTrue($user->hasRole('RoleA'));
$this->assertTrue($user->hasRole('RoleB'));
$this->assertFalse($user->hasRole('RoleC'));
Expand Down Expand Up @@ -170,6 +168,7 @@ public function testCan()
| Assertion
|------------------------------------------------------------
*/
$this->assertTrue($user->can([]));
$this->assertTrue($user->can('manage_a'));
$this->assertTrue($user->can('manage_b'));
$this->assertTrue($user->can('manage_c'));
Expand Down Expand Up @@ -475,6 +474,7 @@ public function testSyncRoles()
$this->assertInstanceOf('HasRoleUser', $user->syncRoles($rolesIds));
}


public function testAttachPermission()
{
/*
Expand Down Expand Up @@ -521,6 +521,8 @@ public function testAttachPermission()
$this->assertInstanceOf('HasRoleUser', $result);
$result = $user->attachPermission(3);
$this->assertInstanceOf('HasRoleUser', $result);
$this->setExpectedException(InvalidArgumentException::class);
$user->attachPermission(true);
}

public function testDetachPermission()
Expand Down Expand Up @@ -637,6 +639,80 @@ public function testDetachPermissions()
$this->assertInstanceOf('HasRoleUser', $result);
}


public function testDetachAllPermissions()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$permissionA = $this->mockRole('PermissionA');
$permissionB = $this->mockRole('PermissionB');

$user = m::mock('HasRoleUser')->makePartial();
$user->permissions = [$permissionA, $permissionB];

$relationship = m::mock('BelongsToMany');

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
Config::shouldReceive('get')->with('laratrust.permission')->once()->andReturn('App\Permission');
Config::shouldReceive('get')->with('laratrust.permission_user_table')->once()->andReturn('permission_user');
Config::shouldReceive('get')->with('laratrust.user_foreign_key')->once()->andReturn('user_id');
Config::shouldReceive('get')->with('laratrust.permission_foreign_key')->once()->andReturn('permission_id');

$relationship->shouldReceive('get')
->andReturn($user->permissions)->once();

$user->shouldReceive('belongsToMany')
->andReturn($relationship)->once();

$user->shouldReceive('detachPermission')->twice();

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$user->detachPermissions();
}

public function testSyncPermissions()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$permissionsIds = [1, 2, 3];
$user = m::mock('HasRoleUser')->makePartial();

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$user->shouldReceive('permissions')
->andReturn($user);
$user->shouldReceive('sync')
->with($permissionsIds)
->once()->ordered();

Cache::shouldReceive('forget')
->twice();

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertInstanceOf('HasRoleUser', $user->syncPermissions($permissionsIds));
}

public function testUserOwnsaPostModel()
{
$user = m::mock('HasRoleUser')->makePartial();
Expand All @@ -662,6 +738,20 @@ public function testUserOwnsaPostModelCustomKey()
$this->assertTrue($user->owns($post, 'UserId'));
$this->assertFalse($user->owns($post2, 'UserId'));
}

public function testScopeWhereRoleIs()
{
$query = m::mock();

$query->shouldReceive('whereHas')
->with('roles', m::any())
->once()
->andReturn($query);

$user = m::mock('HasRoleUser')->makePartial();

$this->assertInstanceOf(get_class($query), $user->scopeWhereRoleIs($query, 'admin'));
}
}

class HasRoleUser extends Model implements LaratrustUserInterface
Expand Down

0 comments on commit 2e4fe7b

Please sign in to comment.