Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Constraints/PasswordRestrictionsValidatorTest.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Kunstmaan\MediaBundle\Tests\Validator\Constraints;
3
4
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictions;
5
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictionsValidator;
6
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
7
8
/**
9
 * Class PasswordRestrictionsValidatorTest
10
 *
11
 * Unit test for the password restrictions validator, will test with different sets of parameters
12
 * for the validator is possible.
13
 */
14
class PasswordRestrictionsValidatorTest extends ConstraintValidatorTestCase
15
{
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
29
        return new PasswordRestrictionsValidator(self::PARAMETER_MIN_DIGITS,self::PARAMETER_MIN_UPPERCASE,self::PARAMETER_MIN_SPECIAL_CHARACTERS,self::PARAMETER_MIN_LENGTH,self::PARAMETER_MAX_LENGTH);
30
    }
31
32
    /**
33
     * Set a validator with a limited number of parameters to overrule the default one from createValidator.
34
     *
35
     * @param int $minDigits
36
     * @param int $minUppercase
37
     * @param int $minSpecialCharacters
38
     * @param int $minLength
39
     * @param int $maxLength
40
     */
41
    protected function setValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength)
42
    {
43
44
        $this->validator = new PasswordRestrictionsValidator($minDigits, $minUppercase, $minSpecialCharacters, $minLength, $maxLength);
45
        $this->validator->initialize($this->context);
46
47
    }
48
49
    /**
50
     * @param string $password
51
     * @param null|string $message
52
     * @param array $parameters
53
     * @param null $code
54
     *
55
     * @dataProvider dataPasswordsWithAllParameters
56
     */
57
    public function testPasswordWithAllParametersSet($password, $message = null, array $parameters = [], $code = null)
58
    {
59
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
60
    }
61
62
    /**
63
     * @param string $password
64
     * @param null|string $message
65
     * @param array $parameters
66
     * @param null $code
67
     *
68
     * @dataProvider dataPasswordsToShort
69
     */
70
    public function testPasswordToShortOnlySet($password, $message = null, array $parameters = [], $code = null)
71
    {
72
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, null);
73
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
74
    }
75
76
    /**
77
     * @param string $password
78
     * @param null|string $message
79
     * @param array $parameters
80
     * @param null $code
81
     *
82
     * @dataProvider dataPasswordsToLong
83
     */
84
    public function testPasswordToLongOnlySet($password, $message = null, array $parameters = [], $code = null)
85
    {
86
87
        $this->setValidator(null, null, null, null, self::PARAMETER_MAX_LENGTH);
88
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
89
    }
90
91
    /**
92
     * @param string $password
93
     * @param null|string $message
94
     * @param array $parameters
95
     * @param null $code
96
     *
97
     * @dataProvider dataPasswordsMinimumDigits
98
     */
99
    public function testPasswordMinimumDigitsOnlySet($password, $message = null, array $parameters = [], $code = null)
100
    {
101
102
        $this->setValidator(self::PARAMETER_MIN_DIGITS, null, null, null, null);
103
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
104
    }
105
106
    /**
107
     * @param string $password
108
     * @param null|string $message
109
     * @param array $parameters
110
     * @param null $code
111
     *
112
     * @dataProvider dataPasswordsMinimumUppercase
113
     */
114
    public function testPasswordMinimumUppercaseOnlySet($password, $message = null, array $parameters = [], $code = null)
115
    {
116
117
        $this->setValidator(null, self::PARAMETER_MIN_UPPERCASE, null, null, null);
118
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
119
    }
120
121
    /**
122
     * @param string $password
123
     * @param null|string $message
124
     * @param array $parameters
125
     * @param null $code
126
     *
127
     * @dataProvider dataPasswordsMinimumSpecialCharacters
128
     */
129
    public function testPasswordMinimumSpecialCharactersOnlySet($password, $message = null, array $parameters = [], $code = null)
130
    {
131
132
        $this->setValidator(null, null, self::PARAMETER_MIN_SPECIAL_CHARACTERS, null, null);
133
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
134
    }
135
136
    /**
137
     * @param string $password
138
     * @param null|string $message
139
     * @param array $parameters
140
     * @param null $code
141
     *
142
     * @dataProvider dataPasswordsLengthRange
143
     */
144
    public function testPasswordLengthRangeSet($password, $message = null, array $parameters = [], $code = null)
145
    {
146
147
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, self::PARAMETER_MAX_LENGTH);
148
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
149
150
    }
151
152
    /**
153
     * Uses the set validator combined with data to assert.
154
     *
155
     * @param string $password
156
     * @param null|string $message
157
     * @param array $parameters
158
     * @param null $code
159
     */
160
    private function buildAndTestPasswordRestrictions( $password, $message = null, array $parameters = [], $code = null )
161
    {
162
        $constraint = new PasswordRestrictions();
163
164
        $this->validator->validate($password,$constraint);
165
166
        if ($message && $code) {
167
            $this->buildViolation($message)
168
                ->setCode($code)
169
                ->setParameters($parameters)
170
                ->assertRaised();
171
172
        } else {
173
            $this->assertNoViolation();
174
        }
175
    }
176
177
    /**
178
     * @return array
0 ignored issues
show
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...
179
     */
180
    public function dataPasswordsWithAllParameters()
181
    {
182
183
        return [
184
            ['ABcdef789!'],
185
            ['AB123!q', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
186
            ['AB123!q541kkjhghvhb451', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
187
            ['abCDEFG*', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
188
            ['ab123efg!', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
189
            ['AB123efg', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
190
        ];
191
    }
192
193
    /**
194
     * @return array
0 ignored issues
show
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...
195
     */
196
    public function dataPasswordsToShort()
197
    {
198
        return [
199
            ['password'],
200
            ['ABC?123', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
201
            ['admin', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
202
        ];
203
    }
204
205
    /**
206
     * @return array
0 ignored issues
show
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 dataPasswordsToLong()
209
    {
210
        return [
211
            ['correct!'],
212
            ['thispasswordistolong', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
213
        ];
214
    }
215
216
    /**
217
     * @return array
0 ignored issues
show
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
    public function dataPasswordsMinimumDigits()
220
    {
221
222
        return [
223
            ['withdigits123'],
224
            ['nodigits', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
225
        ];
226
    }
227
228
    /**
229
     * @return array
0 ignored issues
show
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...
230
     */
231 View Code Duplication
    public function dataPasswordsMinimumUppercase()
232
    {
233
234
        return [
235
            ['PassworD'],
236
            ['password', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
237
        ];
238
    }
239
240
    /**
241
     * @return array
0 ignored issues
show
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...
242
     */
243 View Code Duplication
    public function dataPasswordsMinimumSpecialCharacters()
244
    {
245
246
        return [
247
            ['password!'],
248
            ['password', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
249
        ];
250
    }
251
252
    /**
253
     * Combine the data of the too long and too short test for an extra length
254
     * range test.
255
     *
256
     * @return array
0 ignored issues
show
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...
257
     */
258
    public function dataPasswordsLengthRange()
259
    {
260
261
        return $this->dataPasswordsToLong() + $this->dataPasswordsToShort();
262
263
    }
264
265
}