Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

Tests/unit/Helper/Menu/MenuBuilderTest.php (19 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\MenuItem;
8
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
13
/**
14
 * Class MenuBuilderTest
15
 */
16
class MenuBuilderTest extends TestCase
17
{
18
    /**
19
     * @var ContainerInterface (mock)
20
     */
21
    protected $container;
22
23
    protected function setUp()
24
    {
25
        $container = $this->createMock(ContainerInterface::class);
26
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
27
    }
28
29
    /**
30
     * @param ContainerInterface $container
31
     * @param array|null         $methods
32
     *
33
     * @return \PHPUnit\Framework\MockObject\MockObject|MenuBuilder
34
     */
35
    public function setUpMenuBuilderMock(ContainerInterface $container, ?array $methods)
36
    {
37
        $menuBuilderMock = $this->getMockBuilder(MenuBuilder::class)
38
            ->setConstructorArgs([$container])
39
            ->setMethods($methods)
40
            ->getMock()
41
        ;
42
43
        return $menuBuilderMock;
44
    }
45
46
    public function testGetChildrenAndTopChildren()
47
    {
48
        $stack = $this->createMock(RequestStack::class);
49
50
        $this->container->expects($this->any())->method('get')->willReturn($stack);
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
51
52
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, null);
53
54
        /** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */
55
        $menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class);
56
        $menuAdaptorInterfaceMock
0 ignored issues
show
The method expects() does not seem to exist on object<Kunstmaan\AdminBu...u\MenuAdaptorInterface>.

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...
57
            ->expects($this->exactly(2))
58
            ->method('adaptChildren')
59
        ;
60
61
        $menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock);
0 ignored issues
show
The method addAdaptMenu does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
62
63
        $menuItemMock = $this->createMock(MenuItem::class);
64
        $this->assertIsArray($menuBuilderMock->getChildren($menuItemMock));
0 ignored issues
show
The method getChildren does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
65
        $this->assertIsArray($menuBuilderMock->getChildren());
66
    }
67
68
    public function testGetCurrentAndBreadCrumb()
69
    {
70
        $menuItemMock = $this->createMock(MenuItem::class);
71
        $menuItemMock
72
            ->expects($this->any())
73
            ->method('getActive')
74
            ->willReturn(true)
75
        ;
76
77
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']);
78
        $menuBuilderMock
0 ignored issues
show
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder.

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...
79
            ->expects($this->any())
80
            ->method('getChildren')
81
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
82
        ;
83
84
        $current = $menuBuilderMock->getCurrent();
0 ignored issues
show
The method getCurrent does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
85
        $this->assertInstanceOf(MenuItem::class, $current);
86
        $this->assertContainsOnlyInstancesOf(MenuItem::class, $menuBuilderMock->getBreadCrumb());
0 ignored issues
show
The method getBreadCrumb does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
87
    }
88
89
    public function testGetLowestTopChildWithCurrentTopMenuItem()
90
    {
91
        $menuItemMock = $this->createMock(TopMenuItem::class);
92
        $menuItemMock
93
            ->expects($this->any())
94
            ->method('getActive')
95
            ->willReturn(true)
96
        ;
97
98
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']);
99
        $menuBuilderMock
0 ignored issues
show
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder.

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...
100
            ->expects($this->any())
101
            ->method('getChildren')
102
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
103
        ;
104
105
        $this->assertInstanceOf(TopMenuItem::class, $menuBuilderMock->getLowestTopChild());
0 ignored issues
show
The method getLowestTopChild does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
106
    }
107
108
    public function testGetLowestTopChildWithMenuItem()
109
    {
110
        $menuItemMock = $this->createMock(MenuItem::class);
111
        $menuItemMock
112
            ->expects($this->any())
113
            ->method('getActive')
114
            ->willReturn(true)
115
        ;
116
        $menuItemMock
117
            ->expects($this->once())
118
            ->method('getParent')
119
            ->willReturn($this->createMock(TopMenuItem::class))
120
        ;
121
122
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']);
123
        $menuBuilderMock
0 ignored issues
show
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder.

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...
124
            ->expects($this->any())
125
            ->method('getChildren')
126
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
127
        ;
128
129
        $this->assertInstanceOf(TopMenuItem::class, $menuBuilderMock->getLowestTopChild());
0 ignored issues
show
The method getLowestTopChild does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
130
    }
131
132
    public function testGetLowestTopChildWithCurrentNull()
133
    {
134
        $menuItemMock = $this->createMock(TopMenuItem::class);
135
        $menuItemMock
136
            ->expects($this->any())
137
            ->method('getActive')
138
            ->willReturn(false)
139
        ;
140
141
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, ['getChildren']);
142
        $menuBuilderMock
0 ignored issues
show
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder.

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...
143
            ->expects($this->any())
144
            ->method('getChildren')
145
            ->will($this->onConsecutiveCalls([$menuItemMock], []))
146
        ;
147
148
        $this->assertNull($menuBuilderMock->getLowestTopChild());
0 ignored issues
show
The method getLowestTopChild does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
149
    }
150
151
    public function testGetTopChildren()
152
    {
153
        $stack = $this->createMock(RequestStack::class);
154
155
        $this->container->expects($this->any())->method('get')->willReturn($stack);
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
156
157
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, null);
0 ignored issues
show
$menuBuilderMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
159
        /** @var MenuAdaptorInterface $menuAdaptorInterfaceMock */
160
        $menuAdaptorInterfaceMock = $this->createMock(MenuAdaptorInterface::class);
161
        $menuAdaptorInterfaceMock
0 ignored issues
show
The method expects() does not seem to exist on object<Kunstmaan\AdminBu...u\MenuAdaptorInterface>.

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...
162
            ->expects($this->once())
163
            ->method('adaptChildren')
164
        ;
165
166
        $menuBuilderMock = $this->setUpMenuBuilderMock($this->container, null);
167
        $menuBuilderMock->addAdaptMenu($menuAdaptorInterfaceMock);
0 ignored issues
show
The method addAdaptMenu does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
168
169
        $this->assertIsArray($menuBuilderMock->getTopChildren());
0 ignored issues
show
The method getTopChildren does only exist in Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder, 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...
170
    }
171
}
172