Skip to content

Commit

Permalink
Added some test scenarios and changes some class names
Browse files Browse the repository at this point in the history
- Added cases for the new user-permissions feature
- Added the Middleware prefix to all the middleware tests.
- Changed phpunit config file
- Some changes in the composer.json
  • Loading branch information
santigarcor committed Feb 14, 2017
1 parent 7a5db26 commit 9f7d507
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 24 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ php:
- 5.5.9
- 5.6
- 7.0
- hhvm

env:
global:
- setup=basic

matrix:
allow_failures:
- php: hhvm
fast_finish: true
include:
- php: 5.5.9
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
"kkszymanowski/traitor": "^0.2.0"
},
"require-dev": {
"phpunit/phpunit": "~4.1",
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9",
"illuminate/database": "~5.0",
"squizlabs/php_codesniffer": "2.*"
"illuminate/database": "~5.0"
},
"autoload": {
"classmap": [
Expand All @@ -56,5 +55,9 @@
"tests/UserTest.php"
]
},
"config": {
"sort-packages": true
},
"prefer-stable": true,
"minimum-stability": "dev"
}
10 changes: 6 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
<testsuite name="Laratrust Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
90 changes: 79 additions & 11 deletions tests/LaratrustUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,43 @@ public function testRoles()
$this->assertSame($belongsToMany, $user->roles());
}

public function testPermissions()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$belongsToMany = new stdClass();
$user = m::mock('HasRoleUser')->makePartial();

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$user->shouldReceive('belongsToMany')
->with('permission_table_name', 'assigned_permissions_table_name', 'user_id', 'permission_id')
->andReturn($belongsToMany)
->once();

Config::shouldReceive('get')->once()->with('laratrust.permission')
->andReturn('permission_table_name');
Config::shouldReceive('get')->once()->with('laratrust.permission_user_table')
->andReturn('assigned_permissions_table_name');
Config::shouldReceive('get')->once()->with('laratrust.user_foreign_key')
->andReturn('user_id');
Config::shouldReceive('get')->once()->with('laratrust.permission_foreign_key')
->andReturn('permission_id');

/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertSame($belongsToMany, $user->permissions());
}

public function testHasRole()
{
/*
Expand Down Expand Up @@ -95,6 +132,7 @@ public function testCan()
$permA = $this->mockPermission('manage_a');
$permB = $this->mockPermission('manage_b');
$permC = $this->mockPermission('manage_c');
$permD = $this->mockPermission('manage_d');

$roleA = $this->mockRole('RoleA');
$roleB = $this->mockRole('RoleB');
Expand All @@ -104,16 +142,28 @@ public function testCan()

$user = new HasRoleUser();
$user->roles = [$roleA, $roleB];
$user->permissions = [$permD];

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$roleA->shouldReceive('cachedPermissions')->times(11)->andReturn($roleA->perms);
$roleB->shouldReceive('cachedPermissions')->times(7)->andReturn($roleB->perms);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(22)->andReturn('1440');
Cache::shouldReceive('remember')->times(22)->andReturn($user->roles);
$roleA->shouldReceive('cachedPermissions')->times(13)->andReturn($roleA->perms);
$roleB->shouldReceive('cachedPermissions')->times(8)->andReturn($roleB->perms);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(28)->andReturn('1440');
Cache::shouldReceive('remember')
->with(
"laratrust_permissions_for_user_{$user->getKey()}",
1440,
m::any()
)->times(15)->andReturn($user->permissions);
Cache::shouldReceive('remember')
->with(
"laratrust_roles_for_user_{$user->getKey()}",
1440,
m::any()
)->times(13)->andReturn($user->roles);

/*
|------------------------------------------------------------
Expand All @@ -123,12 +173,14 @@ public function testCan()
$this->assertTrue($user->can('manage_a'));
$this->assertTrue($user->can('manage_b'));
$this->assertTrue($user->can('manage_c'));
$this->assertFalse($user->can('manage_d'));
$this->assertTrue($user->can('manage_d'));
$this->assertFalse($user->can('manage_e'));

$this->assertTrue($user->can(['manage_a', 'manage_b', 'manage_c']));
$this->assertTrue($user->can(['manage_a', 'manage_b', 'manage_d']));
$this->assertFalse($user->can(['manage_a', 'manage_b', 'manage_d'], true));
$this->assertFalse($user->can(['manage_d', 'manage_e']));
$this->assertTrue($user->can(['manage_a', 'manage_b', 'manage_d'], true));
$this->assertFalse($user->can(['manage_a', 'manage_b', 'manage_e'], true));
$this->assertFalse($user->can(['manage_e', 'manage_f']));
}


Expand All @@ -142,22 +194,36 @@ public function testCanWithPlaceholderSupport()
$permA = $this->mockPermission('admin.posts');
$permB = $this->mockPermission('admin.pages');
$permC = $this->mockPermission('admin.users');
$permD = $this->mockPermission('config.things');

$role = $this->mockRole('Role');

$role->perms = [$permA, $permB, $permC];

$user = new HasRoleUser();
$user->roles = [$role];
$user->permissions = [$permD];

/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$role->shouldReceive('cachedPermissions')->times(6)->andReturn($role->perms);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(12)->andReturn('1440');
Cache::shouldReceive('remember')->times(12)->andReturn($user->roles);
Config::shouldReceive('get')->with('cache.ttl', 60)->times(13)->andReturn('1440');
Cache::shouldReceive('remember')
->with(
"laratrust_permissions_for_user_{$user->getKey()}",
1440,
m::any()
)->times(7)->andReturn($user->permissions);
Cache::shouldReceive('remember')
->with(
"laratrust_roles_for_user_{$user->getKey()}",
1440,
m::any()
)->times(6)->andReturn($user->roles);


/*
|------------------------------------------------------------
Expand All @@ -170,6 +236,7 @@ public function testCanWithPlaceholderSupport()
$this->assertFalse($user->can('admin.config'));

$this->assertTrue($user->can(['admin.*']));
$this->assertTrue($user->can(['config.*']));
$this->assertFalse($user->can(['site.*']));
}

Expand Down Expand Up @@ -574,10 +641,10 @@ public function testUserOwnsaPostModel()
{
$user = m::mock('HasRoleUser')->makePartial();
$post = new stdClass();
$post->mockery_13__has_role_user_id = $user->getKey();
$post->mockery_14__has_role_user_id = $user->getKey();

$post2 = new stdClass();
$post2->mockery_13__has_role_user_id = 9;
$post2->mockery_14__has_role_user_id = 9;

$this->assertTrue($user->owns($post));
$this->assertFalse($user->owns($post2));
Expand All @@ -602,6 +669,7 @@ class HasRoleUser extends Model implements LaratrustUserInterface
use LaratrustUserTrait;

public $roles;
public $permissions;
public $primaryKey;

public function __construct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Laratrust\Middleware\LaratrustAbility;
use Mockery as m;

class LaratrustAbilityTest extends MiddlewareTest
class MiddlewareLaratrustAbilityTest extends MiddlewareTest
{
public function testHandle_IsGuestWithNoAbility_ShouldAbort403()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Laratrust\Middleware\LaratrustPermission;
use Mockery as m;

class LaratrustPermissionTest extends MiddlewareTest
class MiddlewareLaratrustPermissionTest extends MiddlewareTest
{
public function testHandle_IsGuestWithNoPermission_ShouldAbort403()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Laratrust\Middleware\LaratrustRole;
use Mockery as m;

class LaratrustRoleTest extends MiddlewareTest
class MiddlewareLaratrustRoleTest extends MiddlewareTest
{
public function testHandle_IsGuestWithMismatchingRole_ShouldAbort403()
{
Expand Down

0 comments on commit 9f7d507

Please sign in to comment.