1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Tests\Helper\Menu; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface; |
6
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder; |
7
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor; |
8
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
12
|
|
|
|
13
|
|
|
class SimpleMenuAdaptorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var AuthorizationCheckerInterface (mock) */ |
16
|
|
|
private $authorizationCheckerInterface; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
private $menuItems; |
20
|
|
|
|
21
|
|
|
public function setUp(): void |
22
|
|
|
{ |
23
|
|
|
$this->authorizationCheckerInterface = $this->createMock(AuthorizationCheckerInterface::class); |
24
|
|
|
$this->menuItems = []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject|SimpleMenuAdaptor |
29
|
|
|
*/ |
30
|
|
|
public function setUpSimpleMenuAdaptorMock() |
31
|
|
|
{ |
32
|
|
|
$simpleMenuAdaptorMock = $this->getMockBuilder(SimpleMenuAdaptor::class) |
|
|
|
|
33
|
|
|
->setConstructorArgs([$this->authorizationCheckerInterface, $this->menuItems]) |
34
|
|
|
->setMethods(null) |
35
|
|
|
->getMock() |
36
|
|
|
; |
37
|
|
|
|
38
|
|
|
return $simpleMenuAdaptorMock; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @dataProvider provider |
43
|
|
|
* |
44
|
|
|
* @param TopMenuItem|null $parent |
45
|
|
|
*/ |
46
|
|
|
public function testAdaptChildren(?TopMenuItem $parent, ?string $itemParent) |
47
|
|
|
{ |
48
|
|
|
$this->menuItems[] = ['parent' => 'not_test_route']; |
49
|
|
|
|
50
|
|
|
$this->menuItems[] = [ |
51
|
|
|
'role' => 'some_role', |
52
|
|
|
'parent' => $itemParent, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$this->menuItems[] = [ |
56
|
|
|
'role' => 'some_role', |
57
|
|
|
'parent' => $itemParent, |
58
|
|
|
'route' => 'KunstmaanAdminBundle_menu_item', |
59
|
|
|
'params' => [], |
60
|
|
|
'label' => 'menu_item', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$children = []; |
64
|
|
|
|
65
|
|
|
/** @var MenuBuilder $menuBuilderMock */ |
66
|
|
|
$menuBuilderMock = $this->createMock(MenuBuilder::class); |
67
|
|
|
|
68
|
|
|
/** @var Request $request */ |
69
|
|
|
$request = new Request([], [], ['_route' => 'KunstmaanAdminBundle_menu_item']); |
70
|
|
|
|
71
|
|
|
$this->authorizationCheckerInterface |
72
|
|
|
->expects($this->exactly(2)) |
73
|
|
|
->method('isGranted') |
74
|
|
|
->will($this->onConsecutiveCalls(false, true)) |
75
|
|
|
; |
76
|
|
|
$simpleMenuAdaptorMock = $this->setUpSimpleMenuAdaptorMock(); |
77
|
|
|
$simpleMenuAdaptorMock->adaptChildren($menuBuilderMock, $children, $parent, $request); |
78
|
|
|
|
79
|
|
|
$this->assertCount(1, $children); |
80
|
|
|
$this->assertContainsOnlyInstancesOf(TopMenuItem::class, $children); |
81
|
|
|
$this->assertEquals('menu_item', $children[0]->getLabel()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testHasInterface() |
85
|
|
|
{ |
86
|
|
|
$simpleMenuAdaptorMock = $this->setUpSimpleMenuAdaptorMock(); |
87
|
|
|
$this->assertInstanceOf(MenuAdaptorInterface::class, $simpleMenuAdaptorMock); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function provider() |
91
|
|
|
{ |
92
|
|
|
/** @var TopMenuItem $parent */ |
93
|
|
|
$parent = $this->createMock(TopMenuItem::class); |
94
|
|
|
$parent |
95
|
|
|
->expects($this->any()) |
96
|
|
|
->method('getRoute') |
97
|
|
|
->willReturn('test_route') |
98
|
|
|
; |
99
|
|
|
|
100
|
|
|
return [ |
101
|
|
|
'with no parent' => [null, null], |
102
|
|
|
'with parent' => [$parent, 'test_route'], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.