LdapDriverInterfaceTestTrait::mockUserInterface()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
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
    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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the documented return type Symfony\Component\Security\Core\User\UserInterface.
Loading history...
80
    }
81
82
    /**
83
     * @param array $methodReturns
84
     *
85
     * @return UserInterface|LdapUserInterface
86
     */
87
    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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the documented return type DoL\LdapBundle\Model\Lda...Core\User\UserInterface.
Loading history...
100
    }
101
}
102