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() |
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
|
|
|
->getMock() |
35
|
|
|
; |
36
|
|
|
|
37
|
|
|
return $simpleMenuAdaptorMock; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider provider |
42
|
|
|
* |
43
|
|
|
* @param TopMenuItem|null $parent |
44
|
|
|
*/ |
45
|
|
|
public function testAdaptChildren(?TopMenuItem $parent, ?string $itemParent) |
46
|
|
|
{ |
47
|
|
|
$this->menuItems[] = ['parent' => 'not_test_route']; |
48
|
|
|
|
49
|
|
|
$this->menuItems[] = [ |
50
|
|
|
'role' => 'some_role', |
51
|
|
|
'parent' => $itemParent, |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$this->menuItems[] = [ |
55
|
|
|
'role' => 'some_role', |
56
|
|
|
'parent' => $itemParent, |
57
|
|
|
'route' => 'KunstmaanAdminBundle_menu_item', |
58
|
|
|
'params' => [], |
59
|
|
|
'label' => 'menu_item', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
$children = []; |
63
|
|
|
|
64
|
|
|
/** @var MenuBuilder $menuBuilderMock */ |
65
|
|
|
$menuBuilderMock = $this->createMock(MenuBuilder::class); |
66
|
|
|
|
67
|
|
|
/** @var Request $request */ |
68
|
|
|
$request = new Request([], [], ['_route' => 'KunstmaanAdminBundle_menu_item']); |
69
|
|
|
|
70
|
|
|
$this->authorizationCheckerInterface |
|
|
|
|
71
|
|
|
->expects($this->exactly(2)) |
72
|
|
|
->method('isGranted') |
73
|
|
|
->will($this->onConsecutiveCalls(false, true)) |
74
|
|
|
; |
75
|
|
|
$simpleMenuAdaptorMock = new SimpleMenuAdaptor($this->authorizationCheckerInterface, $this->menuItems); |
76
|
|
|
$simpleMenuAdaptorMock->adaptChildren($menuBuilderMock, $children, $parent, $request); |
77
|
|
|
|
78
|
|
|
$this->assertCount(1, $children); |
|
|
|
|
79
|
|
|
$this->assertContainsOnlyInstancesOf(TopMenuItem::class, $children); |
80
|
|
|
$this->assertEquals('menu_item', $children[0]->getLabel()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testHasInterface() |
84
|
|
|
{ |
85
|
|
|
$simpleMenuAdaptorMock = $this->setUpSimpleMenuAdaptorMock(); |
86
|
|
|
$this->assertInstanceOf(MenuAdaptorInterface::class, $simpleMenuAdaptorMock); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function provider() |
90
|
|
|
{ |
91
|
|
|
/** @var TopMenuItem $parent */ |
92
|
|
|
$parent = $this->createMock(TopMenuItem::class); |
93
|
|
|
$parent |
|
|
|
|
94
|
|
|
->expects($this->any()) |
95
|
|
|
->method('getRoute') |
96
|
|
|
->willReturn('test_route') |
97
|
|
|
; |
98
|
|
|
|
99
|
|
|
return [ |
100
|
|
|
'with no parent' => [null, null], |
101
|
|
|
'with parent' => [$parent, 'test_route'], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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..