Creating and triggering new (hookable) events for menu entity

parent 89a278c1
<?php
namespace Modules\Menu\Events;
use Modules\Core\Abstracts\AbstractEntityHook;
use Modules\Core\Contracts\EntityIsChanging;
class MenuIsCreating extends AbstractEntityHook implements EntityIsChanging
{
}
<?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;
}
}
<?php
namespace Modules\Menu\Events;
use Modules\Menu\Entities\Menu;
class MenuWasUpdated
{
/**
* @var Menu
*/
public $menu;
public function __construct(Menu $menu)
{
$this->menu = $menu;
}
}
......@@ -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;
}
......
......@@ -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)
......
......@@ -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);
}
}
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