|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoL\LdapBundle\Tests\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use DoL\LdapBundle\Driver\LdapDriverInterface; |
|
6
|
|
|
use DoL\LdapBundle\Model\LdapUserInterface; |
|
7
|
|
|
use Maks3w\PhpUnitMethodsTrait\Framework\TestCaseTrait; |
|
8
|
|
|
use PHPUnit_Framework_Assert as Assert; |
|
9
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Common test methods for any DoL\LdapBundle\Driver\LdapDriverInterface implementation. |
|
14
|
|
|
*/ |
|
15
|
|
|
trait LdapDriverInterfaceTestTrait |
|
16
|
|
|
{ |
|
17
|
|
|
use TestCaseTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var LdapDriverInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $driver; |
|
23
|
|
|
|
|
24
|
|
|
public function testImplementsHydratorInterface() |
|
25
|
|
|
{ |
|
26
|
|
|
Assert::assertInstanceOf(LdapDriverInterface::class, $this->driver); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function validUserPasswordProvider() |
|
30
|
|
|
{ |
|
31
|
|
|
$goodDn = 'uid=test_username,ou=example,dc=com'; |
|
32
|
|
|
$goodPassword = 'password'; |
|
33
|
|
|
$goodUsername = 'test_username'; |
|
34
|
|
|
|
|
35
|
|
|
return [ |
|
36
|
|
|
// description => [user, password, expected_bind_rdn] |
|
|
|
|
|
|
37
|
|
|
'by Username' => [$this->mockUserInterface(['getUsername' => $goodUsername]), $goodPassword, $goodUsername], |
|
38
|
|
|
'by Dn' => [$this->mockLdapUserInterface(['getDn' => $goodDn]), $goodPassword, $goodDn], |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function invalidUserPasswordProvider() |
|
43
|
|
|
{ |
|
44
|
|
|
$goodUsername = 'test_username'; |
|
45
|
|
|
$wrongUsername = 'bad_username'; |
|
46
|
|
|
|
|
47
|
|
|
$goodDn = 'uid=test_username,ou=example,dc=com'; |
|
48
|
|
|
$wrongDn = 'uid=bad_username,ou=example,dc=com'; |
|
49
|
|
|
|
|
50
|
|
|
$goodPassword = 'password'; |
|
51
|
|
|
$badPassword = 'bad_password'; |
|
52
|
|
|
|
|
53
|
|
|
return [ |
|
54
|
|
|
// description => [user, password, expected_bind_rdn] |
|
|
|
|
|
|
55
|
|
|
'wrong username' => [$this->mockUserInterface(['getUsername' => $wrongUsername]), $goodPassword, $wrongUsername], |
|
56
|
|
|
'wrong password' => [$this->mockUserInterface(['getUsername' => $goodUsername]), $badPassword, $goodUsername], |
|
57
|
|
|
'wrong DN' => [$this->mockLdapUserInterface(['getDn' => $wrongDn]), $goodPassword, $wrongDn], |
|
58
|
|
|
'good DN, wrong password' => [$this->mockLdapUserInterface(['getDn' => $goodDn]), $badPassword, $goodDn], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $methodReturns |
|
64
|
|
|
* |
|
65
|
|
|
* @return UserInterface |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
private function mockUserInterface(array $methodReturns) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
/** @var UserInterface|\PHPUnit_Framework_MockObject_MockObject $user */ |
|
70
|
|
|
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($methodReturns as $method => $return) { |
|
73
|
|
|
$user->expects(TestCase::any()) |
|
74
|
|
|
->method($method) |
|
75
|
|
|
->will(TestCase::returnValue($return)) |
|
76
|
|
|
; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $user; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param array $methodReturns |
|
84
|
|
|
* |
|
85
|
|
|
* @return UserInterface|LdapUserInterface |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
private function mockLdapUserInterface(array $methodReturns) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
/** @var UserInterface|LdapUserInterface|\PHPUnit_Framework_MockObject_MockObject $user */ |
|
90
|
|
|
$user = $this->getMock('DoL\LdapBundle\Tests\TestUser'); |
|
91
|
|
|
|
|
92
|
|
|
foreach ($methodReturns as $method => $return) { |
|
93
|
|
|
$user->expects(TestCase::any()) |
|
94
|
|
|
->method($method) |
|
95
|
|
|
->will(TestCase::returnValue($return)) |
|
96
|
|
|
; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $user; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.