|
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
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.