Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Tests/unit/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
     * @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);
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...
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