From 33cfe952a9f742f1f06e78a080c88b01b211f39d Mon Sep 17 00:00:00 2001 From: Attila Fulop Date: Sat, 17 Jun 2017 00:18:01 +0300 Subject: [PATCH] Menu has been renamed to Repository, tests have been added --- composer.json | 2 +- src/Exceptions/MenuAlreadyExistsException.php | 19 ++++ src/Item.php | 4 +- src/MenuServiceProvider.php | 15 ++-- src/{Menu.php => Repository.php} | 65 +++++--------- tests/Feature/FacadeTest.php | 32 +++++++ tests/TestCase.php | 47 ++++++++++ tests/Unit/RepositoryTest.php | 90 +++++++++++++++++++ tests/Unit/SmokeTest.php | 29 ------ 9 files changed, 220 insertions(+), 83 deletions(-) create mode 100644 src/Exceptions/MenuAlreadyExistsException.php rename src/{Menu.php => Repository.php} (52%) create mode 100644 tests/Feature/FacadeTest.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/RepositoryTest.php delete mode 100644 tests/Unit/SmokeTest.php diff --git a/composer.json b/composer.json index 510bade..444a11f 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ }, "autoload-dev": { "psr-4": { - "Konekt\\Concord\\Tests\\": "tests" + "Konekt\\Menu\\Tests\\": "tests" } }, "scripts": { diff --git a/src/Exceptions/MenuAlreadyExistsException.php b/src/Exceptions/MenuAlreadyExistsException.php new file mode 100644 index 0000000..c56b312 --- /dev/null +++ b/src/Exceptions/MenuAlreadyExistsException.php @@ -0,0 +1,19 @@ +mergeConfigFrom(__DIR__ . '/../config/settings.php', 'laravel-menu.settings'); - $this->mergeConfigFrom(__DIR__ . '/../config/views.php', 'laravel-menu.views'); + $this->mergeConfigFrom(__DIR__ . '/../config/settings.php', 'menu.settings'); + $this->mergeConfigFrom(__DIR__ . '/../config/views.php', 'menu.views'); $this->app->singleton('menu', function ($app) { - return new Menu(); + return new Repository(); }); $this->registerBladeExtensions(); @@ -48,15 +48,12 @@ public function register() */ public function boot() { - // Extending Blade engine - require_once('blade/lm-attrs.php'); - $this->loadViewsFrom(__DIR__ . '/../resources/views', 'menu'); $this->publishes([ - __DIR__ . '/../resources/views' => base_path('resources/views/vendor/laravel-menu'), - __DIR__ . '/../config/settings.php' => config_path('laravel-menu/settings.php'), - __DIR__ . '/../config/views.php' => config_path('laravel-menu/views.php'), + __DIR__ . '/../resources/views' => base_path('resources/views/vendor/menu'), + __DIR__ . '/../config/settings.php' => config_path('menu_settings.php'), + __DIR__ . '/../config/views.php' => config_path('menu_views.php'), ]); } diff --git a/src/Menu.php b/src/Repository.php similarity index 52% rename from src/Menu.php rename to src/Repository.php index 32791e0..81c8c46 100644 --- a/src/Menu.php +++ b/src/Repository.php @@ -1,6 +1,6 @@ collection = new Collection(); + $this->menus = collect(); } @@ -44,26 +38,23 @@ public function __construct() * @param string $name * @param callable $callback * - * @return \Konekt\Menu\Menu + * @return Repository + * @throws MenuAlreadyExistsException */ - public function make($name, $callback) + public function create($name, $callback = null) { - if (is_callable($callback)) { - if ( ! array_key_exists($name, $this->menu)) { - $this->menu[$name] = new Builder($name, $this->loadConf($name)); - } - - // Registering the items - call_user_func($callback, $this->menu[$name]); - - // Storing each menu instance in the collection - $this->collection->put($name, $this->menu[$name]); + if ($this->menus->has($name)) { + throw new MenuAlreadyExistsException("Can not create menu named `$name` because it already exists"); + } - // Make the instance available in all views - \View::share($name, $this->menu[$name]); + $this->menus->put($name, new Builder($name, $this->loadConf($name))); - return $this->menu[$name]; + if (is_callable($callback)) { + // Registering the items + call_user_func($callback, $this->menus->get($name)); } + + return $this->menus->get($name); } /** @@ -75,7 +66,7 @@ public function make($name, $callback) */ public function loadConf($name) { - $options = config('laravel-menu.settings'); + $options = config('menu.settings'); $name = strtolower($name); if (isset($options[$name]) && is_array($options[$name])) { @@ -90,24 +81,14 @@ public function loadConf($name) * * @param string $key * - * @return \Konekt\Menu\Item + * @return Builder|null */ public function get($key) { - return $this->collection->get($key); + return $this->menus->get($key); } - /** - * Return Menu collection - * - * @return \Illuminate\Support\Collection - */ - public function getCollection() - { - return $this->collection; - } - /** * Alias for getCollection * @@ -115,7 +96,7 @@ public function getCollection() */ public function all() { - return $this->collection; + return $this->menus; } } diff --git a/tests/Feature/FacadeTest.php b/tests/Feature/FacadeTest.php new file mode 100644 index 0000000..937e643 --- /dev/null +++ b/tests/Feature/FacadeTest.php @@ -0,0 +1,32 @@ +add('Home'); + }); + + $this->assertInstanceOf(Builder::class, $navbar); + $this->assertCount(1, $navbar->items); + } + +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..f9ef60a --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,47 @@ +alias('Menu', Menu::class); + } + + +} \ No newline at end of file diff --git a/tests/Unit/RepositoryTest.php b/tests/Unit/RepositoryTest.php new file mode 100644 index 0000000..2ccf412 --- /dev/null +++ b/tests/Unit/RepositoryTest.php @@ -0,0 +1,90 @@ +repo = new Repository(); + } + + public function testEmptyMenuCanBeCreated() + { + $sidebar = $this->repo->create('sidebar'); + + $this->assertNotNull($sidebar); + $this->assertCount(0, $sidebar->items); + } + + public function testMenuCanBeCreatedWithItemsInClosure() + { + $sidebar = $this->repo->create('sidebar', function ($menu) { + $menu->add('Home', 'home'); + $menu->add('Somewhere else', 'somewhere-else'); + }); + + $this->assertCount(2, $sidebar->items); + } + + public function testMenuCanBeRetrievedByKey() + { + $this->repo->create('sidebar', function ($menu) { + $menu->add('Moscow', 'moscow'); + $menu->add('Tallin', 'tallin'); + $menu->add('Göteborg', 'goteborg'); + }); + + $sidebar = $this->repo->get('sidebar'); + $this->assertNotNull($sidebar); + $this->assertCount(3, $sidebar->items); + } + + public function testNonExistentEntryReturnsNull() + { + $this->assertNull($this->repo->get('I do not exist')); + } + + public function testMenusWithSameNameCantBeCreated() + { + $this->repo->create('i_am_unique'); + $this->expectException(MenuAlreadyExistsException::class); + $this->repo->create('i_am_unique'); + } + + public function testAllActuallyReturnsAll() + { + $this->repo->create('splash'); + $this->repo->create('bang'); + $this->repo->create('pow'); + $this->repo->create('phitang'); + + $this->assertCount(4, $this->repo->all()); + + $this->assertArrayHasKey('splash', $this->repo->all()->all()); + $this->assertArrayHasKey('bang', $this->repo->all()->all()); + $this->assertArrayHasKey('pow', $this->repo->all()->all()); + $this->assertArrayHasKey('phitang', $this->repo->all()->all()); + } + +} \ No newline at end of file diff --git a/tests/Unit/SmokeTest.php b/tests/Unit/SmokeTest.php deleted file mode 100644 index d26557d..0000000 --- a/tests/Unit/SmokeTest.php +++ /dev/null @@ -1,29 +0,0 @@ -assertNotNull($menu); - } - -} \ No newline at end of file