Skip to content

Commit

Permalink
Menu has been renamed to Repository, tests have been added
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 16, 2017
1 parent feb171d commit 33cfe95
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 83 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"autoload-dev": {
"psr-4": {
"Konekt\\Concord\\Tests\\": "tests"
"Konekt\\Menu\\Tests\\": "tests"
}
},
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions src/Exceptions/MenuAlreadyExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Contains the MenuAlreadyExistsException class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-06-16
*
*/


namespace Konekt\Menu\Exceptions;


class MenuAlreadyExistsException extends \Exception
{

}
4 changes: 2 additions & 2 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class Item
/**
* Class constructor
*
* @param \Konekt\Menu\Menu $builder
* @param \Konekt\Menu\Repository $builder
* @param $id
* @param string $title
* @param string $title
* @param $options
*/
public function __construct($builder, $id, $title, $options)
Expand Down
15 changes: 6 additions & 9 deletions src/MenuServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class MenuServiceProvider extends ServiceProvider
*/
public function register()
{
$this->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();
Expand All @@ -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'),
]);
}

Expand Down
65 changes: 23 additions & 42 deletions src/Menu.php → src/Repository.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Contains the Menu class.
* Contains the Menu Repository class.
*
* @author Lavary
* @author Attila Fulop
Expand All @@ -11,30 +11,24 @@

namespace Konekt\Menu;

class Menu
{
use Konekt\Menu\Exceptions\MenuAlreadyExistsException;

class Repository
{
/**
* Menu collection
*
* @var Illuminate\Support\Collection
*/
protected $collection;

/**
* List of menu entries
*
* @var Menu[]
* @var \Illuminate\Support\Collection
*/
protected $menu = [];
protected $menus;

/**
* Initializing the menu builder
*/
public function __construct()
{
// creating a collection for storing menus
$this->collection = new Collection();
$this->menus = collect();
}


Expand All @@ -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);
}

/**
Expand All @@ -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])) {
Expand All @@ -90,32 +81,22 @@ 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
*
* @return \Illuminate\Support\Collection
*/
public function all()
{
return $this->collection;
return $this->menus;
}

}
32 changes: 32 additions & 0 deletions tests/Feature/FacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Contains the FacadeTest class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-06-16
*
*/


namespace Konekt\Menu\Tests\Feature;


use Konekt\Menu\Builder;
use Konekt\Menu\Tests\TestCase;
use Menu;

class FacadeTest extends TestCase
{
public function testMenuCanBeCreatedWithFacade()
{
$navbar = Menu::create('navbar', function($menu) {
$menu->add('Home');
});

$this->assertInstanceOf(Builder::class, $navbar);
$this->assertCount(1, $navbar->items);
}

}
47 changes: 47 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Contains the TestCase class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-06-16
*
*/


namespace Konekt\Menu\Tests;


use Konekt\Menu\Facades\Menu;
use Konekt\Menu\MenuServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Illuminate\Foundation\AliasLoader;

abstract class TestCase extends OrchestraTestCase
{
/**
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
{
return [
MenuServiceProvider::class
];
}

/**
* Set up the environment.
*
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
// Register the facade
AliasLoader::getInstance()->alias('Menu', Menu::class);
}


}
90 changes: 90 additions & 0 deletions tests/Unit/RepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Contains the SmokeTest class.
*
* @copyright Copyright (c) 2017 Attila Fulop
* @author Attila Fulop
* @license MIT
* @since 2017-06-16
*
*/


namespace Konekt\Menu\Tests\Unit;


use Konekt\Menu\Exceptions\MenuAlreadyExistsException;
use Konekt\Menu\Repository;
use Konekt\Menu\Tests\TestCase;

class RepositoryTest extends TestCase
{
/** @var Repository */
protected $repo;

public function setUp()
{
parent::setUp();

$this->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());
}

}
Loading

0 comments on commit 33cfe95

Please sign in to comment.