EloquentMenuRepositoryTest.php 2.88 KB
Newer Older
1 2 3 4
<?php

namespace Modules\Menu\Tests;

5 6 7 8 9 10
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;

11
class EloquentMenuRepositoryTest extends BaseMenuTest
12 13 14 15 16 17 18 19 20 21
{
    /** @test */
    public function it_creates_menu()
    {
        $menu = $this->createMenu('main', 'Main Menu');

        $this->assertEquals(1, $this->menu->find($menu->id)->count());
        $this->assertEquals($menu->name, $this->menu->find($menu->id)->name);
    }

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    /** @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 */
98 99 100 101 102
    public function it_should_create_root_item_when_creating_new_menu()
    {
        $menu = $this->createMenu('main', 'Main Menu');

        $items = $this->menuItem->allRootsForMenu($menu->id);
103
        $this->assertCount(1, $items);
104 105
    }
}