-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to use Laravel's cache or not
- Add option to use Laravel's cache #201. - Reduce cache payload size.
- Loading branch information
1 parent
a8bbdd0
commit 227c89d
Showing
5 changed files
with
148 additions
and
6 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
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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Laratrust\Test; | ||
|
||
use Mockery as m; | ||
use Laratrust\Tests\Models\Role; | ||
use Laratrust\Tests\Models\Team; | ||
use Laratrust\Tests\Models\User; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Config; | ||
use Laratrust\Tests\LaratrustTestCase; | ||
use Laratrust\Tests\Models\Permission; | ||
use Laratrust\Tests\Models\OwnableObject; | ||
|
||
class LaratrustCacheTest extends LaratrustTestCase | ||
{ | ||
protected $user; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->migrate(); | ||
} | ||
|
||
public function testUserCanDisableTheRolesAndPermissionsCaching() | ||
{ | ||
/* | ||
|------------------------------------------------------------ | ||
| Set | ||
|------------------------------------------------------------ | ||
*/ | ||
$team = Team::create(['name' => 'team_a']); | ||
$user = User::create(['name' => 'test', 'email' => 'test@test.com']); | ||
$role = Role::create(['name' => 'role_a']) | ||
->attachPermissions([ | ||
Permission::create(['name' => 'permission_a']), | ||
Permission::create(['name' => 'permission_b']), | ||
Permission::create(['name' => 'permission_c']), | ||
]); | ||
|
||
$user->roles()->attach($role->id); | ||
|
||
$user->permissions()->attach([ | ||
Permission::create(['name' => 'permission_d'])->id => ['team_id' => $team->id ], | ||
Permission::create(['name' => 'permission_e'])->id => ['team_id' => $team->id], | ||
]); | ||
|
||
/* | ||
|------------------------------------------------------------ | ||
| User Assertion | ||
|------------------------------------------------------------ | ||
*/ | ||
// With cache | ||
$this->app['config']->set('laratrust.use_cache', true); | ||
$this->assertInternalType('array', $user->cachedRoles()); | ||
$this->assertEquals($user->roles()->get()->toArray(), $user->cachedRoles()); | ||
|
||
$this->assertInternalType('array', $user->cachedPermissions()); | ||
$this->assertEquals($user->permissions()->get()->toArray(), $user->cachedPermissions()); | ||
|
||
// Without cache | ||
$this->app['config']->set('laratrust.use_cache', false); | ||
$this->assertInstanceOf('Illuminate\Support\Collection', $user->cachedRoles()); | ||
$this->assertEquals($user->roles()->get(), $user->cachedRoles()); | ||
|
||
$this->assertInstanceOf('Illuminate\Support\Collection', $user->cachedPermissions()); | ||
$this->assertEquals($user->permissions()->get(), $user->cachedPermissions()); | ||
|
||
/* | ||
|------------------------------------------------------------ | ||
| Role Assertion | ||
|------------------------------------------------------------ | ||
*/ | ||
// With cache | ||
$this->app['config']->set('laratrust.use_cache', true); | ||
$this->assertInternalType('array', $role->cachedPermissions()); | ||
$this->assertEquals($role->permissions()->get()->toArray(), $role->cachedPermissions()); | ||
|
||
// Without cache | ||
$this->app['config']->set('laratrust.use_cache', false); | ||
$this->assertInstanceOf('Illuminate\Support\Collection', $role->cachedPermissions()); | ||
$this->assertEquals($role->permissions()->get(), $role->cachedPermissions()); | ||
} | ||
} |