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

Tests/unit/Helper/Menu/SimpleMenuAdaptorTest.php (1 issue)

a method exists on all of the called types.

Bug Major

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);
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);
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...
78
79
        $this->assertCount(1, $children);
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