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 |
||
| 12 | class ValidatorTest extends TestCase |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Validator |
||
| 16 | */ |
||
| 17 | protected $validator; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Request |
||
| 21 | */ |
||
| 22 | protected $request; |
||
| 23 | |||
| 24 | public function setUp() |
||
| 25 | { |
||
| 26 | $this->request = Request::createFromEnvironment(Environment::mock([ |
||
| 27 | 'QUERY_STRING' => 'username=a_wurth&password=1234' |
||
| 28 | ])); |
||
| 29 | |||
| 30 | $this->validator = new Validator(); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @expectedException InvalidArgumentException |
||
| 35 | */ |
||
| 36 | public function testValidateWithoutRules() |
||
| 37 | { |
||
| 38 | $this->validator->validate($this->request, [ |
||
| 39 | 'username' |
||
| 40 | ]); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @expectedException InvalidArgumentException |
||
| 45 | */ |
||
| 46 | public function testValidateWithWrongOptionsType() |
||
| 47 | { |
||
| 48 | $this->validator->validate($this->request, [ |
||
| 49 | 'username' => null |
||
| 50 | ]); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @expectedException InvalidArgumentException |
||
| 55 | */ |
||
| 56 | public function testValidateWithWrongRulesType() |
||
| 57 | { |
||
| 58 | $this->validator->validate($this->request, [ |
||
| 59 | 'username' => [ |
||
| 60 | 'rules' => null |
||
| 61 | ] |
||
| 62 | ]); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function testValidate() |
||
| 66 | { |
||
| 67 | $this->validator->validate($this->request, [ |
||
| 68 | 'username' => V::length(6) |
||
| 69 | ]); |
||
| 70 | |||
| 71 | $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
||
| 72 | $this->assertEquals('a_wurth', $this->validator->getValue('username')); |
||
| 73 | $this->assertTrue($this->validator->isValid()); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testValidateWithErrors() |
||
| 77 | { |
||
| 78 | $this->validator->validate($this->request, [ |
||
| 79 | 'username' => V::length(8) |
||
| 80 | ]); |
||
| 81 | |||
| 82 | $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
||
| 83 | $this->assertEquals('a_wurth', $this->validator->getValue('username')); |
||
| 84 | $this->assertFalse($this->validator->isValid()); |
||
| 85 | $this->assertEquals([ |
||
| 86 | 'username' => [ |
||
| 87 | 'length' => '"a_wurth" must have a length greater than 8' |
||
| 88 | ] |
||
| 89 | ], $this->validator->getErrors()); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testValidateWithIndexedErrors() |
||
| 93 | { |
||
| 94 | $this->validator->setStoreErrorsWithRules(false); |
||
| 95 | $this->validator->validate($this->request, [ |
||
| 96 | 'username' => V::length(8) |
||
| 97 | ]); |
||
| 98 | |||
| 99 | $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
||
| 100 | $this->assertEquals('a_wurth', $this->validator->getValue('username')); |
||
| 101 | $this->assertFalse($this->validator->isValid()); |
||
| 102 | $this->assertEquals([ |
||
| 103 | 'username' => [ |
||
| 104 | '"a_wurth" must have a length greater than 8' |
||
| 105 | ] |
||
| 106 | ], $this->validator->getErrors()); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testValidateWithCustomDefaultMessage() |
||
| 110 | { |
||
| 111 | $this->validator->setDefaultMessages([ |
||
| 112 | 'length' => 'Too short!' |
||
| 113 | ]); |
||
| 114 | |||
| 115 | $this->validator->validate($this->request, [ |
||
| 116 | 'username' => V::length(8) |
||
| 117 | ]); |
||
| 118 | |||
| 119 | $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues()); |
||
| 120 | $this->assertEquals('a_wurth', $this->validator->getValue('username')); |
||
| 121 | $this->assertFalse($this->validator->isValid()); |
||
| 122 | $this->assertEquals([ |
||
| 123 | 'username' => [ |
||
| 124 | 'length' => 'Too short!' |
||
| 125 | ] |
||
| 126 | ], $this->validator->getErrors()); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testValidateWithCustomGlobalMessages() |
||
| 130 | { |
||
| 131 | $this->validator->validate($this->request, [ |
||
| 132 | 'username' => V::length(8), |
||
| 133 | 'password' => V::length(8) |
||
| 134 | ], [ |
||
| 135 | 'length' => 'Too short!' |
||
| 136 | ]); |
||
| 137 | |||
| 138 | $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
||
| 139 | $this->assertFalse($this->validator->isValid()); |
||
| 140 | $this->assertEquals([ |
||
| 141 | 'username' => [ |
||
| 142 | 'length' => 'Too short!' |
||
| 143 | ], |
||
| 144 | 'password' => [ |
||
| 145 | 'length' => 'Too short!' |
||
| 146 | ] |
||
| 147 | ], $this->validator->getErrors()); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function testValidateWithCustomDefaultAndGlobalMessages() |
||
| 151 | { |
||
| 152 | $this->validator->setDefaultMessages([ |
||
| 153 | 'length' => 'Too short!' |
||
| 154 | ]); |
||
| 155 | |||
| 156 | $this->validator->validate($this->request, [ |
||
| 157 | 'username' => V::length(8), |
||
| 158 | 'password' => V::length(8)->alpha() |
||
| 159 | ], [ |
||
| 160 | 'alpha' => 'Only letters are allowed' |
||
| 161 | ]); |
||
| 162 | |||
| 163 | $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
||
| 164 | $this->assertFalse($this->validator->isValid()); |
||
| 165 | $this->assertEquals([ |
||
| 166 | 'username' => [ |
||
| 167 | 'length' => 'Too short!' |
||
| 168 | ], |
||
| 169 | 'password' => [ |
||
| 170 | 'length' => 'Too short!', |
||
| 171 | 'alpha' => 'Only letters are allowed' |
||
| 172 | ] |
||
| 173 | ], $this->validator->getErrors()); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @expectedException InvalidArgumentException |
||
| 178 | * @expectedExceptionMessage Expected custom individual messages to be of type array, string given |
||
| 179 | */ |
||
| 180 | public function testValidateWithWrongCustomIndividualMessagesType() |
||
| 181 | { |
||
| 182 | $this->validator->validate($this->request, [ |
||
| 183 | 'username' => [ |
||
| 184 | 'rules' => V::length(8), |
||
| 185 | 'messages' => '' |
||
| 186 | ] |
||
| 187 | ]); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function testValidateWithCustomIndividualMessage() |
||
| 191 | { |
||
| 192 | $this->validator->validate($this->request, [ |
||
| 193 | 'username' => [ |
||
| 194 | 'rules' => V::length(8), |
||
| 195 | 'messages' => [ |
||
| 196 | 'length' => 'Too short!' |
||
| 197 | ] |
||
| 198 | ], |
||
| 199 | 'password' => V::length(8) |
||
| 200 | ]); |
||
| 201 | |||
| 202 | $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
||
| 203 | $this->assertFalse($this->validator->isValid()); |
||
| 204 | $this->assertEquals([ |
||
| 205 | 'username' => [ |
||
| 206 | 'length' => 'Too short!' |
||
| 207 | ], |
||
| 208 | 'password' => [ |
||
| 209 | 'length' => '"1234" must have a length greater than 8' |
||
| 210 | ] |
||
| 211 | ], $this->validator->getErrors()); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @expectedException InvalidArgumentException |
||
| 216 | * @expectedExceptionMessage Expected custom message to be of type string, integer given |
||
| 217 | */ |
||
| 218 | public function testValidateWithWrongCustomSingleMessageType() |
||
| 219 | { |
||
| 220 | $this->validator->validate($this->request, [ |
||
| 221 | 'username' => [ |
||
| 222 | 'rules' => V::length(8)->alnum(), |
||
| 223 | 'message' => 10 |
||
| 224 | ] |
||
| 225 | ]); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function testValidateWithCustomSingleMessage() |
||
| 229 | { |
||
| 230 | $this->validator->validate($this->request, [ |
||
| 231 | 'username' => [ |
||
| 232 | 'rules' => V::length(8)->alnum(), |
||
| 233 | 'message' => 'Bad username.', |
||
| 234 | 'messages' => [ |
||
| 235 | 'length' => 'Too short!' |
||
| 236 | ] |
||
| 237 | ], |
||
| 238 | 'password' => [ |
||
| 239 | 'rules' => V::length(8), |
||
| 240 | 'messages' => [ |
||
| 241 | 'length' => 'Too short!' |
||
| 242 | ] |
||
| 243 | ] |
||
| 244 | ]); |
||
| 245 | |||
| 246 | $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues()); |
||
| 247 | $this->assertFalse($this->validator->isValid()); |
||
| 248 | $this->assertEquals([ |
||
| 249 | 'username' => [ |
||
| 250 | 'Bad username.' |
||
| 251 | ], |
||
| 252 | 'password' => [ |
||
| 253 | 'length' => 'Too short!' |
||
| 254 | ] |
||
| 255 | ], $this->validator->getErrors()); |
||
| 256 | } |
||
| 257 | |||
| 258 | public function testIsValidWithErrors() |
||
| 259 | { |
||
| 260 | $this->validator->setErrors(['error']); |
||
| 261 | |||
| 262 | $this->assertFalse($this->validator->isValid()); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function testIsValidWithoutErrors() |
||
| 266 | { |
||
| 267 | $this->validator->setErrors([]); |
||
| 268 | |||
| 269 | $this->assertTrue($this->validator->isValid()); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function testAddError() |
||
| 273 | { |
||
| 274 | $this->validator->addError('param', 'message'); |
||
| 275 | |||
| 276 | $this->assertEquals([ |
||
| 277 | 'param' => [ |
||
| 278 | 'message' |
||
| 279 | ] |
||
| 280 | ], $this->validator->getErrors()); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function testAddErrors() |
||
| 284 | { |
||
| 285 | $this->validator->addErrors('param', ['message1', 'message2']); |
||
| 286 | |||
| 287 | $this->assertEquals([ |
||
| 288 | 'param' => [ |
||
| 289 | 'message1', |
||
| 290 | 'message2' |
||
| 291 | ] |
||
| 292 | ], $this->validator->getErrors()); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function testGetFirstError() |
||
| 296 | { |
||
| 297 | $this->assertEquals('', $this->validator->getFirstError('username')); |
||
| 298 | |||
| 299 | $this->validator->setErrors([ |
||
| 300 | 'param' => [ |
||
| 301 | 'notBlank' => 'Required' |
||
| 302 | ], |
||
| 303 | 'username' => [ |
||
| 304 | 'alnum' => 'Only letters and numbers are allowed', |
||
| 305 | 'length' => 'Too short!' |
||
| 306 | ] |
||
| 307 | ]); |
||
| 308 | |||
| 309 | $this->assertEquals('Only letters and numbers are allowed', $this->validator->getFirstError('username')); |
||
| 310 | |||
| 311 | $this->validator->setErrors([ |
||
| 312 | 'param' => [ |
||
| 313 | 'Required' |
||
| 314 | ], |
||
| 315 | 'username' => [ |
||
| 316 | 'This field is required', |
||
| 317 | 'Only letters and numbers are allowed' |
||
| 318 | ] |
||
| 319 | ]); |
||
| 320 | |||
| 321 | $this->assertEquals('This field is required', $this->validator->getFirstError('username')); |
||
| 322 | } |
||
| 323 | |||
| 324 | public function testGetParamErrors() |
||
| 325 | { |
||
| 326 | $this->assertEquals([], $this->validator->getParamErrors('username')); |
||
| 327 | |||
| 328 | $this->validator->setErrors([ |
||
| 329 | 'param' => [ |
||
| 330 | 'Required' |
||
| 331 | ], |
||
| 332 | 'username' => [ |
||
| 333 | 'This field is required', |
||
| 334 | 'Only letters and numbers are allowed' |
||
| 335 | ] |
||
| 336 | ]); |
||
| 337 | |||
| 338 | $this->assertEquals([ |
||
| 339 | 'This field is required', |
||
| 340 | 'Only letters and numbers are allowed' |
||
| 341 | ], $this->validator->getParamErrors('username')); |
||
| 342 | } |
||
| 343 | |||
| 344 | public function testGetParamRuleError() |
||
| 345 | { |
||
| 346 | $this->assertEquals('', $this->validator->getParamRuleError('username', 'length')); |
||
| 347 | |||
| 348 | $this->validator->setErrors([ |
||
| 349 | 'username' => [ |
||
| 350 | 'alnum' => 'Only letters and numbers are allowed', |
||
| 351 | 'length' => 'Too short!' |
||
| 352 | ] |
||
| 353 | ]); |
||
| 354 | |||
| 355 | $this->assertEquals('Too short!', $this->validator->getParamRuleError('username', 'length')); |
||
| 356 | } |
||
| 357 | |||
| 358 | public function testSetData() |
||
| 359 | { |
||
| 360 | $this->assertEquals([], $this->validator->getData()); |
||
| 361 | |||
| 362 | $this->validator->setData([ |
||
| 363 | 'username' => 'awurth', |
||
| 364 | 'password' => 'pass' |
||
| 365 | ]); |
||
| 366 | |||
| 367 | $this->assertEquals([ |
||
| 368 | 'username' => 'awurth', |
||
| 369 | 'password' => 'pass' |
||
| 370 | ], $this->validator->getData()); |
||
| 371 | } |
||
| 372 | |||
| 373 | public function testSetValues() |
||
| 374 | { |
||
| 375 | $this->assertEquals([], $this->validator->getValues()); |
||
| 376 | |||
| 377 | $this->validator->setValues([ |
||
| 378 | 'username' => 'awurth', |
||
| 379 | 'password' => 'pass' |
||
| 380 | ]); |
||
| 381 | |||
| 382 | $this->assertEquals([ |
||
| 383 | 'username' => 'awurth', |
||
| 384 | 'password' => 'pass' |
||
| 385 | ], $this->validator->getValues()); |
||
| 386 | } |
||
| 387 | |||
| 388 | public function testSetDefaultMessage() |
||
| 389 | { |
||
| 390 | $this->assertEquals([], $this->validator->getDefaultMessages()); |
||
| 391 | |||
| 392 | $this->validator->setDefaultMessage('length', 'Too short!'); |
||
| 393 | |||
| 394 | $this->assertEquals([ |
||
| 395 | 'length' => 'Too short!' |
||
| 396 | ], $this->validator->getDefaultMessages()); |
||
| 397 | } |
||
| 398 | |||
| 399 | public function testSetParamErrors() |
||
| 400 | { |
||
| 401 | $this->assertEquals([], $this->validator->getErrors()); |
||
| 402 | |||
| 403 | $this->validator->setParamErrors('username', [ |
||
| 404 | 'notBlank' => 'Required', |
||
| 405 | 'length' => 'Too short!' |
||
| 406 | ]); |
||
| 407 | |||
| 408 | $this->assertEquals([ |
||
| 409 | 'username' => [ |
||
| 410 | 'notBlank' => 'Required', |
||
| 411 | 'length' => 'Too short!' |
||
| 412 | ] |
||
| 413 | ], $this->validator->getErrors()); |
||
| 414 | } |
||
| 415 | |||
| 416 | public function testSetStoreErrorsWithRules() |
||
| 417 | { |
||
| 418 | $this->assertTrue($this->validator->getStoreErrorsWithRules()); |
||
| 419 | |||
| 420 | $this->validator->setStoreErrorsWithRules(false); |
||
| 421 | |||
| 422 | $this->assertFalse($this->validator->getStoreErrorsWithRules()); |
||
| 423 | } |
||
| 424 | } |
||
| 425 |