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) { |
|
|
|
|
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'); |
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
|
|
|
|
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.