Completed
Push — master ( e32eeb...714cda )
by
unknown
89:50 queued 46:29
created

NavigationItemBuilderBuilderTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 129
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 1
Dependencies 6
Metric Value
wmc 3
lcom 1
cbo 6
dl 9
loc 129
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 9 9 1
B testBuildAnonUser() 0 33 1
A testBuild() 0 62 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Oro\Bundle\NavigationBundle\Tests\Unit\Menu;
4
5
use Oro\Bundle\NavigationBundle\Menu\NavigationItemBuilder;
6
use Oro\Bundle\OrganizationBundle\Entity\Organization;
7
8
class NavigationItemBuilderBuilderTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var \Doctrine\ORM\EntityManager
12
     */
13
    protected $em;
14
15
    /**
16
     * @var \Symfony\Component\Security\Core\SecurityContextInterface
17
     */
18
    protected $securityContext;
19
20
    /**
21
     * @var NavigationItemBuilder
22
     */
23
    protected $builder;
24
25
    /**
26
     * @var \Oro\Bundle\NavigationBundle\Entity\Builder\ItemFactory
27
     */
28
    protected $factory;
29
30 View Code Duplication
    protected function setUp()
31
    {
32
        $this->securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
33
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
34
            ->disableOriginalConstructor()
35
            ->getMock();
36
        $this->factory = $this->getMock('Oro\Bundle\NavigationBundle\Entity\Builder\ItemFactory');
37
        $this->builder = new NavigationItemBuilder($this->securityContext, $this->em, $this->factory);
38
    }
39
40
    public function testBuildAnonUser()
41
    {
42
        $token = $this
43
            ->getMockForAbstractClass(
44
                'Symfony\Component\Security\Core\Authentication\Token\TokenInterface',
45
                [],
46
                '',
47
                true,
48
                true,
49
                true,
50
                ['getUser', 'getOrganizationContext']
51
            )
52
        ;
53
        $token->expects($this->once())
54
            ->method('getUser')
55
            ->will($this->returnValue('anon.'));
56
57
        $token->expects($this->never())->method('getOrganizationContext');
58
59
        $this->securityContext->expects($this->atLeastOnce())
60
            ->method('getToken')
61
            ->will($this->returnValue($token));
62
63
        $menu = $this->getMockBuilder('Knp\Menu\ItemInterface')
64
            ->getMock();
65
        $menu->expects($this->never())
66
            ->method('addChild');
67
        $menu->expects($this->once())
68
            ->method('setExtra')
69
            ->with('type', 'pinbar');
70
71
        $this->builder->build($menu, array(), 'pinbar');
72
    }
73
74
    public function testBuild()
75
    {
76
        $organization   = new Organization();
77
        $type           = 'favorite';
78
        $userId         = 1;
79
        $user = $this->getMockBuilder('stdClass')
80
            ->setMethods(array('getId'))
81
            ->getMock();
82
        $user->expects($this->once($userId))
0 ignored issues
show
Unused Code introduced by
The call to NavigationItemBuilderBuilderTest::once() has too many arguments starting with $userId.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
            ->method('getId')
84
            ->will($this->returnValue(1));
85
86
        $token = $this->getMockBuilder(
87
            'Oro\Bundle\SecurityBundle\Authentication\Token\UsernamePasswordOrganizationToken'
88
        )
89
            ->disableOriginalConstructor()
90
            ->getMock();
91
        $token->expects($this->once())
92
            ->method('getUser')
93
            ->will($this->returnValue($user));
94
95
        $token->expects($this->once())
96
            ->method('getOrganizationContext')
97
            ->will($this->returnValue($organization));
98
99
        $this->securityContext->expects($this->atLeastOnce())
100
            ->method('getToken')
101
            ->will($this->returnValue($token));
102
103
        $item = $this->getMock('Oro\Bundle\NavigationBundle\Entity\NavigationItemInterface');
104
        $this->factory->expects($this->once())
105
            ->method('createItem')
106
            ->with($type, array())
107
            ->will($this->returnValue($item));
108
109
        $repository = $this->getMockBuilder('Oro\Bundle\NavigationBundle\Entity\Repository\NavigationItemRepository')
110
            ->disableOriginalConstructor()
111
            ->getMock();
112
        $items = array(
113
            array('id' => 1, 'title' => 'test1', 'url' => '/', 'type' => $type),
114
            array('id' => 2, 'title' => 'test2', 'url' => '/home', 'type' => $type)
115
        );
116
        $repository->expects($this->once())
117
            ->method('getNavigationItems')
118
            ->with($userId, $organization, $type)
119
            ->will($this->returnValue($items));
120
121
        $this->em->expects($this->once())
122
            ->method('getRepository')
123
            ->with(get_class($item))
124
            ->will($this->returnValue($repository));
125
126
        $menu = $this->getMockBuilder('Knp\Menu\ItemInterface')
127
            ->getMock();
128
        $menu->expects($this->exactly(2))
129
            ->method('addChild');
130
        $menu->expects($this->once())
131
            ->method('setExtra')
132
            ->with('type', $type);
133
134
        $this->builder->build($menu, array(), $type);
135
    }
136
}
137