Creating and triggering new (hookable) events for menu item entity

parent 60de6a52
<?php
namespace Modules\Menu\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging;
class MenuItemIsCreating extends AbstractEntityHook implements EntityIsChanging
{
}
<?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;
}
}
<?php
namespace Modules\Menu\Events;
use Modules\Menu\Entities\Menuitem;
class MenuItemWasUpdated
{
/**
* @var Menuitem
*/
public $menuItem;
public function __construct(Menuitem $menuItem)
{
$this->menuItem = $menuItem;
}
}
......@@ -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;
}
......
......@@ -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);
}
}
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