Completed
Push — master ( 3a65e8...9f4bbe )
by Grégoire
16s
created

gument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Tests\Menu\Provider;
13
14
use Knp\Menu\FactoryInterface;
15
use Knp\Menu\MenuFactory;
16
use Knp\Menu\Provider\MenuProviderInterface;
17
use PHPUnit\Framework\TestCase;
18
use PHPUnit_Framework_MockObject_MockObject as MockObject;
19
use Sonata\AdminBundle\Admin\AdminInterface;
20
use Sonata\AdminBundle\Admin\Pool;
21
use Sonata\AdminBundle\Menu\Provider\GroupMenuProvider;
22
23
class GroupMenuProviderTest extends TestCase
24
{
25
    /**
26
     * @var MockObject|Pool
27
     */
28
    private $pool;
29
    /**
30
     * @var MockObject|MenuProviderInterface
31
     */
32
    private $provider;
33
    /**
34
     * @var MockObject|FactoryInterface
35
     */
36
    private $factory;
37
38
    /**
39
     * @var MockObject
40
     */
41
    private $checker;
42
43
    protected function setUp()
44
    {
45
        $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
46
        $this->checker = $this
47
            ->getMockBuilder('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')
48
            ->setMethods(['isGranted'])
49
            ->disableOriginalConstructor()
50
            ->getMock();
51
52
        $this->factory = new MenuFactory();
53
54
        $this->provider = new GroupMenuProvider($this->factory, $this->pool, $this->checker);
55
    }
56
57
    public function testGroupMenuProviderName()
58
    {
59
        $this->assertTrue($this->provider->has('sonata_group_menu'));
60
    }
61
62
    /**
63
     * NEXT_MAJOR: Remove this test.
64
     *
65
     * @param array $adminGroups
66
     *
67
     * @group legacy
68
     * @dataProvider getAdminGroups
69
     */
70
    public function testGroupMenuProviderWithoutChecker(array $adminGroups)
71
    {
72
        $provider = new GroupMenuProvider($this->factory, $this->pool);
73
74
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
75
            ->method('getInstance')
76
            ->with($this->equalTo('sonata_admin_foo_service'))
77
            ->will($this->returnValue($this->getAdminMock()));
78
79
        $menu = $provider->get(
80
            'providerFoo',
81
            [
82
                'name' => 'foo',
83
                'group' => $adminGroups,
84
            ]
85
        );
86
87
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
88
        $this->assertSame('foo', $menu->getName());
89
90
        $children = $menu->getChildren();
91
92
        $this->assertCount(2, $children);
93
        $this->assertArrayHasKey('foo_admin_label', $children);
94
        $this->assertArrayHasKey('route_label', $children);
95
        $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
96
        $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
97
98
        $extras = $menu['foo_admin_label']->getExtras();
99
        $this->assertArrayHasKey('label_catalogue', $extras);
100
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
101
102
        $extras = $menu['route_label']->getExtras();
103
        $this->assertArrayHasKey('label_catalogue', $extras);
104
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
105
    }
106
107
    /**
108
     * @param array $adminGroups
109
     *
110
     * @dataProvider getAdminGroups
111
     */
112
    public function testGetMenuProviderWithCheckerGrantedGroupRoles(array $adminGroups)
113
    {
114
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
115
            ->method('getInstance')
116
            ->with($this->equalTo('sonata_admin_foo_service'))
117
            ->will($this->returnValue($this->getAdminMock()));
118
119
        $this->checker->expects($this->any())
120
            ->method('isGranted')
121
            ->willReturn(false);
122
123
        $menu = $this->provider->get(
124
            'providerFoo',
125
            [
126
                'name' => 'foo',
127
                'group' => $adminGroups,
128
            ]
129
        );
130
131
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
132
        $this->assertSame('foo', $menu->getName());
133
134
        $children = $menu->getChildren();
135
136
        $this->assertCount(1, $children);
137
        $this->assertArrayHasKey('foo_admin_label', $children);
138
        $this->assertArrayNotHasKey('route_label', $children);
139
        $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
140
        $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
141
142
        $extras = $menu['foo_admin_label']->getExtras();
143
        $this->assertArrayHasKey('label_catalogue', $extras);
144
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
145
    }
146
147
    /**
148
     * @param array $adminGroups
149
     *
150
     * @dataProvider getAdminGroups
151
     */
152
    public function testGetMenuProviderWithAdmin(array $adminGroups)
153
    {
154
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
155
            ->method('getInstance')
156
            ->with($this->equalTo('sonata_admin_foo_service'))
157
            ->will($this->returnValue($this->getAdminMock()));
158
159
        $this->checker->expects($this->any())
160
            ->method('isGranted')
161
            ->willReturn(true);
162
163
        $menu = $this->provider->get(
164
            'providerFoo',
165
            [
166
                'name' => 'foo',
167
                'group' => $adminGroups,
168
            ]
169
        );
170
171
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
172
        $this->assertSame('foo', $menu->getName());
173
174
        $children = $menu->getChildren();
175
176
        $this->assertCount(2, $children);
177
        $this->assertArrayHasKey('foo_admin_label', $children);
178
        $this->assertArrayHasKey('route_label', $children);
179
        $this->assertInstanceOf('Knp\Menu\MenuItem', $menu['foo_admin_label']);
180
        $this->assertSame('foo_admin_label', $menu['foo_admin_label']->getLabel());
181
182
        $extras = $menu['foo_admin_label']->getExtras();
183
        $this->assertArrayHasKey('label_catalogue', $extras);
184
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
185
186
        $extras = $menu['route_label']->getExtras();
187
        $this->assertArrayHasKey('label_catalogue', $extras);
188
        $this->assertSame($extras['label_catalogue'], 'SonataAdminBundle');
189
    }
190
191
    /**
192
     * @param array $adminGroups
193
     *
194
     * @dataProvider getAdminGroups
195
     */
196
    public function testGetKnpMenuWithListRoute(array $adminGroups)
197
    {
198
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
199
            ->method('getInstance')
200
            ->with($this->equalTo('sonata_admin_foo_service'))
201
            ->will($this->returnValue($this->getAdminMock(false)));
202
203
        $this->checker->expects($this->any())
204
            ->method('isGranted')
205
            ->willReturn(true);
206
207
        $menu = $this->provider->get(
208
            'providerFoo',
209
            [
210
                'name' => 'foo',
211
                'group' => $adminGroups,
212
            ]
213
        );
214
215
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
216
        $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
217
        $this->assertArrayHasKey('route_label', $menu->getChildren());
218
        $this->assertCount(1, $menu->getChildren());
219
    }
220
221
    /**
222
     * @param array $adminGroups
223
     *
224
     * @dataProvider getAdminGroups
225
     */
226
    public function testGetKnpMenuWithGrantedList(array $adminGroups)
227
    {
228
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
229
            ->method('getInstance')
230
            ->with($this->equalTo('sonata_admin_foo_service'))
231
            ->will($this->returnValue($this->getAdminMock(true, false)));
232
233
        $this->checker->expects($this->any())
234
            ->method('isGranted')
235
            ->willReturn(true);
236
237
        $menu = $this->provider->get(
238
            'providerFoo',
239
            [
240
                'name' => 'foo',
241
                'group' => $adminGroups,
242
            ]
243
        );
244
245
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
246
        $this->assertArrayNotHasKey('foo_admin_label', $menu->getChildren());
247
        $this->assertArrayHasKey('route_label', $menu->getChildren());
248
        $this->assertCount(1, $menu->getChildren());
249
    }
250
251
    /**
252
     * @param array $adminGroupsOnTopOption
253
     *
254
     * @dataProvider getAdminGroupsWithOnTopOption
255
     */
256
    public function testGetMenuProviderOnTopOptions(array $adminGroupsOnTopOption)
257
    {
258
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
259
            ->method('getInstance')
260
            ->with($this->equalTo('sonata_admin_foo_service'))
261
            ->will($this->returnValue($this->getAdminMock(true, false)));
262
263
        $menu = $this->provider->get(
264
            'providerFoo',
265
            [
266
                'name' => 'foo',
267
                'group' => $adminGroupsOnTopOption,
268
            ]
269
        );
270
271
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
272
        $this->assertCount(0, $menu->getChildren());
273
    }
274
275
    /**
276
     * @param array $adminGroups
277
     *
278
     * @dataProvider getAdminGroups
279
     */
280
    public function testGetMenuProviderKeepOpenOption(array $adminGroups)
281
    {
282
        $this->pool->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Sonata\AdminBundle\Admin\Pool>.

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...
283
            ->method('getInstance')
284
            ->with($this->equalTo('sonata_admin_foo_service'))
285
            ->will($this->returnValue($this->getAdminMock()));
286
287
        $this->checker->expects($this->any())
288
            ->method('isGranted')
289
            ->willReturn(true);
290
291
        $adminGroups['keep_open'] = true;
292
293
        $menu = $this->provider->get(
294
            'providerFoo',
295
            [
296
                'name' => 'foo',
297
                'group' => $adminGroups,
298
            ]
299
        );
300
301
        $this->assertInstanceOf('Knp\Menu\ItemInterface', $menu);
302
        $this->assertSame('keep-open', $menu->getAttribute('class'));
303
        $this->assertTrue($menu->getExtra('keep_open'));
304
    }
305
306
    /**
307
     * @return array
308
     */
309
    public function getAdminGroups()
310
    {
311
        return [
312
            [
313
                'bar' => [
314
                    'label' => 'foo',
315
                    'icon' => '<i class="fa fa-edit"></i>',
316
                    'label_catalogue' => 'SonataAdminBundle',
317
                    'items' => [
318
                        [
319
                            'admin' => 'sonata_admin_foo_service',
320
                            'label' => 'fooLabel',
321
                            'route_absolute' => true,
322
                        ],
323
                        [
324
                            'admin' => '',
325
                            'label' => 'route_label',
326
                            'route' => 'FooRoute',
327
                            'route_params' => ['foo' => 'bar'],
328
                            'route_absolute' => true,
329
                            'roles' => [],
330
                        ],
331
                    ],
332
                    'item_adds' => [],
333
                    'roles' => ['foo'],
334
                ],
335
            ],
336
        ];
337
    }
338
339
    /**
340
     * @return array
341
     */
342
    public function getAdminGroupsWithOnTopOption()
343
    {
344
        return [
345
            [
346
                'foo' => [
347
                    'label' => 'foo_on_top',
348
                    'icon' => '<i class="fa fa-edit"></i>',
349
                    'label_catalogue' => 'SonataAdminBundle',
350
                    'on_top' => true,
351
                    'items' => [
352
                        [
353
                            'admin' => 'sonata_admin_foo_service',
354
                            'label' => 'fooLabel',
355
                            'route_absolute' => true,
356
                            'route_params' => [],
357
                        ],
358
                    ],
359
                    'item_adds' => [],
360
                    'roles' => [],
361
                ],
362
            ],
363
        ];
364
    }
365
366
    /**
367
     * @param bool $hasRoute
368
     * @param bool $isGranted
369
     *
370
     * @return MockObject|AdminInterface
371
     */
372
    private function getAdminMock($hasRoute = true, $isGranted = true)
373
    {
374
        $admin = $this->createMock('Sonata\AdminBundle\Admin\AbstractAdmin');
375
        $admin->expects($this->once())
376
            ->method('hasRoute')
377
            ->with($this->equalTo('list'))
378
            ->will($this->returnValue($hasRoute));
379
380
        $admin->expects($this->any())
381
            ->method('hasAccess')
382
            ->with($this->equalTo('list'))
383
            ->will($this->returnValue($isGranted));
384
385
        $admin->expects($this->any())
386
            ->method('getLabel')
387
            ->will($this->returnValue('foo_admin_label'));
388
389
        $admin->expects($this->any())
390
            ->method('generateMenuUrl')
391
            ->will($this->returnValue([]));
392
393
        $admin->expects($this->any())
394
            ->method('getTranslationDomain')
395
            ->will($this->returnValue('SonataAdminBundle'));
396
397
        return $admin;
398
    }
399
}
400