HydratorInterfaceTestTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testHydrate() 0 7 2
A testImplementsHydratorInterface() 0 3 1
A validLdapUserAttributesProvider() 0 66 1
1
<?php
2
3
namespace DoL\LdapBundle\Tests\Hydrator;
4
5
use DoL\LdapBundle\Hydrator\HydratorInterface;
6
use PHPUnit_Framework_Assert as Assert;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
9
/**
10
 * Common test methods for any FR3D\LdapBundle\Hydrator\HydratorInterface implementation.
11
 */
12
trait HydratorInterfaceTestTrait
13
{
14
    /**
15
     * @var HydratorInterface
16
     */
17
    protected $hydrator;
18
19
    public function testImplementsHydratorInterface()
20
    {
21
        Assert::assertInstanceOf(HydratorInterface::class, $this->hydrator);
22
    }
23
24
    /**
25
     * @dataProvider validLdapUserAttributesProvider
26
     *
27
     * @param array $ldapEntry
28
     * @param array $methodsReturn
29
     */
30
    public function testHydrate(array $ldapEntry, array $methodsReturn)
31
    {
32
        $user = $this->hydrator->hydrate($ldapEntry);
33
34
        Assert::assertInstanceOf(UserInterface::class, $user);
35
        foreach ($methodsReturn as $method => $returnValue) {
36
            Assert::assertEquals($returnValue, $user->$method(), "UserInterface::{$method}() return value mismatch");
37
        }
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function validLdapUserAttributesProvider()
44
    {
45
        return [
46
            // Description => [ldap entry, [UserInterfaceMethod => return value]]
47
            'hydrate single attributes' => [
48
                'ldap entry' => [
49
                    'dn' => 'ou=group, dc=host, dc=foo',
50
                    'count' => 1,
51
                    'uid' => [
52
                        'count' => 1,
53
                        0 => 'test_username',
54
                    ],
55
                ],
56
                'expected methods return' => [
57
                    'getUserName' => 'test_username',
58
                ],
59
            ],
60
            'hydrate attributes collections' => [
61
                'ldap entry' => [
62
                    'dn' => 'ou=group, dc=host, dc=foo',
63
                    'roles' => [
64
                        'count' => 2,
65
                        0 => 'ROLE1',
66
                        1 => 'ROLE2',
67
                    ],
68
                ],
69
                'expected methods return' => [
70
                    'getRoles' => [
71
                        0 => 'ROLE1',
72
                        1 => 'ROLE2',
73
                    ],
74
                ],
75
            ],
76
            'hydrate single attributes without count index' => [
77
                'ldap entry' => [
78
                    'dn' => 'ou=group, dc=host, dc=foo',
79
                    'uid' => [
80
                        0 => 'test_username',
81
                    ],
82
                ],
83
                'expected methods return' => [
84
                    'getUserName' => 'test_username',
85
                ],
86
            ],
87
            'hydrate attributes collections without count index' => [
88
                'ldap entry' => [
89
                    'dn' => 'ou=group, dc=host, dc=foo',
90
                    'roles' => [
91
                        0 => 'ROLE1',
92
                        1 => 'ROLE2',
93
                    ],
94
                ],
95
                'expected methods return' => [
96
                    'getRoles' => [
97
                        0 => 'ROLE1',
98
                        1 => 'ROLE2',
99
                    ],
100
                ],
101
            ],
102
            'empty ldap entry return an empty user' => [
103
                'ldap entry' => [
104
                    'dn' => 'ou=group, dc=host, dc=foo',
105
                    'count' => 1,
106
                ],
107
                'expected methods return' => [
108
                    'getUserName' => null,
109
                ],
110
            ],
111
        ];
112
    }
113
}
114