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
57b11873
Unverified
Commit
57b11873
authored
Jul 10, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trigger an event before a user is created, allowing customising its data via listeners
parent
e47cd4e5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
1 deletion
+102
-1
UserIsCreating.php
Modules/User/Events/UserIsCreating.php
+34
-0
SentinelUserRepository.php
...les/User/Repositories/Sentinel/SentinelUserRepository.php
+4
-1
SentinelUserRepositoryTest.php
Modules/User/Tests/SentinelUserRepositoryTest.php
+64
-0
No files found.
Modules/User/Events/UserIsCreating.php
0 → 100644
View file @
57b11873
<?php
namespace
Modules\User\Events
;
final
class
UserIsCreating
{
/**
* @var array
*/
private
$attributes
;
public
$original
;
public
function
__construct
(
array
$attributes
)
{
$this
->
attributes
=
$attributes
;
$this
->
original
=
$attributes
;
}
/**
* @return array
*/
public
function
getAttributes
()
{
return
$this
->
attributes
;
}
/**
* @param array $attributes
*/
public
function
setAttributes
(
array
$attributes
)
{
$this
->
attributes
=
array_merge
(
$this
->
attributes
,
$attributes
);
}
}
Modules/User/Repositories/Sentinel/SentinelUserRepository.php
View file @
57b11873
...
...
@@ -7,6 +7,7 @@ use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use
Illuminate\Support\Facades\Hash
;
use
Modules\User\Entities\Sentinel\User
;
use
Modules\User\Events\UserHasRegistered
;
use
Modules\User\Events\UserIsCreating
;
use
Modules\User\Events\UserIsUpdating
;
use
Modules\User\Events\UserWasCreated
;
use
Modules\User\Events\UserWasUpdated
;
...
...
@@ -48,7 +49,9 @@ class SentinelUserRepository implements UserRepository
public
function
create
(
array
$data
,
$activated
=
false
)
{
$this
->
hashPassword
(
$data
);
$user
=
$this
->
user
->
create
((
array
)
$data
);
event
(
$event
=
new
UserIsCreating
(
$data
));
$user
=
$this
->
user
->
create
(
$event
->
getAttributes
());
if
(
$activated
)
{
$this
->
activateUser
(
$user
);
...
...
Modules/User/Tests/SentinelUserRepositoryTest.php
View file @
57b11873
...
...
@@ -5,6 +5,7 @@ namespace Modules\User\Tests;
use
Illuminate\Support\Facades\Event
;
use
Modules\User\Entities\Sentinel\User
;
use
Modules\User\Events\UserHasRegistered
;
use
Modules\User\Events\UserIsCreating
;
use
Modules\User\Events\UserIsUpdating
;
use
Modules\User\Events\UserWasCreated
;
use
Modules\User\Events\UserWasUpdated
;
...
...
@@ -44,6 +45,69 @@ class SentinelUserRepositoryTest extends BaseUserTestCase
$this
->
assertCount
(
1
,
$this
->
user
->
all
());
}
/** @test */
public
function
it_fires_event_when_user_is_creating
()
{
Event
::
fake
();
$user
=
$this
->
user
->
create
([
'email'
=>
'n.widart@gmail.com'
,
'password'
=>
'demo1234'
,
]);
Event
::
assertDispatched
(
UserIsCreating
::
class
,
function
(
$e
)
use
(
$user
)
{
return
$e
->
getAttributes
()[
'email'
]
===
$user
->
email
;
});
}
/** @test */
public
function
it_can_change_data_when_it_is_creating_event
()
{
Event
::
listen
(
UserIsCreating
::
class
,
function
(
UserIsCreating
$event
)
{
$event
->
setAttributes
([
'email'
=>
'john@doe.com'
]);
});
$user
=
$this
->
user
->
create
([
'email'
=>
'n.widart@gmail.com'
,
'password'
=>
'demo1234'
,
]);
$this
->
assertEquals
(
'john@doe.com'
,
$user
->
email
);
}
/** @test */
public
function
it_can_change_the_data_multiple_times
()
{
Event
::
listen
(
UserIsCreating
::
class
,
function
(
UserIsCreating
$event
)
{
$event
->
setAttributes
([
'email'
=>
'john@doe.com'
]);
});
Event
::
listen
(
UserIsCreating
::
class
,
function
(
UserIsCreating
$event
)
{
$event
->
setAttributes
([
'email'
=>
'jane@doe.com'
]);
});
$user
=
$this
->
user
->
create
([
'email'
=>
'n.widart@gmail.com'
,
'password'
=>
'demo1234'
,
]);
$this
->
assertEquals
(
'jane@doe.com'
,
$user
->
email
);
}
/** @test */
public
function
it_makes_sure_the_event_contains_original_attributes
()
{
Event
::
fake
();
$this
->
user
->
create
([
'email'
=>
'n.widart@gmail.com'
,
'password'
=>
'demo1234'
,
]);
Event
::
assertDispatched
(
UserIsCreating
::
class
,
function
(
$e
)
{
return
$e
->
original
[
'email'
]
===
'n.widart@gmail.com'
;
});
}
/** @test */
public
function
it_fires_event_when_user_created
()
{
...
...
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