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
64f7e413
Unverified
Commit
64f7e413
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 entity
parent
89a278c1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
161 additions
and
2 deletions
+161
-2
MenuIsCreating.php
Modules/Menu/Events/MenuIsCreating.php
+10
-0
MenuIsUpdating.php
Modules/Menu/Events/MenuIsUpdating.php
+29
-0
MenuWasUpdated.php
Modules/Menu/Events/MenuWasUpdated.php
+18
-0
EloquentMenuRepository.php
...les/Menu/Repositories/Eloquent/EloquentMenuRepository.php
+9
-2
BaseMenuTest.php
Modules/Menu/Tests/BaseMenuTest.php
+8
-0
EloquentMenuRepositoryTest.php
Modules/Menu/Tests/EloquentMenuRepositoryTest.php
+87
-0
No files found.
Modules/Menu/Events/MenuIsCreating.php
0 → 100644
View file @
64f7e413
<?php
namespace
Modules\Menu\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
class
MenuIsCreating
extends
AbstractEntityHook
implements
EntityIsChanging
{
}
Modules/Menu/Events/MenuIsUpdating.php
0 → 100644
View file @
64f7e413
<?php
namespace
Modules\Menu\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
use
Modules\Menu\Entities\Menu
;
class
MenuIsUpdating
extends
AbstractEntityHook
implements
EntityIsChanging
{
/**
* @var Menu
*/
private
$menu
;
public
function
__construct
(
Menu
$menu
,
$attributes
)
{
$this
->
menu
=
$menu
;
parent
::
__construct
(
$attributes
);
}
/**
* @return Menu
*/
public
function
getMenu
()
{
return
$this
->
menu
;
}
}
Modules/Menu/Events/MenuWasUpdated.php
0 → 100644
View file @
64f7e413
<?php
namespace
Modules\Menu\Events
;
use
Modules\Menu\Entities\Menu
;
class
MenuWasUpdated
{
/**
* @var Menu
*/
public
$menu
;
public
function
__construct
(
Menu
$menu
)
{
$this
->
menu
=
$menu
;
}
}
Modules/Menu/Repositories/Eloquent/EloquentMenuRepository.php
View file @
64f7e413
...
...
@@ -5,14 +5,18 @@ namespace Modules\Menu\Repositories\Eloquent;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Support\Facades\App
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Menu\Events\MenuIsCreating
;
use
Modules\Menu\Events\MenuIsUpdating
;
use
Modules\Menu\Events\MenuWasCreated
;
use
Modules\Menu\Events\MenuWasUpdated
;
use
Modules\Menu\Repositories\MenuRepository
;
class
EloquentMenuRepository
extends
EloquentBaseRepository
implements
MenuRepository
{
public
function
create
(
$data
)
{
$menu
=
$this
->
model
->
create
(
$data
);
event
(
$event
=
new
MenuIsCreating
(
$data
));
$menu
=
$this
->
model
->
create
(
$event
->
getAttributes
());
event
(
new
MenuWasCreated
(
$menu
));
...
...
@@ -21,7 +25,10 @@ class EloquentMenuRepository extends EloquentBaseRepository implements MenuRepos
public
function
update
(
$menu
,
$data
)
{
$menu
->
update
(
$data
);
event
(
$event
=
new
MenuIsUpdating
(
$menu
,
$data
));
$menu
->
update
(
$event
->
getAttributes
());
event
(
new
MenuWasUpdated
(
$menu
));
return
$menu
;
}
...
...
Modules/Menu/Tests/BaseMenuTest.php
View file @
64f7e413
...
...
@@ -9,10 +9,12 @@ use Maatwebsite\Sidebar\SidebarServiceProvider;
use
Mcamara\LaravelLocalization\Facades\LaravelLocalization
;
use
Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider
;
use
Modules\Core\Providers\CoreServiceProvider
;
use
Modules\Menu\Providers\EventServiceProvider
;
use
Modules\Menu\Providers\MenuServiceProvider
;
use
Modules\Menu\Repositories\MenuItemRepository
;
use
Modules\Menu\Repositories\MenuRepository
;
use
Modules\Page\Providers\PageServiceProvider
;
use
Modules\Setting\Providers\SettingServiceProvider
;
use
Modules\Tag\Providers\TagServiceProvider
;
use
Nwidart\Modules\LaravelModulesServiceProvider
;
use
Orchestra\Testbench\TestCase
;
...
...
@@ -48,6 +50,8 @@ abstract class BaseMenuTest extends TestCase
CoreServiceProvider
::
class
,
TagServiceProvider
::
class
,
PageServiceProvider
::
class
,
SettingServiceProvider
::
class
,
EventServiceProvider
::
class
,
MenuServiceProvider
::
class
,
LaravelLocalizationServiceProvider
::
class
,
SidebarServiceProvider
::
class
,
...
...
@@ -96,6 +100,10 @@ abstract class BaseMenuTest extends TestCase
'--database'
=>
'sqlite'
,
'--path'
=>
'Modules/Tag/Database/Migrations'
,
]);
$this
->
artisan
(
'migrate'
,
[
'--database'
=>
'sqlite'
,
'--path'
=>
'Modules/Setting/Database/Migrations'
,
]);
}
public
function
createMenu
(
$name
,
$title
)
...
...
Modules/Menu/Tests/EloquentMenuRepositoryTest.php
View file @
64f7e413
...
...
@@ -2,6 +2,13 @@
namespace
Modules\Menu\Tests
;
use
Illuminate\Support\Facades\Event
;
use
Modules\Menu\Events\MenuIsCreating
;
use
Modules\Menu\Events\MenuIsUpdating
;
use
Modules\Menu\Events\MenuWasCreated
;
use
Modules\Menu\Events\MenuWasUpdated
;
use
Modules\Setting\Repositories\SettingRepository
;
class
EloquentMenuRepositoryTest
extends
BaseMenuTest
{
/** @test */
...
...
@@ -13,10 +20,90 @@ class EloquentMenuRepositoryTest extends BaseMenuTest
$this
->
assertEquals
(
$menu
->
name
,
$this
->
menu
->
find
(
$menu
->
id
)
->
name
);
}
/** @test */
public
function
it_triggers_event_when_menu_was_created
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
Event
::
assertDispatched
(
MenuWasCreated
::
class
,
function
(
$e
)
use
(
$menu
)
{
return
$e
->
menu
->
id
===
$menu
->
id
;
});
}
/** @test */
public
function
it_triggers_event_when_menu_is_creating
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
Event
::
assertDispatched
(
MenuIsCreating
::
class
,
function
(
$e
)
use
(
$menu
)
{
return
$e
->
getAttribute
(
'name'
)
===
$menu
->
name
;
});
}
/** @test */
public
function
it_can_change_data_when_it_is_creating_event
()
{
Event
::
listen
(
MenuIsCreating
::
class
,
function
(
MenuIsCreating
$event
)
{
$event
->
setAttributes
([
'name'
=>
'MAIN'
]);
});
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$this
->
assertEquals
(
'MAIN'
,
$menu
->
name
);
}
/** @test */
public
function
it_triggers_event_when_menu_item_was_updated
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$this
->
menu
->
update
(
$menu
,
[]);
Event
::
assertDispatched
(
MenuWasUpdated
::
class
,
function
(
$e
)
use
(
$menu
)
{
return
$e
->
menu
->
id
===
$menu
->
id
;
});
}
/** @test */
public
function
it_triggers_event_when_menu_is_updating
()
{
Event
::
fake
();
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$this
->
menu
->
update
(
$menu
,
[]);
Event
::
assertDispatched
(
MenuIsUpdating
::
class
,
function
(
$e
)
use
(
$menu
)
{
return
$e
->
getMenu
()
->
id
===
$menu
->
id
;
});
}
/** @test */
public
function
it_can_change_attributes_before_update
()
{
Event
::
listen
(
MenuIsUpdating
::
class
,
function
(
MenuIsUpdating
$event
)
{
$event
->
setAttributes
([
'name'
=>
'MAIN'
]);
});
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$this
->
menu
->
update
(
$menu
,
[
'name'
=>
'better-one'
]);
$this
->
assertEquals
(
'MAIN'
,
$menu
->
name
);
}
/** @test */
public
function
it_should_create_root_item_when_creating_new_menu
()
{
app
(
SettingRepository
::
class
)
->
createOrUpdate
([
'core::locales'
=>
[
'en'
,
'fr'
,]
]);
$menu
=
$this
->
createMenu
(
'main'
,
'Main Menu'
);
$items
=
$this
->
menuItem
->
allRootsForMenu
(
$menu
->
id
);
$this
->
assertCount
(
1
,
$items
);
}
}
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