Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
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 null        $code
50
     *
51
     * @dataProvider dataPasswordsWithAllParameters
52
     */
53
    public function testPasswordWithAllParametersSet($password, $message = null, array $parameters = [], $code = null)
54
    {
55
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
56
    }
57
58
    /**
59
     * @param string      $password
60
     * @param string|null $message
61
     * @param null        $code
62
     *
63
     * @dataProvider dataPasswordsToShort
64
     */
65
    public function testPasswordToShortOnlySet($password, $message = null, array $parameters = [], $code = null)
66
    {
67
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, null);
68
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
69
    }
70
71
    /**
72
     * @param string      $password
73
     * @param string|null $message
74
     * @param null        $code
75
     *
76
     * @dataProvider dataPasswordsToLong
77
     */
78
    public function testPasswordToLongOnlySet($password, $message = null, array $parameters = [], $code = null)
79
    {
80
        $this->setValidator(null, null, null, null, self::PARAMETER_MAX_LENGTH);
81
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
82
    }
83
84
    /**
85
     * @param string      $password
86
     * @param string|null $message
87
     * @param null        $code
88
     *
89
     * @dataProvider dataPasswordsMinimumDigits
90
     */
91
    public function testPasswordMinimumDigitsOnlySet($password, $message = null, array $parameters = [], $code = null)
92
    {
93
        $this->setValidator(self::PARAMETER_MIN_DIGITS, null, null, null, null);
94
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
95
    }
96
97
    /**
98
     * @param string      $password
99
     * @param string|null $message
100
     * @param null        $code
101
     *
102
     * @dataProvider dataPasswordsMinimumUppercase
103
     */
104
    public function testPasswordMinimumUppercaseOnlySet($password, $message = null, array $parameters = [], $code = null)
105
    {
106
        $this->setValidator(null, self::PARAMETER_MIN_UPPERCASE, null, null, null);
107
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
108
    }
109
110
    /**
111
     * @param string      $password
112
     * @param string|null $message
113
     * @param null        $code
114
     *
115
     * @dataProvider dataPasswordsMinimumSpecialCharacters
116
     */
117
    public function testPasswordMinimumSpecialCharactersOnlySet($password, $message = null, array $parameters = [], $code = null)
118
    {
119
        $this->setValidator(null, null, self::PARAMETER_MIN_SPECIAL_CHARACTERS, null, null);
120
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
121
    }
122
123
    /**
124
     * @param string      $password
125
     * @param string|null $message
126
     * @param null        $code
127
     *
128
     * @dataProvider dataPasswordsLengthRange
129
     */
130
    public function testPasswordLengthRangeSet($password, $message = null, array $parameters = [], $code = null)
131
    {
132
        $this->setValidator(null, null, null, self::PARAMETER_MIN_LENGTH, self::PARAMETER_MAX_LENGTH);
133
        $this->buildAndTestPasswordRestrictions($password, $message, $parameters, $code);
134
    }
135
136
    /**
137
     * Uses the set validator combined with data to assert.
138
     *
139
     * @param string      $password
140
     * @param string|null $message
141
     * @param null        $code
142
     */
143
    private function buildAndTestPasswordRestrictions($password, $message = null, array $parameters = [], $code = null)
144
    {
145
        $constraint = new PasswordRestrictions();
146
147
        $this->validator->validate($password, $constraint);
148
149
        if ($message && $code) {
150
            $this->buildViolation($message)
151
                ->setCode($code)
152
                ->setParameters($parameters)
153
                ->assertRaised();
154
        } else {
155
            $this->assertNoViolation();
156
        }
157
    }
158
159
    /**
160
     * @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...
161
     */
162
    public function dataPasswordsWithAllParameters()
163
    {
164
        return [
165
            ['ABcdef789!'],
166
            ['AB123!q', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
167
            ['AB123!q541kkjhghvhb451', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
168
            ['abCDEFG*', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
169
            ['ab123efg!', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
170
            ['AB123efg', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
171
        ];
172
    }
173
174
    /**
175
     * @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...
176
     */
177
    public function dataPasswordsToShort()
178
    {
179
        return [
180
            ['password'],
181
            ['ABC?123', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
182
            ['admin', PasswordRestrictions::MESSAGE_MIN_LENGTH, ['{{ min_length }}' => self::PARAMETER_MIN_LENGTH], PasswordRestrictions::INVALID_MIN_LENGTH_ERROR],
183
        ];
184
    }
185
186
    /**
187
     * @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...
188
     */
189
    public function dataPasswordsToLong()
190
    {
191
        return [
192
            ['correct!'],
193
            ['thispasswordistolong', PasswordRestrictions::MESSAGE_MAX_LENGTH, ['{{ max_length }}' => self::PARAMETER_MAX_LENGTH], PasswordRestrictions::INVALID_MAX_LENGTH_ERROR],
194
        ];
195
    }
196
197
    /**
198
     * @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...
199
     */
200
    public function dataPasswordsMinimumDigits()
201
    {
202
        return [
203
            ['withdigits123'],
204
            ['nodigits', PasswordRestrictions::MESSAGE_MIN_DIGITS, ['{{ min_digits }}' => self::PARAMETER_MIN_DIGITS], PasswordRestrictions::INVALID_MIN_DIGITS_ERROR],
205
        ];
206
    }
207
208
    /**
209
     * @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...
210
     */
211 View Code Duplication
    public function dataPasswordsMinimumUppercase()
212
    {
213
        return [
214
            ['PassworD'],
215
            ['password', PasswordRestrictions::MESSAGE_MIN_UPPERCASE, ['{{ min_uppercase }}' => self::PARAMETER_MIN_UPPERCASE], PasswordRestrictions::INVALID_MIN_UPPERCASE_ERROR],
216
        ];
217
    }
218
219
    /**
220
     * @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...
221
     */
222 View Code Duplication
    public function dataPasswordsMinimumSpecialCharacters()
223
    {
224
        return [
225
            ['password!'],
226
            ['password', PasswordRestrictions::MESSAGE_MIN_SPECIAL_CHARACTERS, ['{{ min_special_characters }}' => self::PARAMETER_MIN_SPECIAL_CHARACTERS], PasswordRestrictions::INVALID_MIN_SPECIAL_CHARACTERS_ERROR],
227
        ];
228
    }
229
230
    /**
231
     * Combine the data of the too long and too short test for an extra length
232
     * range test.
233
     *
234
     * @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...
235
     */
236
    public function dataPasswordsLengthRange()
237
    {
238
        return $this->dataPasswordsToLong() + $this->dataPasswordsToShort();
239
    }
240
}
241