Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

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