-
-
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 events to the attaching/detaching methods.
- Fix #151
- Loading branch information
1 parent
d4c0242
commit 6342410
Showing
6 changed files
with
423 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Laratrust\Traits; | ||
|
||
trait LaratrustHasEvents | ||
{ | ||
/** | ||
* Fire the given event for the model. | ||
* | ||
* @param string $event | ||
* @param array $payload | ||
* @return mixed | ||
*/ | ||
protected function fireLaratrustEvent($event, array $payload) | ||
{ | ||
if (! isset(static::$dispatcher)) { | ||
return true; | ||
} | ||
|
||
return static::$dispatcher->fire( | ||
"laratrust.{$event}: ".static::class, | ||
$payload | ||
); | ||
} | ||
|
||
/** | ||
* Register a laratrust event with the dispatcher. | ||
* | ||
* @param string $event | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function registerLaratrustEvent($event, $callback) | ||
{ | ||
if (isset(static::$dispatcher)) { | ||
$name = static::class; | ||
|
||
static::$dispatcher->listen("laratrust.{$event}: {$name}", $callback); | ||
} | ||
} | ||
|
||
/** | ||
* Register a role attached laratrust event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function roleAttached($callback) | ||
{ | ||
static::registerLaratrustEvent('role.attached', $callback); | ||
} | ||
|
||
/** | ||
* Register a role detached laratrust event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function roleDetached($callback) | ||
{ | ||
static::registerLaratrustEvent('role.detached', $callback); | ||
} | ||
|
||
/** | ||
* Register a permission attached laratrust event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function permissionAttached($callback) | ||
{ | ||
static::registerLaratrustEvent('permission.attached', $callback); | ||
} | ||
|
||
/** | ||
* Register a permission detached laratrust event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function permissionDetached($callback) | ||
{ | ||
static::registerLaratrustEvent('permission.detached', $callback); | ||
} | ||
|
||
/** | ||
* Register a role synced laratrust event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function roleSynced($callback) | ||
{ | ||
static::registerLaratrustEvent('role.synced', $callback); | ||
} | ||
|
||
/** | ||
* Register a permission synced laratrust event with the dispatcher. | ||
* | ||
* @param \Closure|string $callback | ||
* @return void | ||
*/ | ||
public static function permissionSynced($callback) | ||
{ | ||
static::registerLaratrustEvent('permission.synced', $callback); | ||
} | ||
} |
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,69 @@ | ||
<?php | ||
|
||
namespace Laratrust\Tests; | ||
|
||
use Mockery as m; | ||
|
||
class LaratrustEventsTestCase extends LaratrustTestCase | ||
{ | ||
protected $dispatcher; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->migrate(); | ||
$this->dispatcher = m::mock('\Illuminate\Events\Dispatcher')->makePartial(); | ||
$this->app['config']->set('laratrust.use_teams', true); | ||
} | ||
|
||
/** | ||
* Listen to a Laratrust event. | ||
* | ||
* @param string $event | ||
* @return void | ||
*/ | ||
protected function listenTo($event, $modelClass) | ||
{ | ||
$method = \Illuminate\Support\Str::camel(str_replace('.', ' ', $event)); | ||
|
||
$modelClass::{$method}(function ($user, $roleId) { | ||
return 'test'; | ||
}); | ||
} | ||
|
||
/** | ||
* Assert that the dispatcher has listeners for the given event. | ||
* | ||
* @param string $event | ||
* @return void | ||
*/ | ||
protected function assertHasListenersFor($event, $modelClass) | ||
{ | ||
$eventName = "laratrust.{$event}: {$modelClass}"; | ||
$dispatcher = $modelClass::getEventDispatcher(); | ||
|
||
$this->assertTrue($dispatcher->hasListeners($eventName)); | ||
$this->assertCount(1, $dispatcher->getListeners($eventName)); | ||
$this->assertEquals('test', $dispatcher->fire($eventName, ['user', 'an_id', null])[0]); | ||
} | ||
|
||
/** | ||
* Assert the dispatcher fires the fire event with the given data. | ||
* | ||
* @param string $event | ||
* @param array $payload | ||
* @param string $model | ||
* @return void | ||
*/ | ||
protected function dispatcherShouldFire($event, array $payload, $modelClass) | ||
{ | ||
$this->dispatcher->shouldReceive('fire') | ||
->with( | ||
"laratrust.{$event}: {$modelClass}", | ||
$payload | ||
) | ||
->andReturn(null) | ||
->once()->ordered(); | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
|
||
namespace Laratrust\Tests; | ||
|
||
use Mockery as m; | ||
use Laratrust\Tests\Models\Role; | ||
use Laratrust\Tests\Models\User; | ||
use Laratrust\Tests\LaratrustTestCase; | ||
use Laratrust\Tests\Models\Permission; | ||
|
||
class LaratrustRoleEventsTest extends LaratrustEventsTestCase | ||
{ | ||
protected $role; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->role = Role::create(['name' => 'role']); | ||
|
||
} | ||
|
||
public function testCanListenToThePermissionAttachedEvent() | ||
{ | ||
$this->listenTo('permission.attached', Role::class); | ||
|
||
$this->assertHasListenersFor('permission.attached', Role::class); | ||
} | ||
|
||
public function testCanListenToThePermissionDetachedEvent() | ||
{ | ||
$this->listenTo('permission.detached', Role::class); | ||
|
||
$this->assertHasListenersFor('permission.detached', Role::class); | ||
} | ||
|
||
public function testCanListenToThePermissionSyncedEvent() | ||
{ | ||
$this->listenTo('permission.synced', Role::class); | ||
|
||
$this->assertHasListenersFor('permission.synced', Role::class); | ||
} | ||
|
||
public function testAnEventIsFiredWhenPermissionIsAttachedToRole() | ||
{ | ||
$permission = Permission::create(['name' => 'permission']); | ||
|
||
Role::setEventDispatcher($this->dispatcher); | ||
|
||
$this->dispatcherShouldFire('permission.attached', [$this->role, $permission->id], Role::class); | ||
|
||
$this->role->attachPermission($permission); | ||
} | ||
|
||
public function testAnEventIsFiredWhenPermissionIsDetachedFromRole() | ||
{ | ||
$permission = Permission::create(['name' => 'permission']); | ||
$this->role->attachPermission($permission); | ||
|
||
Role::setEventDispatcher($this->dispatcher); | ||
|
||
$this->dispatcherShouldFire('permission.detached', [$this->role, $permission->id], Role::class); | ||
|
||
$this->role->detachPermission($permission); | ||
} | ||
|
||
public function testAnEventIsFiredWhenPermissionsAreSynced() | ||
{ | ||
$permission = Permission::create(['name' => 'permission']); | ||
$this->role->attachPermission($permission); | ||
|
||
Role::setEventDispatcher($this->dispatcher); | ||
|
||
$this->dispatcherShouldFire('permission.synced', [ | ||
$this->role, | ||
[ | ||
'attached' => [], 'detached' => [$permission->id], 'updated' => [], | ||
] | ||
], Role::class); | ||
|
||
$this->role->syncPermissions([]); | ||
} | ||
} |
Oops, something went wrong.