Completed
Push — master ( 81c6e7...6c71bd )
by Piotr
02:30
created

MainMenuListenerSpec::getMatchers()   D

Complexity

Conditions 10
Paths 1

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 19
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\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) {
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');
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