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() |
||
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 { |
||
677 | Assertion::min(0, 1); |
||
678 | $this->fail('Expected exception `Assert\AssertionFailedException` not thrown'); |
||
679 | } catch (AssertionFailedException $e){ |
||
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 | public function testValidNotEmptyKey() |
||
926 | |||
927 | /** |
||
928 | * @dataProvider invalidNotEmptyKeyDataprovider |
||
929 | */ |
||
930 | public function testInvalidNotEmptyKey($invalidArray, $key) |
||
935 | |||
936 | public static function invalidNotEmptyKeyDataprovider() |
||
943 | |||
944 | public function testAllWithSimpleAssertion() |
||
948 | |||
949 | public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
954 | |||
955 | public function testAllWithComplexAssertion() |
||
959 | |||
960 | public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
966 | |||
967 | public function testAllWithNoValueThrows() |
||
972 | |||
973 | public function testValidCount() |
||
978 | |||
979 | public static function dataInvalidCount() |
||
986 | |||
987 | /** |
||
988 | * @dataProvider dataInvalidCount |
||
989 | */ |
||
990 | public function testInvalidCount($countable, $count) |
||
995 | |||
996 | public function testChoicesNotEmpty() |
||
1003 | |||
1004 | /** |
||
1005 | * @dataProvider invalidChoicesProvider |
||
1006 | */ |
||
1007 | public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode) |
||
1015 | |||
1016 | public function invalidChoicesProvider() |
||
1024 | |||
1025 | public function testIsObject() |
||
1029 | |||
1030 | public function testIsObjectExpectingException() |
||
1035 | |||
1036 | public function testMethodExists() |
||
1040 | |||
1041 | public function testMethodExistsFailure() |
||
1046 | |||
1047 | /** |
||
1048 | * @test |
||
1049 | */ |
||
1050 | public function it_passes_values_and_constraints_to_exception() |
||
1061 | |||
1062 | public function testLessThan() |
||
1069 | |||
1070 | public function invalidLessProvider() |
||
1081 | |||
1082 | /** |
||
1083 | * @dataProvider invalidLessProvider |
||
1084 | */ |
||
1085 | public function testLessThanThrowsException($value, $limit) |
||
1090 | |||
1091 | public function testLessOrEqualThan() |
||
1101 | |||
1102 | public function invalidLessOrEqualProvider() |
||
1110 | |||
1111 | /** |
||
1112 | * @dataProvider invalidLessOrEqualProvider |
||
1113 | */ |
||
1114 | public function testLessOrEqualThanThrowsException($value, $limit) |
||
1119 | |||
1120 | public function testGreaterThan() |
||
1127 | |||
1128 | public function invalidGreaterProvider() |
||
1139 | |||
1140 | /** |
||
1141 | * @dataProvider validDateProvider |
||
1142 | */ |
||
1143 | public function testValidDate($value, $format) |
||
1147 | |||
1148 | public function validDateProvider() |
||
1157 | |||
1158 | /** |
||
1159 | * @dataProvider invalidGreaterProvider |
||
1160 | */ |
||
1161 | public function testGreaterThanThrowsException($value, $limit) |
||
1166 | |||
1167 | public function testGreaterOrEqualThan() |
||
1177 | |||
1178 | public function invalidGreaterOrEqualProvider() |
||
1186 | |||
1187 | /** |
||
1188 | * @dataProvider invalidGreaterOrEqualProvider |
||
1189 | * |
||
1190 | * @param mixed $value |
||
1191 | * @param mixed $limit |
||
1192 | */ |
||
1193 | public function testGreaterOrEqualThanThrowsException($value, $limit) |
||
1198 | |||
1199 | /** |
||
1200 | * @dataProvider invalidDateProvider |
||
1201 | */ |
||
1202 | public function testInvalidDate($value, $format) |
||
1207 | |||
1208 | public function invalidDateProvider() |
||
1216 | |||
1217 | public function testInvalidTraversable() |
||
1222 | |||
1223 | public function testInvalidArrayAccessible() |
||
1228 | |||
1229 | public function testInvalidCallable() |
||
1234 | |||
1235 | public function testValidCallable() |
||
1243 | |||
1244 | public function testInvalidSatisfy() |
||
1251 | |||
1252 | public function testValidSatisfy() |
||
1266 | |||
1267 | public function testInvalidInterfaceExists() |
||
1272 | |||
1273 | public function testValidInterfaceExists() |
||
1278 | } |
||
1279 | |||
1297 |