Complex classes like AssertTest 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.
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 AssertTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class AssertTest extends \PHPUnit_Framework_TestCase |
||
| 8 | { |
||
| 9 | public static function dataInvalidFloat() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @dataProvider dataInvalidFloat |
||
| 23 | */ |
||
| 24 | public function testInvalidFloat($nonFloat) |
||
| 29 | |||
| 30 | public function testValidFloat() |
||
| 31 | { |
||
| 32 | Assertion::float(1.0); |
||
| 33 | Assertion::float(0.1); |
||
| 34 | Assertion::float(-1.1); |
||
| 35 | } |
||
| 36 | |||
| 37 | public static function dataInvalidInteger() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @dataProvider dataInvalidInteger |
||
| 52 | */ |
||
| 53 | public function testInvalidInteger($nonInteger) |
||
| 58 | |||
| 59 | public function testValidInteger() |
||
| 64 | |||
| 65 | public function testValidIntegerish() |
||
| 70 | |||
| 71 | public static function dataInvalidIntegerish() |
||
| 72 | { |
||
| 73 | return array( |
||
| 74 | array(1.23), |
||
| 75 | array(false), |
||
| 76 | array("test"), |
||
| 77 | array(null), |
||
| 78 | array("1.23"), |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @dataProvider dataInvalidIntegerish |
||
| 84 | */ |
||
| 85 | public function testInvalidIntegerish($nonInteger) |
||
| 90 | |||
| 91 | public function testValidBoolean() |
||
| 96 | |||
| 97 | public function testInvalidBoolean() |
||
| 102 | |||
| 103 | public function testInvalidScalar() |
||
| 104 | { |
||
| 105 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_SCALAR); |
||
| 106 | Assertion::scalar(new \stdClass); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testValidScalar() |
||
| 110 | { |
||
| 111 | Assertion::scalar("foo"); |
||
| 112 | Assertion::scalar(52); |
||
| 113 | Assertion::scalar(12.34); |
||
| 114 | Assertion::scalar(false); |
||
| 115 | } |
||
| 116 | |||
| 117 | public static function dataInvalidNotEmpty() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @dataProvider dataInvalidNotEmpty |
||
| 130 | */ |
||
| 131 | public function testInvalidNotEmpty($value) |
||
| 136 | |||
| 137 | public function testNotEmpty() |
||
| 144 | |||
| 145 | public function testEmpty() |
||
| 146 | { |
||
| 147 | Assertion::noContent(""); |
||
| 148 | Assertion::noContent(0); |
||
| 149 | Assertion::noContent(false); |
||
| 150 | Assertion::noContent( array() ); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function dataInvalidEmpty() |
||
| 154 | { |
||
| 155 | return array( |
||
| 156 | array("foo"), |
||
| 157 | array(true), |
||
| 158 | array(12), |
||
| 159 | array( array('foo') ), |
||
| 160 | array( new \stdClass() ), |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @dataProvider dataInvalidEmpty |
||
| 166 | */ |
||
| 167 | public function testInvalidEmpty($value) |
||
| 168 | { |
||
| 169 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_EMPTY); |
||
| 170 | Assertion::noContent($value); |
||
| 171 | } |
||
| 172 | |||
| 173 | public static function dataInvalidNull() |
||
| 174 | { |
||
| 175 | return array( |
||
| 176 | array("foo"), |
||
| 177 | array(""), |
||
| 178 | array(false), |
||
| 179 | array(12), |
||
| 180 | array(0), |
||
| 181 | array(array()), |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @dataProvider dataInvalidNull |
||
| 187 | */ |
||
| 188 | public function testInvalidNull($value) |
||
| 189 | { |
||
| 190 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NOT_NULL); |
||
| 191 | Assertion::null($value); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function testNull() |
||
| 195 | { |
||
| 196 | Assertion::null(null); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function testNotNull() |
||
| 207 | |||
| 208 | public function testInvalidNotNull() |
||
| 213 | |||
| 214 | public function testString() |
||
| 215 | { |
||
| 216 | Assertion::string("test-string"); |
||
| 217 | Assertion::string(""); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @dataProvider dataInvalidString |
||
| 222 | */ |
||
| 223 | public function testInvalidString($invalidString) |
||
| 224 | { |
||
| 225 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); |
||
| 226 | Assertion::string($invalidString); |
||
| 227 | } |
||
| 228 | |||
| 229 | public static function dataInvalidString() |
||
| 230 | { |
||
| 231 | return array( |
||
| 232 | array(1.23), |
||
| 233 | array(false), |
||
| 234 | array(new \ArrayObject), |
||
| 235 | array(null), |
||
| 236 | array(10), |
||
| 237 | array(true), |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function testInvalidRegex() |
||
| 246 | |||
| 247 | public function testInvalidRegexValueNotString() |
||
| 252 | |||
| 253 | public function testInvalidMinLength() |
||
| 258 | |||
| 259 | public function testValidMinLength() |
||
| 267 | |||
| 268 | public function testInvalidMaxLength() |
||
| 273 | |||
| 274 | public function testValidMaxLength() |
||
| 281 | |||
| 282 | public function testInvalidBetweenLengthMin() |
||
| 287 | |||
| 288 | public function testInvalidBetweenLengthMax() |
||
| 293 | |||
| 294 | public function testValidBetweenLength() |
||
| 299 | |||
| 300 | public function testInvalidStartsWith() |
||
| 305 | |||
| 306 | public function testInvalidStartsWithDueToWrongEncoding() |
||
| 311 | |||
| 312 | public function testValidStartsWith() |
||
| 319 | |||
| 320 | public function testInvalidEndsWith() |
||
| 321 | { |
||
| 322 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
||
| 323 | Assertion::endsWith("foo", "bar"); |
||
| 324 | } |
||
| 325 | |||
| 326 | public function testInvalidEndsWithDueToWrongEncoding() |
||
| 327 | { |
||
| 328 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
||
| 329 | Assertion::endsWith("址", "址址", null, null, 'ASCII'); |
||
| 330 | } |
||
| 331 | |||
| 332 | public function testValidEndsWith() |
||
| 333 | { |
||
| 334 | Assertion::endsWith("foo", "foo"); |
||
| 335 | Assertion::endsWith("sonderbar", "bar"); |
||
| 336 | Assertion::endsWith("opp", "p"); |
||
| 337 | Assertion::endsWith("foo址", "址"); |
||
| 338 | } |
||
| 339 | |||
| 340 | public function testInvalidContains() |
||
| 345 | |||
| 346 | public function testValidContains() |
||
| 351 | |||
| 352 | public function testInvalidChoice() |
||
| 357 | |||
| 358 | public function testValidChoice() |
||
| 362 | |||
| 363 | public function testInvalidInArray() |
||
| 368 | |||
| 369 | public function testValidInArray() |
||
| 373 | |||
| 374 | public function testInvalidNumeric() |
||
| 379 | |||
| 380 | public function testValidNumeric() |
||
| 386 | |||
| 387 | public static function dataInvalidArray() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @dataProvider dataInvalidArray |
||
| 402 | */ |
||
| 403 | public function testInvalidArray($value) |
||
| 408 | |||
| 409 | public function testValidArray() |
||
| 415 | |||
| 416 | public function testInvalidKeyExists() |
||
| 421 | |||
| 422 | public function testValidKeyExists() |
||
| 426 | |||
| 427 | public function testInvalidKeyNotExists() |
||
| 428 | { |
||
| 429 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_NOT_EXISTS); |
||
| 430 | Assertion::keyNotExists(array("foo" => "bar"), "foo"); |
||
| 431 | } |
||
| 432 | |||
| 433 | public function testValidKeyNotExists() |
||
| 434 | { |
||
| 435 | Assertion::keyNotExists(array("foo" => "bar"), "baz"); |
||
| 436 | } |
||
| 437 | |||
| 438 | public static function dataInvalidNotBlank() |
||
| 439 | { |
||
| 440 | return array( |
||
| 441 | array(""), |
||
| 442 | array(" "), |
||
| 443 | array("\t"), |
||
| 444 | array("\n"), |
||
| 445 | array("\r"), |
||
| 446 | array(false), |
||
| 447 | array(null), |
||
| 448 | array( array() ), |
||
| 449 | ); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @dataProvider dataInvalidNotBlank |
||
| 454 | */ |
||
| 455 | public function testInvalidNotBlank($notBlank) |
||
| 456 | { |
||
| 457 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_BLANK); |
||
| 458 | Assertion::notBlank($notBlank); |
||
| 459 | } |
||
| 460 | |||
| 461 | public function testValidNotBlank() |
||
| 465 | |||
| 466 | public function testInvalidNotInstanceOf() |
||
| 467 | { |
||
| 468 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_INSTANCE_OF); |
||
| 469 | Assertion::notIsInstanceOf(new \stdClass, 'stdClass'); |
||
| 470 | } |
||
| 471 | |||
| 472 | public function testValidNotIsInstanceOf() |
||
| 473 | { |
||
| 474 | Assertion::notIsInstanceOf(new \stdClass, 'PDO'); |
||
| 475 | } |
||
| 476 | |||
| 477 | public function testInvalidInstanceOf() |
||
| 482 | |||
| 483 | public function testValidInstanceOf() |
||
| 487 | |||
| 488 | public function testInvalidSubclassOf() |
||
| 493 | |||
| 494 | public function testValidSubclassOf() |
||
| 498 | |||
| 499 | public function testInvalidRange() |
||
| 505 | |||
| 506 | public function testValidRange() |
||
| 513 | |||
| 514 | public function testInvalidEmail() |
||
| 519 | |||
| 520 | public function testValidEmail() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @dataProvider dataInvalidUrl |
||
| 527 | */ |
||
| 528 | public function testInvalidUrl($url) |
||
| 529 | { |
||
| 530 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_URL); |
||
| 531 | |||
| 532 | Assertion::url($url); |
||
| 533 | } |
||
| 534 | |||
| 535 | public static function dataInvalidUrl() |
||
| 536 | { |
||
| 537 | return array( |
||
| 538 | 'null value' => array(""), |
||
| 539 | 'empty string' => array(" "), |
||
| 540 | 'no scheme' => array("url.de"), |
||
| 541 | 'unsupported scheme' => array("git://url.de"), |
||
| 542 | 'Http with query (no / between tld und ?)' => array("http://example.org?do=something"), |
||
| 543 | 'Http with query and port (no / between port und ?)' => array("http://example.org:8080?do=something"), |
||
| 544 | ); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @dataProvider dataValidUrl |
||
| 549 | */ |
||
| 550 | public function testValidUrl($url) |
||
| 551 | { |
||
| 552 | Assertion::url($url); |
||
| 553 | } |
||
| 554 | |||
| 555 | public static function dataValidUrl() |
||
| 556 | { |
||
| 557 | return array( |
||
| 558 | 'straight with Http' => array("http://example.org"), |
||
| 559 | 'Http with path' => array("http://example.org/do/something"), |
||
| 560 | 'Http with query' => array("http://example.org/index.php?do=something"), |
||
| 561 | 'Http with port' => array("http://example.org:8080"), |
||
| 562 | 'Http with all possibilities' => array("http://example.org:8080/do/something/index.php?do=something"), |
||
| 563 | 'straight with Https' => array("https://example.org"), |
||
| 564 | ); |
||
| 565 | } |
||
| 566 | |||
| 567 | public function testInvalidDigit() |
||
| 572 | |||
| 573 | public function testValidDigit() |
||
| 579 | |||
| 580 | public function testValidAlnum() |
||
| 587 | |||
| 588 | public function testInvalidAlnum() |
||
| 593 | |||
| 594 | public function testValidTrue() |
||
| 598 | |||
| 599 | public function testInvalidTrue() |
||
| 604 | |||
| 605 | public function testValidFalse() |
||
| 609 | |||
| 610 | public function testInvalidFalse() |
||
| 615 | |||
| 616 | public function testInvalidClass() |
||
| 621 | |||
| 622 | public function testValidClass() |
||
| 626 | |||
| 627 | public function testSame() |
||
| 635 | |||
| 636 | public function testEq() |
||
| 644 | |||
| 645 | public function testNotEq() |
||
| 652 | |||
| 653 | public function testNotSame() |
||
| 654 | { |
||
| 655 | Assertion::notSame("1", 2); |
||
| 656 | Assertion::notSame(new \stdClass(), array()); |
||
| 657 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NOT_SAME); |
||
| 658 | Assertion::notSame(1, 1); |
||
| 659 | } |
||
| 660 | |||
| 661 | public function testNotInArray() |
||
| 662 | { |
||
| 663 | Assertion::notInArray(6, range(1, 5)); |
||
| 664 | |||
| 665 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_VALUE_IN_ARRAY); |
||
| 666 | Assertion::notInArray(1, range(1, 5)); |
||
| 667 | Assertion::notInArray(range('a', 'c'), range('a', 'd')); |
||
| 668 | } |
||
| 669 | |||
| 670 | public function testMin() |
||
| 671 | { |
||
| 672 | Assertion::min(1, 1); |
||
| 673 | Assertion::min(2, 1); |
||
| 674 | Assertion::min(2.5, 1); |
||
| 675 | |||
| 676 | try { |
||
| 692 | |||
| 693 | public function testMax() |
||
| 715 | |||
| 716 | public function testNullOr() |
||
| 721 | |||
| 722 | public function testNullOrWithNoValueThrows() |
||
| 727 | |||
| 728 | public function testLength() |
||
| 733 | |||
| 734 | public static function dataLengthUtf8Characters() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @dataProvider dataLengthUtf8Characters |
||
| 744 | */ |
||
| 745 | public function testLenghtUtf8Characters($value, $expected) |
||
| 749 | |||
| 750 | public function testLengthFailed() |
||
| 755 | |||
| 756 | public function testLengthFailedForWrongEncoding() |
||
| 761 | |||
| 762 | public function testLengthValidForGivenEncoding() |
||
| 766 | |||
| 767 | public function testFile() |
||
| 771 | |||
| 772 | public function testFileWithEmptyFilename() |
||
| 777 | |||
| 778 | public function testFileDoesNotExists() |
||
| 783 | |||
| 784 | public function testDirectory() |
||
| 791 | |||
| 792 | public function testReadable() |
||
| 799 | |||
| 800 | public function testWriteable() |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @expectedException \BadMethodCallException |
||
| 810 | * @expectedExceptionMessage No assertion |
||
| 811 | */ |
||
| 812 | public function testFailedNullOrMethodCall() |
||
| 816 | |||
| 817 | public function testImplementsInterface() |
||
| 830 | |||
| 831 | public function testImplementsInterfaceWithClassObject() |
||
| 846 | |||
| 847 | /** |
||
| 848 | * @dataProvider isJsonStringDataprovider |
||
| 849 | */ |
||
| 850 | public function testIsJsonString($content) |
||
| 854 | |||
| 855 | public static function isJsonStringDataprovider() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * @dataProvider isJsonStringInvalidStringDataprovider |
||
| 867 | */ |
||
| 868 | public function testIsJsonStringExpectingException($invalidString) |
||
| 873 | |||
| 874 | public static function isJsonStringInvalidStringDataprovider() |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @dataProvider providesValidUuids |
||
| 884 | */ |
||
| 885 | public function testValidUuids($uuid) |
||
| 889 | |||
| 890 | /** |
||
| 891 | * @dataProvider providesInvalidUuids |
||
| 892 | */ |
||
| 893 | public function testInvalidUuids($uuid) |
||
| 898 | |||
| 899 | static public function providesValidUuids() |
||
| 910 | |||
| 911 | static public function providesInvalidUuids() |
||
| 921 | |||
| 922 | /** |
||
| 923 | * @dataProvider providesValidE164s |
||
| 924 | */ |
||
| 925 | public function testValidE164s($e164) |
||
| 929 | |||
| 930 | /** |
||
| 931 | * @dataProvider providesInvalidE164s |
||
| 932 | */ |
||
| 933 | public function testInvalidE164s($e164) |
||
| 938 | |||
| 939 | static public function providesValidE164s() |
||
| 947 | |||
| 948 | static public function providesInvalidE164s() |
||
| 955 | |||
| 956 | public function testValidNotEmptyKey() |
||
| 960 | |||
| 961 | /** |
||
| 962 | * @dataProvider invalidNotEmptyKeyDataprovider |
||
| 963 | */ |
||
| 964 | public function testInvalidNotEmptyKey($invalidArray, $key) |
||
| 969 | |||
| 970 | public static function invalidNotEmptyKeyDataprovider() |
||
| 977 | |||
| 978 | public function testAllWithSimpleAssertion() |
||
| 982 | |||
| 983 | public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
| 988 | |||
| 989 | public function testAllWithComplexAssertion() |
||
| 993 | |||
| 994 | public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
| 1000 | |||
| 1001 | public function testAllWithNoValueThrows() |
||
| 1006 | |||
| 1007 | public function testValidCount() |
||
| 1012 | |||
| 1013 | public static function dataInvalidCount() |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * @dataProvider dataInvalidCount |
||
| 1023 | */ |
||
| 1024 | public function testInvalidCount($countable, $count) |
||
| 1029 | |||
| 1030 | public function testChoicesNotEmpty() |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * @dataProvider invalidChoicesProvider |
||
| 1040 | */ |
||
| 1041 | public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode) |
||
| 1049 | |||
| 1050 | public function invalidChoicesProvider() |
||
| 1058 | |||
| 1059 | public function testIsObject() |
||
| 1063 | |||
| 1064 | public function testIsObjectExpectingException() |
||
| 1069 | |||
| 1070 | public function testMethodExists() |
||
| 1074 | |||
| 1075 | public function testMethodExistsFailure() |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * @test |
||
| 1083 | */ |
||
| 1084 | public function it_passes_values_and_constraints_to_exception() |
||
| 1095 | |||
| 1096 | public function testLessThan() |
||
| 1103 | |||
| 1104 | public function invalidLessProvider() |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @dataProvider invalidLessProvider |
||
| 1118 | */ |
||
| 1119 | public function testLessThanThrowsException($value, $limit) |
||
| 1124 | |||
| 1125 | public function testLessOrEqualThan() |
||
| 1135 | |||
| 1136 | public function invalidLessOrEqualProvider() |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * @dataProvider invalidLessOrEqualProvider |
||
| 1147 | */ |
||
| 1148 | public function testLessOrEqualThanThrowsException($value, $limit) |
||
| 1153 | |||
| 1154 | public function testGreaterThan() |
||
| 1161 | |||
| 1162 | public function invalidGreaterProvider() |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * @dataProvider validDateProvider |
||
| 1176 | */ |
||
| 1177 | public function testValidDate($value, $format) |
||
| 1181 | |||
| 1182 | public function validDateProvider() |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * @dataProvider invalidGreaterProvider |
||
| 1194 | */ |
||
| 1195 | public function testGreaterThanThrowsException($value, $limit) |
||
| 1200 | |||
| 1201 | public function testGreaterOrEqualThan() |
||
| 1211 | |||
| 1212 | public function invalidGreaterOrEqualProvider() |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * @dataProvider invalidGreaterOrEqualProvider |
||
| 1223 | * |
||
| 1224 | * @param mixed $value |
||
| 1225 | * @param mixed $limit |
||
| 1226 | */ |
||
| 1227 | public function testGreaterOrEqualThanThrowsException($value, $limit) |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * @dataProvider invalidDateProvider |
||
| 1235 | */ |
||
| 1236 | public function testInvalidDate($value, $format) |
||
| 1241 | |||
| 1242 | public function invalidDateProvider() |
||
| 1250 | |||
| 1251 | public function testInvalidTraversable() |
||
| 1256 | |||
| 1257 | public function testInvalidArrayAccessible() |
||
| 1262 | |||
| 1263 | public function testInvalidCallable() |
||
| 1268 | |||
| 1269 | public function testValidCallable() |
||
| 1277 | |||
| 1278 | public function testInvalidSatisfy() |
||
| 1285 | |||
| 1286 | public function testValidSatisfy() |
||
| 1300 | |||
| 1301 | /** |
||
| 1302 | * @dataProvider validIpProvider |
||
| 1303 | */ |
||
| 1304 | public function testValidIp($value) { |
||
| 1307 | |||
| 1308 | public function validIpProvider() { |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * @dataProvider invalidIpProvider |
||
| 1319 | */ |
||
| 1320 | public function testInvalidIp($value, $flag = null) { |
||
| 1324 | |||
| 1325 | public function invalidIpProvider() { |
||
| 1336 | |||
| 1337 | public function testValidIpv4() { |
||
| 1340 | |||
| 1341 | public function testInvalidIpv4() { |
||
| 1345 | |||
| 1346 | public function testValidIpv6() { |
||
| 1349 | |||
| 1350 | public function testInvalidIpv6() |
||
| 1355 | |||
| 1356 | public function testInvalidInterfaceExists() |
||
| 1361 | |||
| 1362 | public function testValidInterfaceExists() |
||
| 1367 | } |
||
| 1368 | |||
| 1386 |