Complex classes like DomainValidatorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DomainValidatorTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class DomainValidatorTest extends TestCase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var DomainValidator |
||
| 18 | */ |
||
| 19 | protected $validator; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @inheritdoc |
||
| 23 | */ |
||
| 24 | protected function setUp() |
||
| 29 | |||
| 30 | public static function validDomainProvider() |
||
| 51 | |||
| 52 | public static function validDomainInUrlProvider() |
||
| 76 | |||
| 77 | public static function validDomainIdnProvider() |
||
| 98 | |||
| 99 | public static function validDomainIdnInUrlProvider() |
||
| 123 | |||
| 124 | public static function validDomainAllWithoutIdnProvider() |
||
| 131 | |||
| 132 | public static function validDomainAllOnlyIdnProvider() |
||
| 139 | |||
| 140 | public static function validDomainAllProvider() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $value |
||
| 152 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 153 | * @dataProvider validDomainAllWithoutIdnProvider |
||
| 154 | * @small |
||
| 155 | */ |
||
| 156 | public function testValidDomain($value) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $value |
||
| 163 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 164 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 165 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 166 | * @dataProvider validDomainAllOnlyIdnProvider |
||
| 167 | * @small |
||
| 168 | */ |
||
| 169 | public function testInvalidDomainWithDisabledIdn($value) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $value |
||
| 180 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 181 | * @dataProvider validDomainAllProvider |
||
| 182 | * @small |
||
| 183 | */ |
||
| 184 | public function testValidDomainWithEnabledIdn($value) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 197 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 198 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 199 | * @medium |
||
| 200 | */ |
||
| 201 | public function testDns() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 224 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 225 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 226 | * @medium |
||
| 227 | */ |
||
| 228 | public function testDnsWithEnabledIdn() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 258 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 259 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 260 | * @small |
||
| 261 | */ |
||
| 262 | public function testUnderscore() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 291 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 292 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 293 | * @small |
||
| 294 | */ |
||
| 295 | public function testUnderscoreWithEnabledIdn() |
||
| 305 | |||
| 306 | public static function urlNotAllowedProvider() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $value |
||
| 316 | * @param boolean $expectedResult |
||
| 317 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 318 | * @uses kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 319 | * @uses kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 320 | * @dataProvider urlNotAllowedProvider |
||
| 321 | * @small |
||
| 322 | */ |
||
| 323 | public function testUrlNotAllowed($value, $expectedResult) |
||
| 329 | |||
| 330 | public static function urlNotAllowedProviderWithEnabledIdn() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $value |
||
| 341 | * @param boolean $expectedResult |
||
| 342 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 343 | * @uses kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 344 | * @uses kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 345 | * @dataProvider urlNotAllowedProviderWithEnabledIdn |
||
| 346 | * @small |
||
| 347 | */ |
||
| 348 | public function testUrlNotAllowedWithEnabledIdn($value, $expectedResult) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 361 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 362 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 363 | * @small |
||
| 364 | */ |
||
| 365 | public function testLabelNumberMin() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 381 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 382 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 383 | * @small |
||
| 384 | */ |
||
| 385 | public function testLabelNumberMinWithEnabledIdn() |
||
| 395 | |||
| 396 | public static function invalidDomainProvider($testName) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $value |
||
| 487 | * @param string $expectedErrorMessage |
||
| 488 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 489 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 490 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 491 | * @dataProvider invalidDomainProvider |
||
| 492 | * @small |
||
| 493 | */ |
||
| 494 | public function testInvalidDomain($value, $expectedErrorMessage) |
||
| 499 | |||
| 500 | public static function invalidDomainWithEnabledIdnProvider() |
||
| 501 | { |
||
| 502 | $message = 'the input value is invalid.'; |
||
| 503 | $messageLabelStartEnd = 'Each label of the input value should start and end with letter or number.' . |
||
| 504 | ' The rightmost label of the input value should start with letter.'; |
||
| 505 | $messageLabelTooLong = 'Label of the input value is too long.'; |
||
| 506 | $messageTooLong = 'the input value is too long.'; |
||
| 507 | return array_merge( |
||
| 508 | static::invalidDomainProvider('testInvalidDomainWithEnabledIdn'), |
||
| 509 | [ |
||
| 510 | /* todo it causes fatal error |
||
| 511 | 'IDN, domain name too long, fatal' => [ |
||
| 512 | 'ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.ф.s.s.s.s', |
||
| 513 | $messageTooLong, |
||
| 514 | ], |
||
| 515 | //*/ |
||
| 516 | /* todo it causes fatal error |
||
| 517 | 'IDN, domain name too long, fatal' => [ |
||
| 518 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.' . |
||
| 519 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.' . |
||
| 520 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.' . |
||
| 521 | 'фффффффффффффффффффффффффффффффффффффффффффффффффффффффф.', |
||
| 522 | $messageTooLong, |
||
| 523 | ], |
||
| 524 | //*/ |
||
| 525 | |||
| 526 | 'IDN, first domain name label starts with hyphen' => ['-пример.испытание', $messageLabelStartEnd], |
||
| 527 | 'IDN, first domain name label ends with hyphen' => ['пример-.испытание', $messageLabelStartEnd], |
||
| 528 | 'IDN, last domain name label starts with hyphen' => ['пример.-испытание', $messageLabelStartEnd], |
||
| 529 | 'IDN, last domain name label ends with hyphen' => ['пример.испытание-', $messageLabelStartEnd], |
||
| 530 | |||
| 531 | 'IDN, HTTP, first domain name label starts with hyphen' => [ |
||
| 532 | 'http://-пример.испытание/index.html', |
||
| 533 | $messageLabelStartEnd, |
||
| 534 | ], |
||
| 535 | 'IDN, HTTP, first domain name label ends with hyphen' => [ |
||
| 536 | 'http://пример-.испытание/index.html', |
||
| 537 | $messageLabelStartEnd, |
||
| 538 | ], |
||
| 539 | 'IDN, HTTP, last domain name label starts with hyphen' => [ |
||
| 540 | 'http://пример.-испытание/index.html', |
||
| 541 | $messageLabelStartEnd, |
||
| 542 | ], |
||
| 543 | 'IDN, HTTP, last domain name label ends with hyphen' => [ |
||
| 544 | 'http://пример.испытание-/index.html', |
||
| 545 | $messageLabelStartEnd, |
||
| 546 | ], |
||
| 547 | |||
| 548 | // todo it causes empty array in "idn_to_ascii" $idnaInfo |
||
| 549 | 'IDN, domain name too long' => [ |
||
| 550 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.' . |
||
| 551 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.' . |
||
| 552 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.' . |
||
| 553 | 'ффффффффффффффффффффффффффффффффффффффффффффффффффффффффф.', |
||
| 554 | $messageTooLong, |
||
| 555 | ], |
||
| 556 | 'IDN, domain name label too long' => [ |
||
| 557 | 'фффффффффффффффффффффффффффффффффффффффффффффффффффффффффs', |
||
| 558 | $messageLabelTooLong, |
||
| 559 | ], |
||
| 560 | |||
| 561 | 'IDN, IDNA_ERROR_HYPHEN_3_4' => ['aa--a', $message], |
||
| 562 | 'IDN, IDNA_ERROR_LEADING_COMBINING_MARK' => [static::u('\u0308c'), $message], |
||
| 563 | 'IDN, IDNA_ERROR_PUNYCODE' => ['xn--0', $message], |
||
| 564 | 'IDN, IDNA_ERROR_INVALID_ACE_LABEL' => ['xn--a', $message], |
||
| 565 | 'IDN, IDNA_ERROR_BIDI' => [static::u('0A.\u05D0'), $message], |
||
| 566 | ] |
||
| 567 | ); |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param string $value |
||
| 572 | * @param string $expectedErrorMessage |
||
| 573 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 574 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 575 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 576 | * @dataProvider invalidDomainWithEnabledIdnProvider |
||
| 577 | * @small |
||
| 578 | */ |
||
| 579 | public function testInvalidDomainWithEnabledIdn($value, $expectedErrorMessage) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 592 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 593 | * @small |
||
| 594 | */ |
||
| 595 | public function testCustomErrorMessage() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 606 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 607 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 608 | * @small |
||
| 609 | */ |
||
| 610 | public function testSimpleErrorMessage() |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @covers kdn\yii2\validators\DomainValidator::getDefaultErrorMessages |
||
| 620 | * @covers kdn\yii2\validators\DomainValidator::getErrorMessage |
||
| 621 | * @covers kdn\yii2\validators\DomainValidator::validateValue |
||
| 622 | * @small |
||
| 623 | */ |
||
| 624 | public function testValidateAttributeAndI18n() |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Important: this test should be executed last, because it can remove function "idn_to_ascii". |
||
| 654 | * @covers kdn\yii2\validators\DomainValidator::init |
||
| 655 | * @expectedException \yii\base\InvalidConfigException |
||
| 656 | * @expectedExceptionMessage In order to use IDN validation intl extension must be installed and enabled. |
||
| 657 | * @small |
||
| 658 | */ |
||
| 659 | public function testInitIdnIntlException() |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Add column to array. |
||
| 672 | * @param array $array |
||
| 673 | * @param mixed $value |
||
| 674 | * @return array |
||
| 675 | */ |
||
| 676 | protected static function arrayAddColumn($array, $value) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * \u escape sequence for PHP. |
||
| 689 | * @param string $text |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | protected static function u($text) |
||
| 696 | } |
||
| 697 |