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

Tests/Helper/Menu/SimpleMenuAdaptorTest.php (4 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
0 ignored issues
show
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...
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);
0 ignored issues
show
The method adaptChildren does only exist in Kunstmaan\AdminBundle\He...\Menu\SimpleMenuAdaptor, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
77
        $this->assertCount(1, $children);
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
0 ignored issues
show
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...
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