Passed
Branch master (022a1c)
by Darwin
03:39
created

LdapManagerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 170
Duplicated Lines 31.18 %

Importance

Changes 0
Metric Value
dl 53
loc 170
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testBind() 13 13 1
A testBuildFilter() 0 16 1
B setUp() 0 25 1
B ldapResponse() 0 28 1
A testFindUserBy() 19 19 1
A testFindUserByUsername() 18 18 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 DoL\LdapBundle\Tests\Ldap;
4
5
use DoL\LdapBundle\Hydrator\HydratorInterface;
6
use DoL\LdapBundle\Ldap\LdapManager;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
9
/**
10
 * @covers \DoL\LdapBundle\Ldap\LdapManager
11
 */
12
class LdapManagerTest extends \PHPUnit_Framework_TestCase
13
{
14
    /** @var array */
15
    protected $paramSets;
16
17
    /**
18
     * @var \DoL\LdapBundle\Driver\LdapDriverInterface|\PHPUnit_Framework_MockObject_MockObject
19
     */
20
    protected $driver;
21
22
    /**
23
     * @var HydratorInterface|\PHPUnit_Framework_MockObject_MockObject
24
     */
25
    protected $hydrator;
26
27
    /**
28
     * @var LdapManager
29
     */
30
    protected $ldapManager;
31
32
    /**
33
     * Sets up the fixture, for example, opens a network connection.
34
     * This method is called before a test is executed.
35
     */
36
    protected function setUp()
37
    {
38
        $this->paramSets = [
39
            'server1' => [
40
                'driver' => [
41
                    // SOME ATTRIBUTES
42
                ],
43
                'user' => [
44
                    'baseDn' => 'ou=Groups,dc=example,dc=com',
45
                    'filter' => '(attr0=value0)',
46
                    'attributes' => [
47
                        [
48
                            'ldap_attr' => 'uid',
49
                            'user_method' => 'setUsername',
50
                        ],
51
                    ],
52
                ],
53
            ],
54
        ];
55
56
        $this->driver = $this->getMock('DoL\LdapBundle\Driver\LdapDriverInterface');
57
58
        $this->hydrator = $this->getMock('DoL\LdapBundle\Hydrator\HydratorInterface');
59
60
        $this->ldapManager = new LdapManager($this->driver, $this->hydrator, $this->paramSets);
61
    }
62
63
    /**
64
     * @covers \DoL\LdapBundle\Ldap\LdapManager::findUserByUsername
65
     */
66 View Code Duplication
    public function testFindUserByUsername()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $username = 'test_username';
69
70
        $ldapResponse = $this->ldapResponse($username);
71
72
        $this->driver
73
            ->expects($this->once())
74
            ->method('search')
75
            ->with($this->equalTo('ou=Groups,dc=example,dc=com'),
76
                $this->equalTo('(&(attr0=value0)(uid=test_username))')
77
            )
78
            ->will($this->returnValue($ldapResponse))
79
        ;
80
81
        $resultUser = $this->ldapManager->findUserByUsername($username);
82
83
        self::assertEquals($username, $resultUser->getUsername());
84
    }
85
86
    /**
87
     * @covers \DoL\LdapBundle\Ldap\LdapManager::findUserBy
88
     */
89 View Code Duplication
    public function testFindUserBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $username = 'test_username';
92
93
        $ldapResponse = $this->ldapResponse($username);
94
95
        $this->driver
96
            ->expects($this->once())
97
            ->method('search')
98
            ->with($this->equalTo('ou=Groups,dc=example,dc=com'),
99
                $this->equalTo('(&(attr0=value0)(uid=test_username))')
100
            )
101
            ->will($this->returnValue($ldapResponse))
102
        ;
103
104
        $criteria = ['uid' => 'test_username'];
105
        $resultUser = $this->ldapManager->findUserBy($criteria);
106
107
        self::assertEquals($username, $resultUser->getUsername());
108
    }
109
110
    /**
111
     * @covers \DoL\LdapBundle\Ldap\LdapManager::buildFilter
112
     */
113
    public function testBuildFilter()
114
    {
115
        $reflectionClass = new \ReflectionClass('DoL\LdapBundle\Ldap\LdapManager');
116
        $method = $reflectionClass->getMethod('buildFilter');
117
        $method->setAccessible(true);
118
        $params = $reflectionClass->getProperty('params');
119
        $params->setAccessible(true);
120
        $params->setValue($this->ldapManager, $this->paramSets['server1']['user']);
121
122
        $criteria = array(
123
            'attr1' => 'value1',
124
            'attr2' => 'value2',
125
        );
126
        $expected = '(&(attr0=value0)(attr1=value1)(attr2=value2))';
127
128
        self::assertEquals($expected, $method->invoke($this->ldapManager, $criteria));
129
    }
130
131
    /**
132
     * @covers \DoL\LdapBundle\Ldap\LdapManager::bind
133
     */
134 View Code Duplication
    public function testBind()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $password = 'password';
137
138
        /** @var UserInterface $user */
139
        $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
140
141
        $this->driver->expects($this->once())
142
            ->method('bind')
143
            ->with($user, $this->equalTo($password))
144
            ->will($this->returnValue(true));
145
146
        self::assertTrue($this->ldapManager->bind($user, $password));
147
    }
148
149
    /**
150
     * @param $username
151
     *
152
     * @return array
153
     */
154
    protected function ldapResponse($username)
155
    {
156
        $entry = [
157
            'dn' => 'ou=group, dc=host, dc=foo',
158
            'uid' => [
159
                'count' => 1,
160
                0 => $username,
161
            ],
162
        ];
163
164
        $entries = [
165
            'count' => 1,
166
            $entry,
167
        ];
168
169
        $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
170
        $user->expects($this->any())
171
            ->method('getUsername')
172
            ->willReturn($username)
173
        ;
174
175
        $this->hydrator->expects($this->once())
176
            ->method('hydrate')
177
            ->with($entry)
178
            ->willReturn($user)
179
        ;
180
181
        return $entries;
182
    }
183
}
184