From 658ba861a50a9c08e0fdcfbea407b35e219ad0e0 Mon Sep 17 00:00:00 2001 From: MGatner Date: Thu, 29 Apr 2021 01:13:18 +0000 Subject: [PATCH 1/3] Rename styles, fixes #11 --- examples/Menus.php | 9 +++++++-- src/Config/Menus.php | 5 ++++- src/Menus/BreadcrumbsMenu.php | 7 ++++--- src/{Traits => Styles}/BootstrapStyle.php | 12 +++++++----- src/{Traits => Styles}/BreadcrumbsStyle.php | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) rename src/{Traits => Styles}/BootstrapStyle.php (53%) rename src/{Traits => Styles}/BreadcrumbsStyle.php (93%) diff --git a/examples/Menus.php b/examples/Menus.php index 3fb22e3..ce79cb3 100644 --- a/examples/Menus.php +++ b/examples/Menus.php @@ -1,5 +1,8 @@ */ - public $aliases = []; + public $aliases = [ + 'breadcrumbs' => BreadcrumbsMenu::class, + ]; } diff --git a/src/Config/Menus.php b/src/Config/Menus.php index 0b31603..83df738 100644 --- a/src/Config/Menus.php +++ b/src/Config/Menus.php @@ -1,6 +1,7 @@ */ - public $aliases = []; + public $aliases = [ + 'breadcrumbs' => BreadcrumbsMenu::class, + ]; } diff --git a/src/Menus/BreadcrumbsMenu.php b/src/Menus/BreadcrumbsMenu.php index d29a810..f943744 100644 --- a/src/Menus/BreadcrumbsMenu.php +++ b/src/Menus/BreadcrumbsMenu.php @@ -4,7 +4,7 @@ use CodeIgniter\HTTP\URI; use Tatter\Menus\Breadcrumb; use Tatter\Menus\Menu; -use Tatter\Menus\Traits\BreadcrumbsStyle; +use Tatter\Menus\Styles\BreadcrumbsStyle; /** * Breadcrumbs Menu @@ -124,7 +124,8 @@ public function __toString(): string } return $this->builder - ->html($last->display, ['class' => 'breadcrumb-item active']) - ->render(); + ->html($last->display, [ + 'class' => 'breadcrumb-item active' + ])->render(); } } diff --git a/src/Traits/BootstrapStyle.php b/src/Styles/BootstrapStyle.php similarity index 53% rename from src/Traits/BootstrapStyle.php rename to src/Styles/BootstrapStyle.php index a5cbdd4..d6d3f3f 100644 --- a/src/Traits/BootstrapStyle.php +++ b/src/Styles/BootstrapStyle.php @@ -1,4 +1,4 @@ -builder->registerFilter(function (Link $link) { - $link->addParentClass('nav-item'); - $link->addClass('nav-link'); - }); + $this->builder + ->addClass('navbar-nav mr-auto') + ->registerFilter(function (Link $link) { + $link->addParentClass('nav-item'); + $link->addClass('nav-link'); + }); } } diff --git a/src/Traits/BreadcrumbsStyle.php b/src/Styles/BreadcrumbsStyle.php similarity index 93% rename from src/Traits/BreadcrumbsStyle.php rename to src/Styles/BreadcrumbsStyle.php index c159cdb..8b56f66 100644 --- a/src/Traits/BreadcrumbsStyle.php +++ b/src/Styles/BreadcrumbsStyle.php @@ -1,4 +1,4 @@ - Date: Thu, 29 Apr 2021 01:14:25 +0000 Subject: [PATCH 2/3] Add AdminLTE style --- src/Styles/AdminLTEStyle.php | 28 +++++++++++++++++++ tests/menu/StylesTest.php | 52 ++++++++++++++++++++++++++++++++++++ tests/menu/TraitsTest.php | 29 -------------------- 3 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 src/Styles/AdminLTEStyle.php create mode 100644 tests/menu/StylesTest.php delete mode 100644 tests/menu/TraitsTest.php diff --git a/src/Styles/AdminLTEStyle.php b/src/Styles/AdminLTEStyle.php new file mode 100644 index 0000000..a93bdaa --- /dev/null +++ b/src/Styles/AdminLTEStyle.php @@ -0,0 +1,28 @@ +builder + ->addClass('nav nav-pills nav-sidebar flex-column') + ->setActiveClass('active menu-open') + ->setAttribute('data-widget', 'treeview') + ->setAttribute('role', 'menu') + ->setAttribute('data-accordion', 'false') + ->registerFilter(function (Link $link) { + $link->addParentClass('nav-item'); + $link->addClass('nav-link'); + }); + } +} diff --git a/tests/menu/StylesTest.php b/tests/menu/StylesTest.php new file mode 100644 index 0000000..a8aa780 --- /dev/null +++ b/tests/menu/StylesTest.php @@ -0,0 +1,52 @@ +builder + ->link(site_url('/'), 'Home') + ->link(site_url('/current'), 'Grain') + ->render(); + } + }; + + $result = $menu->__toString(); + + $this->assertStringContainsString('li class="nav-item"', $result); + $this->assertStringContainsString('class="nav-link"', $result); + } + + public function testAdminLTEStyleAppliesClasses() + { + $menu = new class extends Menu { + + use AdminLTEStyle; + + public function __toString(): string + { + return $this->builder + ->link(site_url('/'), 'Home') + ->link(site_url('/current'), 'Grain') + ->render(); + } + }; + + $result = $menu->__toString(); + + $this->assertStringContainsString('data-widget="treeview"', $result); + $this->assertStringContainsString('class="nav nav-pills', $result); + $this->assertStringContainsString('class="nav-link"', $result); + } +} diff --git a/tests/menu/TraitsTest.php b/tests/menu/TraitsTest.php deleted file mode 100644 index bdd64a7..0000000 --- a/tests/menu/TraitsTest.php +++ /dev/null @@ -1,29 +0,0 @@ -builder - ->link(site_url('/'), 'Home') - ->link(site_url('/current'), 'Grain') - ->render(); - } - }; - - $result = $menu->__toString(); - - $this->assertStringContainsString('li class="nav-item"', $result); - $this->assertStringContainsString('class="nav-link"', $result); - } -} From 4e0ef5f3a1bfa6a4f55e6823e33f3cbfe44d669f Mon Sep 17 00:00:00 2001 From: MGatner Date: Thu, 29 Apr 2021 01:16:12 +0000 Subject: [PATCH 3/3] Ignore redirects, fixes #12 --- src/Filters/MenusFilter.php | 11 +++++++++-- tests/filter/MenusFilterTest.php | 13 ++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Filters/MenusFilter.php b/src/Filters/MenusFilter.php index af71705..297547f 100644 --- a/src/Filters/MenusFilter.php +++ b/src/Filters/MenusFilter.php @@ -1,9 +1,11 @@ getBody())) { diff --git a/tests/filter/MenusFilterTest.php b/tests/filter/MenusFilterTest.php index 09cf49d..b048981 100644 --- a/tests/filter/MenusFilterTest.php +++ b/tests/filter/MenusFilterTest.php @@ -1,5 +1,6 @@ expectException('RuntimeException'); + $this->expectException('InvalidArgumentException'); $this->expectExceptionMessage('No arguments supplied to Menus filter.'); ($this->caller)(); @@ -53,6 +54,16 @@ public function testEmptyBody() $caller(['test']); } + public function testRedirects() + { + $this->response = new RedirectResponse(config('App')); + $caller = $this->getFilterCaller(MenusFilter::class, 'after'); + + $result = $caller(['test']); + + $this->assertNull($result); + } + public function testUnknownAlias() { $this->expectException('RuntimeException');