ZendLdapDriverTest::testBindSuccessful()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DoL\LdapBundle\Tests\Driver;
4
5
use DoL\LdapBundle\Driver\ZendLdapDriver;
6
use FR3D\Psr3MessagesAssertions\PhpUnit\TestLogger;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
use Zend\Ldap\Exception\LdapException as ZendLdapException;
9
use Zend\Ldap\Ldap;
10
11
/**
12
 * Test class for ZendLdapDriver.
13
 */
14
class ZendLdapDriverTest extends \PHPUnit_Framework_TestCase
15
{
16
    use LdapDriverInterfaceTestTrait;
17
18
    /**
19
     * @var \Zend\Ldap\Ldap|\PHPUnit_Framework_MockObject_MockObject
20
     */
21
    protected $zend;
22
23
    /**
24
     * Sets up the fixture, for example, opens a network Driver.
25
     * This method is called before a test is executed.
26
     */
27
    protected function setUp()
28
    {
29
        if (!function_exists('ldap_connect')) {
30
            $this->markTestSkipped('PHP LDAP extension not loaded');
31
        }
32
33
        $this->zend = $this->getMock('Zend\Ldap\Ldap');
34
        $this->driver = new ZendLdapDriver($this->zend, new TestLogger());
35
    }
36
37
    public function testSearch()
38
    {
39
        $baseDn = 'ou=example,dc=org';
40
        $filter = '(&(uid=test_username))';
41
        $attributes = ['uid'];
42
43
        $entry = [
44
            'dn' => 'uid=test_username,ou=example,dc=org',
45
            'uid' => ['test_username'],
46
        ];
47
        $expect = [
48
            'count' => 1,
49
            $entry,
50
        ];
51
52
        $this->zend->expects($this->once())
53
            ->method('searchEntries')
54
            ->with($this->equalTo($filter), $this->equalTo($baseDn), $this->equalTo(Ldap::SEARCH_SCOPE_SUB), $this->equalTo($attributes))
55
            ->will($this->returnValue([$entry]));
56
57
        self::assertEquals($expect, $this->driver->search($baseDn, $filter, $attributes));
58
    }
59
60
    /**
61
     * @dataProvider validUserPasswordProvider
62
     *
63
     * @param UserInterface $user
64
     * @param string        $password
65
     * @param string        $expectedBindRdn
66
     */
67
    public function testBindSuccessful(UserInterface $user, $password, $expectedBindRdn)
68
    {
69
        $this->zend->expects($this->once())
70
            ->method('bind')
71
            ->with($this->equalTo($expectedBindRdn), $this->equalTo($password))
72
            ->will($this->returnValue($this->zend));
73
74
        self::assertTrue($this->driver->bind($user, $password));
75
    }
76
77
    /**
78
     * @dataProvider invalidUserPasswordProvider
79
     *
80
     * @param UserInterface $user
81
     * @param string        $password
82
     */
83
    public function testFailBindByDn(UserInterface $user, $password)
84
    {
85
        $this->zend->expects($this->once())
86
            ->method('bind')
87
            ->will($this->throwException(new ZendLdapException($this->zend)));
88
89
        self::assertFalse($this->driver->bind($user, $password));
90
    }
91
}
92