Completed
Pull Request — 5.2 (#2361)
by
unknown
15:14 queued 02:24
created

SimpleMenuAdaptorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 92
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A setUpSimpleMenuAdaptorMock() 0 9 1
A testAdaptChildren() 0 37 1
A testHasInterface() 0 5 1
A provider() 0 15 1
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...heckerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...zationCheckerInterface> of property $authorizationCheckerInterface.

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...
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
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...zationCheckerInterface>.

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...
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);
0 ignored issues
show
Documentation introduced by
$children is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Kunstmaan\AdminBu...elper\Menu\TopMenuItem>.

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...
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