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 null|string $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 null|string $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 null|string $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 null|string $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 null|string $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 null|string $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 null|string $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 null|string $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) { |
||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
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
|
|||
218 | */ |
||
219 | View Code Duplication | public function dataPasswordsMinimumUppercase() |
|
0 ignored issues
–
show
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
|
|||
229 | */ |
||
230 | View Code Duplication | public function dataPasswordsMinimumSpecialCharacters() |
|
0 ignored issues
–
show
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
|
|||
243 | */ |
||
244 | public function dataPasswordsLengthRange() |
||
245 | { |
||
246 | return $this->dataPasswordsToLong() + $this->dataPasswordsToShort(); |
||
247 | } |
||
248 | } |
||
249 |
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.