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
0121f995
Unverified
Commit
0121f995
authored
Jul 12, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Creating and triggering new (hookable) events for menu item entity
parent
60de6a52
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
169 additions
and
2 deletions
+169
-2
MenuItemIsCreating.php
Modules/Menu/Events/MenuItemIsCreating.php
+10
-0
MenuItemIsUpdating.php
Modules/Menu/Events/MenuItemIsUpdating.php
+29
-0
MenuItemWasUpdated.php
Modules/Menu/Events/MenuItemWasUpdated.php
+18
-0
EloquentMenuItemRepository.php
...Menu/Repositories/Eloquent/EloquentMenuItemRepository.php
+9
-2
EloquentMenuItemRepositoryTest.php
Modules/Menu/Tests/EloquentMenuItemRepositoryTest.php
+103
-0
No files found.
Modules/Menu/Events/MenuItemIsCreating.php
0 → 100644
View file @
0121f995
<?php
namespace
Modules\Menu\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
class
MenuItemIsCreating
extends
AbstractEntityHook
implements
EntityIsChanging
{
}
Modules/Menu/Events/MenuItemIsUpdating.php
0 → 100644
View file @
0121f995
<?php
namespace
Modules\Menu\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
use
Modules\Menu\Entities\Menuitem
;
class
MenuItemIsUpdating
extends
AbstractEntityHook
implements
EntityIsChanging
{
/**
* @var Menuitem
*/
private
$menuItem
;
public
function
__construct
(
Menuitem
$menuItem
,
$attributes
)
{
parent
::
__construct
(
$attributes
);
$this
->
menuItem
=
$menuItem
;
}
/**
* @return Menuitem
*/
public
function
getMenuItem
()
{
return
$this
->
menuItem
;
}
}
Modules/Menu/Events/MenuItemWasUpdated.php
0 → 100644
View file @
0121f995
<?php
namespace
Modules\Menu\Events
;
use
Modules\Menu\Entities\Menuitem
;
class
MenuItemWasUpdated
{
/**
* @var Menuitem
*/
public
$menuItem
;
public
function
__construct
(
Menuitem
$menuItem
)
{
$this
->
menuItem
=
$menuItem
;
}
}
Modules/Menu/Repositories/Eloquent/EloquentMenuItemRepository.php
View file @
0121f995
...
...
@@ -6,14 +6,18 @@ use Illuminate\Database\Eloquent\Builder;
use
Illuminate\Support\Facades\App
;
use
Illuminate\Support\Facades\DB
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Menu\Events\MenuItemIsCreating
;
use
Modules\Menu\Events\MenuItemIsUpdating
;
use
Modules\Menu\Events\MenuItemWasCreated
;
use
Modules\Menu\Events\MenuItemWasUpdated
;
use
Modules\Menu\Repositories\MenuItemRepository
;
class
EloquentMenuItemRepository
extends
EloquentBaseRepository
implements
MenuItemRepository
{
public
function
create
(
$data
)
{
$menuItem
=
$this
->
model
->
create
(
$data
);
event
(
$event
=
new
MenuItemIsCreating
(
$data
));
$menuItem
=
$this
->
model
->
create
(
$event
->
getAttributes
());
event
(
new
MenuItemWasCreated
(
$menuItem
));
...
...
@@ -22,7 +26,10 @@ class EloquentMenuItemRepository extends EloquentBaseRepository implements MenuI
public
function
update
(
$menuItem
,
$data
)
{
$menuItem
->
update
(
$data
);
event
(
$event
=
new
MenuItemIsUpdating
(
$menuItem
,
$data
));
$menuItem
->
update
(
$event
->
getAttributes
());
event
(
new
MenuItemWasUpdated
(
$menuItem
));
return
$menuItem
;
}
...
...
Modules/Menu/Tests/EloquentMenuItemRepositoryTest.php
View file @
0121f995
...
...
@@ -2,6 +2,12 @@
namespace
Modules\Menu\Tests
;
use
Illuminate\Support\Facades\Event
;
use
Modules\Menu\Events\MenuItemIsCreating
;
use
Modules\Menu\Events\MenuItemIsUpdating
;
use
Modules\Menu\Events\MenuItemWasCreated
;
use
Modules\Menu\Events\MenuItemWasUpdated
;
class
EloquentMenuItemRepositoryTest
extends
BaseMenuTest
{
public
function
setUp
()
...
...
@@ -57,4 +63,101 @@ class EloquentMenuItemRepositoryTest extends BaseMenuTest
$this
->
menuItem
->
destroy
(
$item2
);
$this
->
assertEquals
(
7
,
$this
->
menuItem
->
all
()
->
count
());
}
/** @test */
public
function
it_triggers_event_when_menu_item_was_created
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$item1
=
$this
->
createMenuItemForMenu
(
$menu
->
id
,
0
);
Event
::
assertDispatched
(
MenuItemWasCreated
::
class
,
function
(
$e
)
use
(
$item1
)
{
return
$e
->
menuItem
->
id
===
$item1
->
id
;
});
}
/** @test */
public
function
it_triggers_event_when_menu_item_is_creating
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$item1
=
$this
->
createMenuItemForMenu
(
$menu
->
id
,
0
);
Event
::
assertDispatched
(
MenuItemIsCreating
::
class
,
function
(
$e
)
use
(
$item1
)
{
return
$e
->
getAttribute
(
'target'
)
===
$item1
->
target
;
});
}
/** @test */
public
function
it_can_change_data_when_it_is_creating_event
()
{
Event
::
listen
(
MenuItemIsCreating
::
class
,
function
(
MenuItemIsCreating
$event
)
{
$event
->
setAttributes
([
'target'
=>
'_blank'
,
'en'
=>
[
'title'
=>
'My Title'
,
],
]);
});
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$item
=
$this
->
createMenuItemForMenu
(
$menu
->
id
,
0
);
$this
->
assertEquals
(
'_blank'
,
$item
->
target
);
$this
->
assertEquals
(
'My Title'
,
$item
->
translate
(
'en'
)
->
title
);
}
/** @test */
public
function
it_triggers_event_when_menu_item_is_updated
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$item1
=
$this
->
createMenuItemForMenu
(
$menu
->
id
,
0
);
$this
->
menuItem
->
update
(
$item1
,
[]);
Event
::
assertDispatched
(
MenuItemWasUpdated
::
class
,
function
(
$e
)
use
(
$item1
)
{
return
$e
->
menuItem
->
id
===
$item1
->
id
;
});
}
/** @test */
public
function
it_triggers_event_when_menu_item_is_updating
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$item1
=
$this
->
createMenuItemForMenu
(
$menu
->
id
,
0
);
$this
->
menuItem
->
update
(
$item1
,
[]);
Event
::
assertDispatched
(
MenuItemIsUpdating
::
class
,
function
(
$e
)
use
(
$item1
)
{
return
$e
->
getMenuItem
()
->
id
===
$item1
->
id
;
});
}
/** @test */
public
function
it_can_change_data_before_updating_menu_item
()
{
Event
::
listen
(
MenuItemIsUpdating
::
class
,
function
(
MenuItemIsUpdating
$event
)
{
$event
->
setAttributes
([
'target'
=>
'_blank'
,
'en'
=>
[
'title'
=>
'My Title'
,
],
]);
});
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$item1
=
$this
->
createMenuItemForMenu
(
$menu
->
id
,
0
);
$this
->
menuItem
->
update
(
$item1
,
[
'en'
=>
[
'title'
=>
'This one!'
],
]);
$this
->
assertEquals
(
'My Title'
,
$item1
->
translate
(
'en'
)
->
title
);
}
}
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