Creating and triggering new (hookable) events for Role entity

parent 0121f995
<?php
namespace Modules\User\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging;
class RoleIsCreating extends AbstractEntityHook implements EntityIsChanging
{
}
<?php
namespace Modules\User\Events;
use Cartalyst\Sentinel\Roles\RoleInterface;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging;
class RoleIsUpdating extends AbstractEntityHook implements EntityIsChanging
{
/**
* @var RoleInterface
*/
private $role;
public function __construct(RoleInterface $role, $attributes)
{
$this->role = $role;
parent::__construct($attributes);
}
/**
* @return RoleInterface
*/
public function getRole()
{
return $this->role;
}
}
<?php
namespace Modules\User\Events;
use Cartalyst\Sentinel\Roles\RoleInterface;
class RoleWasCreated
{
/**
* @var RoleInterface
*/
public $role;
public function __construct(RoleInterface $role)
{
$this->role = $role;
}
}
......@@ -3,6 +3,9 @@
namespace Modules\User\Repositories\Sentinel;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Modules\User\Events\RoleIsCreating;
use Modules\User\Events\RoleIsUpdating;
use Modules\User\Events\RoleWasCreated;
use Modules\User\Events\RoleWasUpdated;
use Modules\User\Repositories\RoleRepository;
......@@ -33,7 +36,12 @@ class SentinelRoleRepository implements RoleRepository
*/
public function create($data)
{
return $this->role->create($data);
event($event = new RoleIsCreating($data));
$role = $this->role->create($event->getAttributes());
event(new RoleWasCreated($role));
return $role;
}
/**
......@@ -56,8 +64,9 @@ class SentinelRoleRepository implements RoleRepository
{
$role = $this->role->find($id);
$role->fill($data);
event($event = new RoleIsUpdating($role, $data));
$role->fill($event->getAttributes());
$role->save();
event(new RoleWasUpdated($role));
......
......@@ -3,6 +3,9 @@
namespace Modules\User\Tests;
use Illuminate\Support\Facades\Event;
use Modules\User\Events\RoleIsCreating;
use Modules\User\Events\RoleIsUpdating;
use Modules\User\Events\RoleWasCreated;
use Modules\User\Events\RoleWasUpdated;
use Modules\User\Repositories\RoleRepository;
......@@ -27,6 +30,42 @@ class SentinelRoleRepositoryTest extends BaseUserTestCase
$this->assertCount(1, $this->role->all());
}
/** @test */
public function it_triggers_event_when_role_was_created()
{
Event::fake();
$role = $this->role->create(['name' => 'Admin', 'slug' => 'admin']);
Event::assertDispatched(RoleWasCreated::class, function ($e) use ($role) {
return $e->role->name === $role->name;
});
}
/** @test */
public function it_triggers_event_when_role_is_creating()
{
Event::fake();
$role = $this->role->create(['name' => 'Admin', 'slug' => 'admin']);
Event::assertDispatched(RoleIsCreating::class, function ($e) use ($role) {
return $e->getAttribute('name') === $role->name;
});
}
/** @test */
public function it_can_change_data_when_it_is_creating_event()
{
Event::listen(RoleIsCreating::class, function (RoleIsCreating $event) {
$event->setAttributes(['name' => 'BETTER']);
});
$role = $this->role->create(['name' => 'Admin', 'slug' => 'admin']);
$this->assertEquals('BETTER', $role->name);
}
/** @test */
public function it_finds_a_role_by_id()
{
......@@ -63,6 +102,32 @@ class SentinelRoleRepositoryTest extends BaseUserTestCase
});
}
/** @test */
public function it_fires_event_when_role_is_updating()
{
Event::fake();
$role = $this->role->create(['name' => 'Admin', 'slug' => 'admin']);
$this->role->update($role->id, ['name' => 'Better Admin']);
Event::assertDispatched(RoleIsUpdating::class, function ($e) use ($role) {
return $e->getRole()->id === $role->id;
});
}
/** @test */
public function it_can_change_data_before_role_is_updated()
{
Event::listen(RoleIsUpdating::class, function (RoleIsUpdating $event) {
$event->setAttributes(['name' => 'BETTER']);
});
$role = $this->role->create(['name' => 'Admin', 'slug' => 'admin']);
$role = $this->role->update($role->id, ['name' => 'Better Admin']);
$this->assertEquals('BETTER', $role->name);
}
/** @test */
public function it_deletes_a_role()
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment