Completed
Pull Request — 3.1 (#348)
by Piotr
07:35
created

MainMenuListenerSpec::getMatchers()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 6.6166
c 0
b 0
f 0
cc 13
nc 1
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\EventListener;
4
5
use FSi\Bundle\AdminBundle\Admin\ManagerInterface;
6
use FSi\Bundle\AdminBundle\Event\MenuEvent;
7
use FSi\Bundle\AdminBundle\Menu\Builder\Exception\InvalidYamlStructureException;
8
use FSi\Bundle\AdminBundle\Menu\Item\ElementItem;
9
use FSi\Bundle\AdminBundle\Menu\Item\Item;
10
use FSi\Bundle\AdminBundle\Menu\Item\RoutableItem;
11
use PhpSpec\ObjectBehavior;
12
use Prophecy\Argument;
13
use Prophecy\Prophet;
14
use FSi\Bundle\AdminBundle\Admin\Element;
15
16
class MainMenuListenerSpec extends ObjectBehavior
17
{
18
    function let(ManagerInterface $manager)
19
    {
20
        $prophet = new Prophet();
21
        $manager->getElement(Argument::type('string'))->will(function($args) use ($prophet) {
0 ignored issues
show
Bug introduced by
The method will() does not seem to exist on object<FSi\Bundle\AdminBundle\Admin\Element>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22
            if ($args[0] == 'non_existing') {
23
                throw new \Exception(sprintf('Element %s does not exist', $args[0]));
24
            };
25
            $element = $prophet->prophesize(Element::class);
26
            $element->getId()->willReturn($args[0]);
27
            return $element;
28
        });
29
30
        $manager->hasElement(Argument::type('string'))->will(function ($args) {
31
            return $args[0] != 'non_existing';
32
        });
33
        $this->beConstructedWith($manager, __DIR__ . '/admin_menu.yml');
0 ignored issues
show
Unused Code introduced by
The call to MainMenuListenerSpec::beConstructedWith() has too many arguments starting with __DIR__ . '/admin_menu.yml'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
34
    }
35
36
    function it_throws_exception_when_yaml_definition_of_menu_is_invalid(
37
        ManagerInterface $manager,
38
        MenuEvent $event
39
    ) {
40
        $menuYaml = __DIR__ . '/invalid_admin_menu.yml';
41
        $this->beConstructedWith($manager, $menuYaml);
42
43
        $this->shouldThrow(new InvalidYamlStructureException(
44
            sprintf('File "%s" should contain top level "menu:" key', $menuYaml)
45
        ))->during('createMainMenu', [$event]);
46
    }
47
48
    function it_build_menu()
49
    {
50
        $menu = $this->createMainMenu(new MenuEvent(new Item()));
51
52
        $menu->shouldHaveItem('News', 'news');
53
        $menu->shouldHaveItem('article', 'article');
54
        $menu->shouldHaveItem('admin.menu.structure');
55
        $menu->shouldHaveItem('Home', 'fsi_admin');
56
        $menu->shouldHaveItem('Something custom', 'custom_route', ['foo' => 'bar']);
57
        $menu->shouldHaveItemThatHaveChild('admin.menu.structure', 'home_page', 'home_page');
58
        $menu->shouldHaveItemThatHaveChild ('admin.menu.structure', 'Contact', 'contact');
59
        $menu->shouldHaveItemThatHaveChild ('admin.menu.structure', 'Offer', 'offer');
60
61
        $offerItem = $menu->getChildren()['admin.menu.structure']->getChildren()['Offer'];
62
        $offerItem->getOption('elements')[0]->getId()->shouldReturn('category');
63
        $offerItem->getOption('elements')[1]->getId()->shouldReturn('product');
64
    }
65
66
    public function getMatchers(): array
67
    {
68
        return [
69
            'haveItem' => function(Item $menu, string $itemName, ?string $elementId = null, ?array $parameters = []) {
70
                $items = $menu->getChildren();
71
                foreach ($items as $item) {
72
                    if ($item->getName() === $itemName) {
73
                        if (null === $elementId) {
74
                            return true;
75
                        }
76
77
                        if ($item instanceof ElementItem) {
78
                            /** @var ElementItem $item */
79
                            return $item->getElement()->getId() === $elementId;
80
                        }
81
82
                        if ($item instanceof RoutableItem) {
83
                            return $item->getRoute() === $elementId && $item->getRouteParameters() === $parameters;
84
                        }
85
                    }
86
                }
87
                return false;
88
            },
89
            'haveItemThatHaveChild' => function(Item $menu, $itemName, $childName, $elementId = false) {
90
                foreach ($menu->getChildren() as $item) {
91
                    if ($item->getName() === $itemName && $item->hasChildren()) {
92
                        foreach ($item->getChildren() as $child) {
93
                            if ($child->getName() === $childName) {
94
                                if (!$elementId) {
95
                                    return true;
96
                                }
97
98
                                /** @var ElementItem $child */
99
                                return $child->getElement()->getId() === $elementId;
100
                            }
101
                        }
102
                    }
103
                }
104
                return false;
105
            }
106
        ];
107
    }
108
}
109