Failed Conditions
Push — preview_pr236 ( 69ee07...f370cd )
by Guilherme
13:30
created

testGetPasswordRequirementsNoMin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Tests\Service;
12
13
use LoginCidadao\CoreBundle\Service\PasswordHintService;
14
use Rollerworks\Bundle\PasswordStrengthBundle\Validator\Constraints\PasswordRequirements;
15
use Symfony\Component\Translation\TranslatorInterface;
16
use Symfony\Component\Validator\Constraints\Length;
17
use Symfony\Component\Validator\Constraints\NotBlank;
18
use Symfony\Component\Validator\Validator\ValidatorInterface;
19
20
class PasswordHintServiceTest extends \PHPUnit_Framework_TestCase
21
{
22
23
    public function testGetHintStringNoLimits()
24
    {
25
        $userClass = 'UserClass';
26
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator(), $userClass);
27
        $service->getHintString();
28
    }
29
30
    public function testGetHintStringRange()
31
    {
32
        $messages = [
33
            ['password_hint.range.no_reqs'],
34
        ];
35
        $userClass = 'UserClass';
36
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
37
        $service->getHintString(['min' => 8, 'max' => 160]);
38
    }
39
40
    public function testGetHintStringMin()
41
    {
42
        $messages = [
43
            ['password_hint.min.no_reqs'],
44
        ];
45
        $userClass = 'UserClass';
46
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
47
        $service->getHintString(['min' => 8]);
48
    }
49
50
    public function testGetHintStringMax()
51
    {
52
        $messages = [
53
            ['password_hint.max.no_reqs'],
54
        ];
55
        $userClass = 'UserClass';
56
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
57
        $service->getHintString(['max' => 200]);
58
    }
59
60
    public function testGetHintStringNoLimitsFullReqs()
61
    {
62
        $messages = [
63
            ['password_hint.no_limit.with_reqs'],
64
            ['password_hint.requirements.numbers'],
65
            ['password_hint.requirements.letters'],
66
            ['password_hint.requirements.special'],
67
            ['password_hint.and'],
68
        ];
69
        $userClass = 'UserClass';
70
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator($messages), $userClass);
71
        $service->getHintString([
72
            'requireLetters' => true,
73
            'requireNumbers' => true,
74
            'requireSpecialCharacter' => true,
75
        ]);
76
    }
77
78
    public function testNoMetadata()
79
    {
80
        $expected = [
81
            'min' => 0,
82
            'max' => null,
83
            'requireLetters' => false,
84
            'requireNumbers' => false,
85
            'requireSpecialCharacter' => false,
86
        ];
87
88
        $userClass = 'UserClass';
89
        $service = new PasswordHintService($this->getValidator(), $this->getTranslator(), $userClass);
90
        $this->assertSame($expected, $service->getPasswordRequirements());
91
    }
92
93
    public function testNoPassword()
94
    {
95
        $expected = [
96
            'min' => 0,
97
            'max' => null,
98
            'requireLetters' => false,
99
            'requireNumbers' => false,
100
            'requireSpecialCharacter' => false,
101
        ];
102
103
        $userClass = 'UserClass';
104
105
        $this->runGetRequirementsTest(false, [], $userClass, $expected);
106
107
        $metadata = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataInterface');
108
        $metadata->expects($this->once())
109
            ->method('hasPropertyMetadata')->with('plainPassword')->willReturn(false);
110
111
        $validator = $this->getValidator();
112
        $validator->expects($this->once())
113
            ->method('getMetadataFor')->with($userClass)
114
            ->willReturn($metadata);
115
116
        $service = new PasswordHintService($validator, $this->getTranslator(), $userClass);
117
        $this->assertSame($expected, $service->getPasswordRequirements());
118
    }
119
120
    public function testGetPasswordRequirements()
121
    {
122
        $expected = [
123
            'min' => 8,
124
            'max' => 256,
125
            'requireLetters' => true,
126
            'requireNumbers' => true,
127
            'requireSpecialCharacter' => true,
128
        ];
129
130
        $userClass = 'UserClass';
131
132
        $constraints = [
133
            new PasswordRequirements([
134
                'minLength' => 8,
135
                'requireLetters' => true,
136
                'requireCaseDiff' => true,
137
                'requireNumbers' => true,
138
                'requireSpecialCharacter' => true,
139
            ]),
140
            new Length(['min' => 6, 'max' => 256]), // 'min' should be overridden
141
        ];
142
143
        $this->runGetRequirementsTest(true, $constraints, $userClass, $expected);
144
    }
145
146
    public function testGetPasswordRequirementsNoMin()
147
    {
148
        $expected = [
149
            'min' => 0,
150
            'max' => null,
151
            'requireLetters' => false,
152
            'requireNumbers' => false,
153
            'requireSpecialCharacter' => false,
154
        ];
155
156
        $userClass = 'UserClass';
157
158
        $constraints = [new NotBlank()];
159
160
        $this->runGetRequirementsTest(true, $constraints, $userClass, $expected);
161
    }
162
163
    /**
164
     * @param array $expectedMessagesMap
165
     * @return TranslatorInterface|\PHPUnit_Framework_MockObject_MockObject
166
     */
167
    private function getTranslator($expectedMessagesMap = [])
168
    {
169
        $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
170
        $translator->expects($this->exactly(count($expectedMessagesMap)))
171
            ->method('trans')->willReturnMap($expectedMessagesMap);
172
173
        return $translator;
174
    }
175
176
    /**
177
     * @return ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
178
     */
179
    private function getValidator()
180
    {
181
        return $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
182
    }
183
184
    private function runGetRequirementsTest(
185
        $hasProp,
186
        $constraints,
187
        $userClass,
188
        $expected
189
    ) {
190
        $metadata = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataInterface');
191
        $metadata->expects($this->once())
192
            ->method('hasPropertyMetadata')->with('plainPassword')->willReturn($hasProp);
193
194
        if ($hasProp) {
195
            $propMetadata = $this->getMock('Symfony\Component\Validator\Mapping\PropertyMetadataInterface');
196
            $propMetadata->expects($this->once())
197
                ->method('getConstraints')->willReturn($constraints);
198
199
            $metadata->expects($this->once())
200
                ->method('getPropertyMetadata')->with('plainPassword')->willReturn([$propMetadata]);
201
        }
202
203
        $validator = $this->getValidator();
204
        $validator->expects($this->once())
205
            ->method('getMetadataFor')->with($userClass)
206
            ->willReturn($metadata);
207
208
        $service = new PasswordHintService($validator, $this->getTranslator(), $userClass);
209
        $this->assertSame($expected, $service->getPasswordRequirements());
210
    }
211
}
212