Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

PasswordRestrictionsValidatorTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 234
Duplicated Lines 5.98 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 14
loc 234
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A createValidator() 0 4 1
A setValidator() 0 5 1
A testPasswordWithAllParametersSet() 0 4 1
A testPasswordToShortOnlySet() 0 5 1
A testPasswordToLongOnlySet() 0 5 1
A testPasswordMinimumDigitsOnlySet() 0 5 1
A testPasswordMinimumUppercaseOnlySet() 0 5 1
A testPasswordMinimumSpecialCharactersOnlySet() 0 5 1
A testPasswordLengthRangeSet() 0 5 1
A buildAndTestPasswordRestrictions() 0 15 3
A dataPasswordsWithAllParameters() 0 11 1
A dataPasswordsToShort() 0 8 1
A dataPasswordsToLong() 0 7 1
A dataPasswordsMinimumDigits() 0 7 1
A dataPasswordsMinimumUppercase() 7 7 1
A dataPasswordsMinimumSpecialCharacters() 7 7 1
A dataPasswordsLengthRange() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Validator\Constraints;
4
5
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictions;
6
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictionsValidator;
7
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
8
9
/**
10
 * Class PasswordRestrictionsValidatorTest
11
 *
12
 * Unit test for the password restrictions validator, will test with different sets of parameters
13
 * for the validator is possible.
14
 */
15
class PasswordRestrictionsValidatorTest extends ConstraintValidatorTestCase
16
{
17
    const PARAMETER_MIN_LENGTH = 8;
18
    const PARAMETER_MAX_LENGTH = 16;
19
    const PARAMETER_MIN_DIGITS = 3;
20
    const PARAMETER_MIN_UPPERCASE = 2;
21
    const PARAMETER_MIN_SPECIAL_CHARACTERS = 1;
22
23
    /**
24
     * @return PasswordRestrictionsValidator
25
     */
26
    protected function createValidator()
27
    {
28
        return new PasswordRestrictionsValidator(self::PARAMETER_MIN_DIGITS, self::PARAMETER_MIN_UPPERCASE, self::PARAMETER_MIN_SPECIAL_CHARACTERS, self::PARAMETER_MIN_LENGTH, self::PARAMETER_MAX_LENGTH);
29
    }
30
31
    /**
32
     * Set a validator with a limited number of parameters to overrule the default one from createValidator.
33
     *
34
     * @param int $minDigits
35
     * @param int $minUppercase
36
     * @param int $minSpecialCharacters
37
     * @param int $minLength
38
     * @param int $maxLength
39
     */
40
    protected function setValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength)
41
    {
42
        $this->validator = new PasswordRestrictionsValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength);
43
        $this->validator->initialize($this->context);
44
    }
45
46
    /**
47
     * @param string      $password
48
     * @param string|null $message
49
     * @param array       $parameters
50
     * @param null        $code
51
     *
52
     * @dataProvider dataPasswordsWithAllParameters
53
     */
54
    public function testPasswordWithAllParametersSet($password, $message = null, array $parameters = [], $code = null)
55
    {
56
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
57
    }
58
59
    /**
60
     * @param string      $password
61
     * @param string|null $message
62
     * @param array       $parameters
63
     * @param null        $code
64
     *
65
     * @dataProvider dataPasswordsToShort
66
     */
67
    public function testPasswordToShortOnlySet($password, $message = null, array $parameters = [], $code = null)
68
    {
69
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, null);
70
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
71
    }
72
73
    /**
74
     * @param string      $password
75
     * @param string|null $message
76
     * @param array       $parameters
77
     * @param null        $code
78
     *
79
     * @dataProvider dataPasswordsToLong
80
     */
81
    public function testPasswordToLongOnlySet($password, $message = null, array $parameters = [], $code = null)
82
    {
83
        $this->setValidator(null, null, null, null, self::PARAMETER_MAX_LENGTH);
84
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
85
    }
86
87
    /**
88
     * @param string      $password
89
     * @param string|null $message
90
     * @param array       $parameters
91
     * @param null        $code
92
     *
93
     * @dataProvider dataPasswordsMinimumDigits
94
     */
95
    public function testPasswordMinimumDigitsOnlySet($password, $message = null, array $parameters = [], $code = null)
96
    {
97
        $this->setValidator(self::PARAMETER_MIN_DIGITS, null, null, null, null);
98
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
99
    }
100
101
    /**
102
     * @param string      $password
103
     * @param string|null $message
104
     * @param array       $parameters
105
     * @param null        $code
106
     *
107
     * @dataProvider dataPasswordsMinimumUppercase
108
     */
109
    public function testPasswordMinimumUppercaseOnlySet($password, $message = null, array $parameters = [], $code = null)
110
    {
111
        $this->setValidator(null, self::PARAMETER_MIN_UPPERCASE, null, null, null);
112
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
113
    }
114
115
    /**
116
     * @param string      $password
117
     * @param string|null $message
118
     * @param array       $parameters
119
     * @param null        $code
120
     *
121
     * @dataProvider dataPasswordsMinimumSpecialCharacters
122
     */
123
    public function testPasswordMinimumSpecialCharactersOnlySet($password, $message = null, array $parameters = [], $code = null)
124
    {
125
        $this->setValidator(null, null, self::PARAMETER_MIN_SPECIAL_CHARACTERS, null, null);
126
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
127
    }
128
129
    /**
130
     * @param string      $password
131
     * @param string|null $message
132
     * @param array       $parameters
133
     * @param null        $code
134
     *
135
     * @dataProvider dataPasswordsLengthRange
136
     */
137
    public function testPasswordLengthRangeSet($password, $message = null, array $parameters = [], $code = null)
138
    {
139
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, self::PARAMETER_MAX_LENGTH);
140
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
141
    }
142
143
    /**
144
     * Uses the set validator combined with data to assert.
145
     *
146
     * @param string      $password
147
     * @param string|null $message
148
     * @param array       $parameters
149
     * @param null        $code
150
     */
151
    private function buildAndTestPasswordRestrictions($password, $message = null, array $parameters = [], $code = null)
152
    {
153
        $constraint = new PasswordRestrictions();
154
155
        $this->validator->validate($password, $constraint);
156
157
        if ($message && $code) {
0 ignored issues
show
Bug Best Practice introduced by Peter Zentjens
The expression $message of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
158
            $this->buildViolation($message)
159
                ->setCode($code)
160
                ->setParameters($parameters)
161
                ->assertRaised();
162
        } else {
163
            $this->assertNoViolation();
164
        }
165
    }
166
167
    /**
168
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
169
     */
170
    public function dataPasswordsWithAllParameters()
171
    {
172
        return [
173
            ['ABcdef789!'],
174
            ['AB123!q', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
175
            ['AB123!q541kkjhghvhb451', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
176
            ['abCDEFG*', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
177
            ['ab123efg!', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
178
            ['AB123efg', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
179
        ];
180
    }
181
182
    /**
183
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
184
     */
185
    public function dataPasswordsToShort()
186
    {
187
        return [
188
            ['password'],
189
            ['ABC?123', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
190
            ['admin', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
191
        ];
192
    }
193
194
    /**
195
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
196
     */
197
    public function dataPasswordsToLong()
198
    {
199
        return [
200
            ['correct!'],
201
            ['thispasswordistolong', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
202
        ];
203
    }
204
205
    /**
206
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
207
     */
208
    public function dataPasswordsMinimumDigits()
209
    {
210
        return [
211
            ['withdigits123'],
212
            ['nodigits', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
213
        ];
214
    }
215
216
    /**
217
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
218
     */
219 View Code Duplication
    public function dataPasswordsMinimumUppercase()
0 ignored issues
show
Duplication introduced by Peter Zentjens
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221
        return [
222
            ['PassworD'],
223
            ['password', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
224
        ];
225
    }
226
227
    /**
228
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array<string|array<string,integer>>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
229
     */
230 View Code Duplication
    public function dataPasswordsMinimumSpecialCharacters()
0 ignored issues
show
Duplication introduced by Peter Zentjens
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232
        return [
233
            ['password!'],
234
            ['password', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
235
        ];
236
    }
237
238
    /**
239
     * Combine the data of the too long and too short test for an extra length
240
     * range test.
241
     *
242
     * @return array
0 ignored issues
show
Documentation introduced by Peter Zentjens
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
243
     */
244
    public function dataPasswordsLengthRange()
245
    {
246
        return $this->dataPasswordsToLong() + $this->dataPasswordsToShort();
247
    }
248
}
249