Skip to content

Commit

Permalink
Menu::getAllMenuParts() method > returns all menu parts (menu, contai…
Browse files Browse the repository at this point in the history
…ners with links and links)
  • Loading branch information
meritoo committed May 13, 2019
1 parent 0dd0e3c commit 08b4e4d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ Build navigation easily, without any efforts. Library that provides tools to bui

1. Minor refactoring
2. Menu, Link & LinkContainer > add getters (to get content)
3. Menu::getAllMenuParts() method > returns all menu parts (menu, containers with links and links)

# 0.0.4

Expand Down
25 changes: 25 additions & 0 deletions src/Menu.php
Expand Up @@ -11,6 +11,7 @@
namespace Meritoo\Menu;

use Meritoo\Common\Collection\Templates;
use Meritoo\Common\Utilities\Arrays;

/**
* Menu. Has containers with links.
Expand Down Expand Up @@ -54,6 +55,30 @@ public function getLinksContainers(): array
return $this->linksContainers;
}

/**
* Returns all menu parts (menu, containers with links and links)
*
* @return MenuPart[]
*/
public function getAllMenuParts(): array
{
$links = [];

foreach ($this->linksContainers as $container) {
$links[] = $container->getLink();
}

$result = array_merge(
[
$this,
],
$this->linksContainers,
$links
);

return Arrays::getNonEmptyValues($result);
}

/**
* Creates new menu
*
Expand Down
79 changes: 79 additions & 0 deletions tests/MenuTest.php
Expand Up @@ -56,6 +56,18 @@ public function testGetLinksContainers(string $description, Menu $menu, array $e
static::assertEquals($expected, $menu->getLinksContainers(), $description);
}

/**
* @param string $description Description of test
* @param Menu $menu Menu to verify
* @param array $expected Expected menu parts
*
* @dataProvider provideMenuToGetAllMenuParts
*/
public function testGetAllMenuParts(string $description, Menu $menu, array $expected): void
{
static::assertEquals($expected, $menu->getAllMenuParts(), $description);
}

public function testRenderWithoutLinksContainers(): void
{
$menu = new Menu([]);
Expand Down Expand Up @@ -597,4 +609,71 @@ public function provideMenuToGetLinksContainers(): ?Generator
],
];
}

public function provideMenuToGetAllMenuParts(): ?Generator
{
yield[
'No menu parts',
new Menu([]),
[
new Menu([]),
],
];

yield[
'1 container only',
new Menu([
new LinkContainer(new Link('', '')),
]),
[
new Menu([
new LinkContainer(new Link('', '')),
]),
new LinkContainer(new Link('', '')),
new Link('', ''),
],
];

yield[
'2 containers',
new Menu([
new LinkContainer(new Link('Test 1', '')),
new LinkContainer(new Link('Test 2', '/')),
]),
[
new Menu([
new LinkContainer(new Link('Test 1', '')),
new LinkContainer(new Link('Test 2', '/')),
]),
new LinkContainer(new Link('Test 1', '')),
new LinkContainer(new Link('Test 2', '/')),
new Link('Test 1', ''),
new Link('Test 2', '/'),
],
];

yield[
'2 containers - created by static method create()',
Menu::create([
[
'Test 1',
'',
],
[
'Test 2',
'/',
],
]),
[
new Menu([
new LinkContainer(new Link('Test 1', '')),
new LinkContainer(new Link('Test 2', '/')),
]),
new LinkContainer(new Link('Test 1', '')),
new LinkContainer(new Link('Test 2', '/')),
new Link('Test 1', ''),
new Link('Test 2', '/'),
],
];
}
}

0 comments on commit 08b4e4d

Please sign in to comment.