Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Tests/unit/Helper/Menu/MenuBuilderTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Kunstmaan\AdminBundle\Tests\Helper\Menu;
3
4
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
5
use Kunstmaan\AdminBundle\Helper\Menu\ModulesMenuAdaptor;
6
use Kunstmaan\AdminBundle\Helper\Menu\SettingsMenuAdaptor;
7
use Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor;
8
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
9
use Kunstmaan\MenuBundle\Entity\MenuItem;
10
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem as MenuItemHelper;
11
use ReflectionClass;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
17
18
/**
19
 * Generated by PHPUnit_SkeletonGenerator on 2012-09-21 at 09:05:10.
20
 */
21
class MenuBuilderTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var MenuBuilder
25
     */
26
    protected $object;
27
28
    /**
29
     * @var ContainerInterface $container (mock)
30
     */
31
    protected $container;
32
33
    /**
34
     * @var AuthorizationCheckerInterface $authCheck (mock)
35
     */
36
    protected $authCheck;
37
38
    /**
39
     * @var RequestStack $stack (mock)
40
     */
41
    protected $stack;
42
43
    protected function setUp()
44
    {
45
        $storage = $this->createMock(TokenStorageInterface::class);
46
        $authCheck = $this->createMock(AuthorizationCheckerInterface::class);
47
        $authCheck->tokenStorage = $storage;
48
        $this->authCheck = $authCheck;
0 ignored issues
show
Documentation Bug introduced by
It seems like $authCheck of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...zationCheckerInterface> of property $authCheck.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        $container = $this->createMock(ContainerInterface::class);
50
        $stack = $this->createMock(RequestStack::class);
51
        $this->stack = $stack;
0 ignored issues
show
Documentation Bug introduced by
It seems like $stack of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...oundation\RequestStack> of property $stack.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
53
        $container->expects($this->any())->method('getParameter')->willReturn(true);
54
        $container->expects($this->any())->method('get')->will($this->onConsecutiveCalls(
55
            $stack, $stack, $authCheck
56
        ));
57
58
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->object = new MenuBuilder($container);
60
    }
61
62
    /**
63
     * @throws \ReflectionException
64
     */
65
    public function testGetCurrent()
66
    {
67
        $this->stack->expects($this->atLeastOnce())
68
            ->method('getCurrentRequest')
69
            ->willReturn(new Request([],[],['_route' => 'KunstmaanAdminBundle_settings']));
70
        $this->authCheck->expects($this->any())->method('isGranted')->willReturn(true);
71
        $adapter = new SettingsMenuAdaptor($this->authCheck, true);
72
        $this->object->addAdaptMenu($adapter);
73
        $current = $this->object->getCurrent();
74
        $this->assertInstanceOf(TopMenuItem::class, $current);
75
76
        $mirror = new ReflectionClass(MenuBuilder::class);
77
        $property = $mirror->getProperty('currentCache');
78
        $property->setAccessible(true);
79
        $property->setValue($this->object, new MenuItem());
80
        $current = $this->object->getCurrent();
81
        $this->assertInstanceOf(MenuItem::class, $current);
82
    }
83
84
85
    public function testGetBreadcrumbs()
86
    {
87
        $this->stack->expects($this->atLeastOnce())
88
        ->method('getCurrentRequest')
89
        ->willReturn(new Request([],[],['_route' => 'KunstmaanAdminBundle_settings']));
90
        $this->authCheck->expects($this->any())->method('isGranted')->willReturn(true);
91
        $adapter = new SettingsMenuAdaptor($this->authCheck, true);
92
        $this->object->addAdaptMenu($adapter);
93
        $crumb = $this->object->getBreadCrumb();
94
        $this->assertTrue(is_array($crumb));
95
        $this->assertCount(1, $crumb);
96
        $this->assertInstanceOf(TopMenuItem::class, $crumb[0]);
97
    }
98
99
100 View Code Duplication
    public function testGetLowestTopChild()
101
    {
102
        $this->stack->expects($this->atLeastOnce())
103
            ->method('getCurrentRequest')
104
            ->willReturn(new Request([],[],['_route' => 'KunstmaanAdminBundle_settings']));
105
        $this->authCheck->expects($this->any())->method('isGranted')->willReturn(true);
106
        $adapter = new SettingsMenuAdaptor($this->authCheck, true);
107
        $this->object->addAdaptMenu($adapter);
108
        $lowest = $this->object->getLowestTopChild();
109
        $this->assertInstanceOf(TopMenuItem::class, $lowest);
110
    }
111
112
    /**
113
     * @throws \ReflectionException
114
     */
115
    public function testGetLowestTopChildNonTop()
116
    {
117
        $parent = new MenuItem();
118
        $child = new MenuItem();
119
        $child->setParent($parent);
120
121
        $mirror = new ReflectionClass(MenuBuilder::class);
122
        $property = $mirror->getProperty('currentCache');
123
        $property->setAccessible(true);
124
        $property->setValue($this->object, $child);
125
126
        $lowest = $this->object->getLowestTopChild();
127
        $this->assertNull($lowest);
128
    }
129
130
    /**
131
     * @throws \ReflectionException
132
     */
133
    public function testAdaptChildren()
134
    {
135
        $this->authCheck->expects($this->any())->method('isGranted')->willReturn(true);
136
137
        $parent = $this->createMock(MenuItemHelper::class);
138
        $parent->expects($this->exactly(2))->method('getRoute')->willReturn('KunstmaanAdminBundle_settings');
139
140
        $container = $this->createMock(ContainerInterface::class);
141
        $container->expects($this->any())->method('getParameter')->willReturn(true);
142
        $container->expects($this->any())->method('get')->willReturn($this->authCheck);
143
144
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
145
        $this->object = new MenuBuilder($container);
146
147
        $adapter = new SettingsMenuAdaptor($this->authCheck, true);
148
        $array = [];
149
        $adapter->adaptChildren($this->object, $array, $parent, new Request([],[],['_route' => 'KunstmaanAdminBundle_settings_bundle_version']));
150
    }
151
152
153 View Code Duplication
    public function testModulesMenuAdaptor()
154
    {
155
        $this->stack->expects($this->atLeastOnce())
156
            ->method('getCurrentRequest')
157
            ->willReturn(new Request([],[],['_route' => 'KunstmaanAdminBundle_modules']));
158
159
        $this->authCheck->expects($this->any())->method('isGranted')->willReturn(true);
160
        $adapter = new ModulesMenuAdaptor();
161
        $this->object->addAdaptMenu($adapter);
162
        $current = $this->object->getCurrent();
163
        $this->assertInstanceOf(TopMenuItem::class, $current);
164
    }
165
166
    /**
167
     * @throws \ReflectionException
168
     */
169
    public function testSimpleMenuAdaptor()
170
    {
171
        $this->stack->expects($this->atLeastOnce())
172
            ->method('getCurrentRequest')
173
            ->willReturn(new Request([],[],['_route' => 'KunstmaanAdminBundle_modules']));
174
175
        $this->authCheck->expects($this->any())->method('isGranted')->willReturn(true);
176
        $adapter = new SimpleMenuAdaptor($this->authCheck, [[
177
            'label' => 'label',
178
            'parent' => null,
179
            'role' => 'ADMIN',
180
            'route' => 'KunstmaanAdminBundle_modules',
181
            'params' => ['x' => 'y'],
182
        ]]);
183
        $this->object->addAdaptMenu($adapter);
184
185
        $mirror = new ReflectionClass(MenuBuilder::class);
186
        $property = $mirror->getProperty('topMenuItems');
187
        $property->setAccessible(true);
188
189
        $helper = new MenuItemHelper($this->object);
190
        $helper->setActive(true);
191
        $property->setValue($this->object, [$helper]);
192
193
        $current = $this->object->getCurrent();
194
        $this->assertInstanceOf(TopMenuItem::class, $current);
195
196
        $mirror = new ReflectionClass(SimpleMenuAdaptor::class);
197
        $method = $mirror->getMethod('parentMatches');
198
        $method->setAccessible(true);
199
        $bool = $method->invoke($adapter, null, ['parent' => null]);
200
        $this->assertTrue($bool);
201
202
        $authCheck = $this->createMock(AuthorizationCheckerInterface::class);
203
        $authCheck->expects($this->any())->method('isGranted')->willReturn(false);
204
        $adapter = new SimpleMenuAdaptor($authCheck, [[
205
            'label' => 'label',
206
            'parent' => null,
207
            'role' => 'ADMIN',
208
            'route' => 'KunstmaanAdminBundle_modules',
209
            'params' => ['x' => 'y'],
210
        ]]);
211
        $array = [];
212
        $adapter->adaptChildren($this->object, $array, null, new Request());
213
        $this->assertEmpty($array);
214
    }
215
}
216