Completed
Pull Request — master (#244)
by Łukasz
09:35
created

MainMenuListenerSpec   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 0
loc 90
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 17 2
A it_throws_exception_when_yaml_definition_of_menu_is_invalid() 0 9 1
A it_build_menu() 0 15 1
D getMatchers() 0 36 10
1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\EventListener;
4
5
use FSi\Bundle\AdminBundle\Event\MenuEvent;
6
use FSi\Bundle\AdminBundle\Menu\Builder\Exception\InvalidYamlStructureException;
7
use FSi\Bundle\AdminBundle\Menu\Item\Item;
8
use PhpSpec\ObjectBehavior;
9
use Prophecy\Argument;
10
use Prophecy\Prophet;
11
12
class MainMenuListenerSpec extends ObjectBehavior
13
{
14
    /**
15
     * @param \FSi\Bundle\AdminBundle\Admin\Manager $manager
16
     */
17
    function let($manager)
18
    {
19
        $prophet = new Prophet();
20
        $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...
21
            if ($args[0] == 'non_existing') {
22
                throw new \Exception(sprintf('Element %s does not exist', $args[0]));
23
            };
24
            $element = $prophet->prophesize('FSi\Bundle\AdminBundle\Admin\Element');
25
            $element->getId()->willReturn($args[0]);
26
            return $element;
27
        });
28
29
        $manager->hasElement(Argument::type('string'))->will(function ($args) {
30
            return $args[0] != 'non_existing';
31
        });
32
        $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...
33
    }
34
35
    /**
36
     * @param \FSi\Bundle\AdminBundle\Admin\Manager $manager
37
     * @param \FSi\Bundle\AdminBundle\Event\MenuEvent $event
38
     */
39
    function it_throws_exception_when_yaml_definition_of_menu_is_invalid($manager, $event)
40
    {
41
        $menuYaml = __DIR__ . '/invalid_admin_menu.yml';
42
        $this->beConstructedWith($manager, $menuYaml);
43
44
        $this->shouldThrow(new InvalidYamlStructureException(
45
            sprintf('File "%s" should contain top level "menu:" key', $menuYaml)
46
        ))->during('createMainMenu', array($event));
47
    }
48
49
    function it_build_menu()
50
    {
51
        $menu = $this->createMainMenu(new MenuEvent(new Item()));
52
53
        $menu->shouldHaveItem('News', 'news');
54
        $menu->shouldHaveItem('article', 'article');
55
        $menu->shouldHaveItem('admin.menu.structure', false);
56
        $menu->shouldHaveItemThatHaveChild('admin.menu.structure', 'home_page', 'home_page');
57
        $menu->shouldHaveItemThatHaveChild ('admin.menu.structure', 'Contact', 'contact');
58
        $menu->shouldHaveItemThatHaveChild ('admin.menu.structure', 'Offer', 'offer');
59
60
        $offerItem = $menu->getChildren()['admin.menu.structure']->getChildren()['Offer'];
61
        $offerItem->getOption('elements')[0]->getId()->shouldReturn('category');
62
        $offerItem->getOption('elements')[1]->getId()->shouldReturn('product');
63
    }
64
65
    public function getMatchers()
66
    {
67
        return array(
68
            'haveItem' => function(Item $menu, $itemName, $elementId = false) {
69
                $items = $menu->getChildren();
70
                foreach ($items as $item) {
71
                    if ($item->getName() === $itemName) {
72
                        if (!$elementId) {
73
                            return true;
74
                        }
75
76
                        /** @var ElementItem $item */
77
                        return $item->getElement()->getId() === $elementId;
78
                    }
79
                }
80
                return false;
81
            },
82
            'haveItemThatHaveChild' => function(Item $menu, $itemName, $childName, $elementId = false) {
83
                foreach ($menu->getChildren() as $item) {
84
                    if ($item->getName() === $itemName && $item->hasChildren()) {
85
                        foreach ($item->getChildren() as $child) {
86
                            if ($child->getName() === $childName) {
87
                                if (!$elementId) {
88
                                    return true;
89
                                }
90
91
                                /** @var ElementItem $child */
92
                                return $child->getElement()->getId() === $elementId;
93
                            }
94
                        }
95
                    }
96
                }
97
                return false;
98
            }
99
        );
100
    }
101
}
102