Completed
Push — master ( bcd255...1f6303 )
by Ruud
169:22 queued 157:52
created

SimpleMenuAdaptorTest::testHasInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
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...
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
Bug introduced by
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);
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...
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
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...
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