LdapUserProviderTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 63
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadUserByUsername() 0 12 1
A testLoadUserByUsernameNotFound() 0 13 2
A setUp() 0 7 1
A testRefreshUser() 0 12 1
1
<?php
2
3
namespace DoL\LdapBundle\Tests\Security\User;
4
5
use DoL\LdapBundle\Security\User\LdapUserProvider;
6
use DoL\LdapBundle\Tests\TestUser;
7
use FR3D\Psr3MessagesAssertions\PhpUnit\TestLogger;
8
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
9
10
/**
11
 * @covers \DoL\LdapBundle\Security\User\LdapUserProvider
12
 */
13
class LdapUserProviderTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var \DoL\LdapBundle\Ldap\LdapManager|\PHPUnit_Framework_MockObject_MockObject
17
     */
18
    protected $ldapManager;
19
20
    /**
21
     * @var \DoL\LdapBundle\Security\User\LdapUserProvider
22
     */
23
    protected $userProvider;
24
25
    protected function setUp()
26
    {
27
        $this->ldapManager = $this->getMockBuilder('DoL\LdapBundle\Ldap\LdapManager')
28
            ->disableOriginalConstructor()
29
            ->getMock();
30
31
        $this->userProvider = new LdapUserProvider($this->ldapManager, new TestLogger());
32
    }
33
34
    public function testLoadUserByUsername()
35
    {
36
        $username = 'test_username';
37
        $user = new TestUser();
38
        $user->setUsername($username);
39
40
        $this->ldapManager->expects($this->once())
41
            ->method('findUserByUsername')
42
            ->with($this->equalTo($username))
43
            ->will($this->returnValue($user));
44
45
        self::assertEquals($username, $this->userProvider->loadUserByUsername($username)->getUsername());
46
    }
47
48
    public function testLoadUserByUsernameNotFound()
49
    {
50
        $username = 'invalid_username';
51
52
        $this->ldapManager->expects($this->once())
53
            ->method('findUserByUsername')
54
            ->will($this->returnValue(null));
55
56
        try {
57
            $this->userProvider->loadUserByUsername($username);
58
            self::fail('Expected Symfony\Component\Security\Core\Exception\UsernameNotFoundException to be thrown');
59
        } catch (UsernameNotFoundException $notFoundException) {
60
            self::assertEquals($username, $notFoundException->getUsername());
61
        }
62
    }
63
64
    public function testRefreshUser()
65
    {
66
        $username = 'test_username';
67
        $user = new TestUser();
68
        $user->setUsername($username);
69
70
        $this->ldapManager->expects($this->once())
71
            ->method('findUserByUsername')
72
            ->with($this->equalTo($username))
73
            ->will($this->returnValue($user));
74
75
        self::assertEquals($user, $this->userProvider->refreshUser($user));
76
    }
77
}
78