Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
875b402d
Unverified
Commit
875b402d
authored
Jul 12, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Creating and triggering new (hookable) events for Role entity
parent
0121f995
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
133 additions
and
2 deletions
+133
-2
RoleIsCreating.php
Modules/User/Events/RoleIsCreating.php
+10
-0
RoleIsUpdating.php
Modules/User/Events/RoleIsUpdating.php
+29
-0
RoleWasCreated.php
Modules/User/Events/RoleWasCreated.php
+18
-0
SentinelRoleRepository.php
...les/User/Repositories/Sentinel/SentinelRoleRepository.php
+11
-2
SentinelRoleRepositoryTest.php
Modules/User/Tests/SentinelRoleRepositoryTest.php
+65
-0
No files found.
Modules/User/Events/RoleIsCreating.php
0 → 100644
View file @
875b402d
<?php
namespace
Modules\User\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
class
RoleIsCreating
extends
AbstractEntityHook
implements
EntityIsChanging
{
}
Modules/User/Events/RoleIsUpdating.php
0 → 100644
View file @
875b402d
<?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
;
}
}
Modules/User/Events/RoleWasCreated.php
0 → 100644
View file @
875b402d
<?php
namespace
Modules\User\Events
;
use
Cartalyst\Sentinel\Roles\RoleInterface
;
class
RoleWasCreated
{
/**
* @var RoleInterface
*/
public
$role
;
public
function
__construct
(
RoleInterface
$role
)
{
$this
->
role
=
$role
;
}
}
Modules/User/Repositories/Sentinel/SentinelRoleRepository.php
View file @
875b402d
...
...
@@ -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
));
...
...
Modules/User/Tests/SentinelRoleRepositoryTest.php
View file @
875b402d
...
...
@@ -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
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment