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() |
||
10 | { |
||
11 | return array( |
||
12 | array(1), |
||
13 | array(false), |
||
14 | array("test"), |
||
15 | array(null), |
||
16 | array("1.23"), |
||
17 | array("10"), |
||
18 | ); |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * @dataProvider dataInvalidFloat |
||
23 | */ |
||
24 | public function testInvalidFloat($nonFloat) |
||
25 | { |
||
26 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_FLOAT); |
||
27 | Assertion::float($nonFloat); |
||
28 | } |
||
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() |
||
38 | { |
||
39 | return array( |
||
40 | array(1.23), |
||
41 | array(false), |
||
42 | array("test"), |
||
43 | array(null), |
||
44 | array("1.23"), |
||
45 | array("10"), |
||
46 | array(new \DateTime()), |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @dataProvider dataInvalidInteger |
||
52 | */ |
||
53 | public function testInvalidInteger($nonInteger) |
||
54 | { |
||
55 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGER); |
||
56 | Assertion::integer($nonInteger); |
||
57 | } |
||
58 | |||
59 | public function testValidInteger() |
||
60 | { |
||
61 | Assertion::integer(10); |
||
62 | Assertion::integer(0); |
||
63 | } |
||
64 | |||
65 | public function testValidIntegerish() |
||
66 | { |
||
67 | Assertion::integerish(10); |
||
68 | Assertion::integerish("10"); |
||
69 | } |
||
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) |
||
86 | { |
||
87 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTEGERISH); |
||
88 | Assertion::integerish($nonInteger); |
||
89 | } |
||
90 | |||
91 | public function testValidBoolean() |
||
92 | { |
||
93 | Assertion::boolean(true); |
||
94 | Assertion::boolean(false); |
||
95 | } |
||
96 | |||
97 | public function testInvalidBoolean() |
||
98 | { |
||
99 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BOOLEAN); |
||
100 | Assertion::boolean(1); |
||
101 | } |
||
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() |
||
118 | { |
||
119 | return array( |
||
120 | array(""), |
||
121 | array(false), |
||
122 | array(0), |
||
123 | array(null), |
||
124 | array( array() ), |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @dataProvider dataInvalidNotEmpty |
||
130 | */ |
||
131 | public function testInvalidNotEmpty($value) |
||
132 | { |
||
133 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_EMPTY); |
||
134 | Assertion::notEmpty($value); |
||
135 | } |
||
136 | |||
137 | public function testNotEmpty() |
||
138 | { |
||
139 | Assertion::notEmpty("test"); |
||
140 | Assertion::notEmpty(1); |
||
141 | Assertion::notEmpty(true); |
||
142 | Assertion::notEmpty( array("foo") ); |
||
143 | } |
||
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 function testNotNull() |
||
174 | { |
||
175 | Assertion::notNull("1"); |
||
176 | Assertion::notNull(1); |
||
177 | Assertion::notNull(0); |
||
178 | Assertion::notNull(array()); |
||
179 | Assertion::notNull(false); |
||
180 | } |
||
181 | |||
182 | public function testInvalidNotNull() |
||
183 | { |
||
184 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::VALUE_NULL); |
||
185 | Assertion::notNull(null); |
||
186 | } |
||
187 | |||
188 | public function testString() |
||
189 | { |
||
190 | Assertion::string("test-string"); |
||
191 | Assertion::string(""); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @dataProvider dataInvalidString |
||
196 | */ |
||
197 | public function testInvalidString($invalidString) |
||
198 | { |
||
199 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); |
||
200 | Assertion::string($invalidString); |
||
201 | } |
||
202 | |||
203 | public static function dataInvalidString() |
||
204 | { |
||
205 | return array( |
||
206 | array(1.23), |
||
207 | array(false), |
||
208 | array(new \ArrayObject), |
||
209 | array(null), |
||
210 | array(10), |
||
211 | array(true), |
||
212 | ); |
||
213 | } |
||
214 | |||
215 | public function testInvalidRegex() |
||
216 | { |
||
217 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_REGEX); |
||
218 | Assertion::regex("foo", "(bar)"); |
||
219 | } |
||
220 | |||
221 | public function testInvalidRegexValueNotString() |
||
222 | { |
||
223 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING); |
||
224 | Assertion::regex(array("foo"), "(bar)"); |
||
225 | } |
||
226 | |||
227 | public function testInvalidMinLength() |
||
228 | { |
||
229 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH); |
||
230 | Assertion::minLength("foo", 4); |
||
231 | } |
||
232 | |||
233 | public function testValidMinLength() |
||
234 | { |
||
235 | Assertion::minLength("foo", 3); |
||
236 | Assertion::minLength("foo", 1); |
||
237 | Assertion::minLength("foo", 0); |
||
238 | Assertion::minLength("", 0); |
||
239 | Assertion::minLength("址址", 2); |
||
240 | } |
||
241 | |||
242 | public function testInvalidMaxLength() |
||
243 | { |
||
244 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH); |
||
245 | Assertion::maxLength("foo", 2); |
||
246 | } |
||
247 | |||
248 | public function testValidMaxLength() |
||
249 | { |
||
250 | Assertion::maxLength("foo", 10); |
||
251 | Assertion::maxLength("foo", 3); |
||
252 | Assertion::maxLength("", 0); |
||
253 | Assertion::maxLength("址址", 2); |
||
254 | } |
||
255 | |||
256 | public function testInvalidBetweenLengthMin() |
||
257 | { |
||
258 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MIN_LENGTH); |
||
259 | Assertion::betweenLength("foo", 4, 100); |
||
260 | } |
||
261 | |||
262 | public function testInvalidBetweenLengthMax() |
||
263 | { |
||
264 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_MAX_LENGTH); |
||
265 | Assertion::betweenLength("foo", 0, 2); |
||
266 | } |
||
267 | |||
268 | public function testValidBetweenLength() |
||
269 | { |
||
270 | Assertion::betweenLength("foo", 0, 3); |
||
271 | Assertion::betweenLength("址址", 2, 2); |
||
272 | } |
||
273 | |||
274 | public function testInvalidStartsWith() |
||
275 | { |
||
276 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START); |
||
277 | Assertion::startsWith("foo", "bar"); |
||
278 | } |
||
279 | |||
280 | public function testInvalidStartsWithDueToWrongEncoding() |
||
281 | { |
||
282 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_START); |
||
283 | Assertion::startsWith("址", "址址", null, null, 'ASCII'); |
||
284 | } |
||
285 | |||
286 | public function testValidStartsWith() |
||
287 | { |
||
288 | Assertion::startsWith("foo", "foo"); |
||
289 | Assertion::startsWith("foo", "fo"); |
||
290 | Assertion::startsWith("foo", "f"); |
||
291 | Assertion::startsWith("址foo", "址"); |
||
292 | } |
||
293 | |||
294 | public function testInvalidEndsWith() |
||
295 | { |
||
296 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
||
297 | Assertion::endsWith("foo", "bar"); |
||
298 | } |
||
299 | |||
300 | public function testInvalidEndsWithDueToWrongEncoding() |
||
301 | { |
||
302 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_END); |
||
303 | Assertion::endsWith("址", "址址", null, null, 'ASCII'); |
||
304 | } |
||
305 | |||
306 | public function testValidEndsWith() |
||
307 | { |
||
308 | Assertion::endsWith("foo", "foo"); |
||
309 | Assertion::endsWith("sonderbar", "bar"); |
||
310 | Assertion::endsWith("opp", "p"); |
||
311 | Assertion::endsWith("foo址", "址"); |
||
312 | } |
||
313 | |||
314 | public function testInvalidContains() |
||
315 | { |
||
316 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_STRING_CONTAINS); |
||
317 | Assertion::contains("foo", "bar"); |
||
318 | } |
||
319 | |||
320 | public function testValidContains() |
||
321 | { |
||
322 | Assertion::contains("foo", "foo"); |
||
323 | Assertion::contains("foo", "oo"); |
||
324 | } |
||
325 | |||
326 | public function testInvalidChoice() |
||
327 | { |
||
328 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); |
||
329 | Assertion::choice("foo", array("bar", "baz")); |
||
330 | } |
||
331 | |||
332 | public function testValidChoice() |
||
333 | { |
||
334 | Assertion::choice("foo", array("foo")); |
||
335 | } |
||
336 | |||
337 | public function testInvalidInArray() |
||
338 | { |
||
339 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_CHOICE); |
||
340 | Assertion::inArray("bar", array("baz")); |
||
341 | } |
||
342 | |||
343 | public function testValidInArray() |
||
347 | |||
348 | public function testInvalidNumeric() |
||
349 | { |
||
350 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_NUMERIC); |
||
351 | Assertion::numeric("foo"); |
||
352 | } |
||
353 | |||
354 | public function testValidNumeric() |
||
355 | { |
||
356 | Assertion::numeric("1"); |
||
357 | Assertion::numeric(1); |
||
358 | Assertion::numeric(1.23); |
||
359 | } |
||
360 | |||
361 | public static function dataInvalidArray() |
||
362 | { |
||
363 | return array( |
||
364 | array(null), |
||
365 | array(false), |
||
373 | |||
374 | /** |
||
375 | * @dataProvider dataInvalidArray |
||
376 | */ |
||
377 | public function testInvalidArray($value) |
||
382 | |||
383 | public function testValidArray() |
||
389 | |||
390 | public function testInvalidKeyExists() |
||
391 | { |
||
392 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_EXISTS); |
||
393 | Assertion::keyExists(array("foo" => "bar"), "baz"); |
||
394 | } |
||
395 | |||
396 | public function testValidKeyExists() |
||
397 | { |
||
398 | Assertion::keyExists(array("foo" => "bar"), "foo"); |
||
399 | } |
||
400 | |||
401 | public function testInvalidKeyNotExists() |
||
402 | { |
||
403 | $this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_KEY_NOT_EXISTS); |
||
404 | Assertion::keyNotExists(array("foo" => "bar"), "foo"); |
||
405 | } |
||
406 | |||
407 | public function testValidKeyNotExists() |
||
408 | { |
||
409 | Assertion::keyNotExists(array("foo" => "bar"), "baz"); |
||
410 | } |
||
411 | |||
412 | public static function dataInvalidNotBlank() |
||
413 | { |
||
414 | return array( |
||
415 | array(""), |
||
416 | array(" "), |
||
417 | array("\t"), |
||
418 | array("\n"), |
||
419 | array("\r"), |
||
420 | array(false), |
||
421 | array(null), |
||
425 | |||
426 | /** |
||
427 | * @dataProvider dataInvalidNotBlank |
||
428 | */ |
||
429 | public function testInvalidNotBlank($notBlank) |
||
434 | |||
435 | public function testValidNotBlank() |
||
439 | |||
440 | public function testInvalidNotInstanceOf() |
||
445 | |||
446 | public function testValidNotIsInstanceOf() |
||
450 | |||
451 | public function testInvalidInstanceOf() |
||
456 | |||
457 | public function testValidInstanceOf() |
||
461 | |||
462 | public function testInvalidSubclassOf() |
||
467 | |||
468 | public function testValidSubclassOf() |
||
472 | |||
473 | public function testInvalidRange() |
||
479 | |||
480 | public function testValidRange() |
||
487 | |||
488 | public function testInvalidEmail() |
||
493 | |||
494 | public function testValidEmail() |
||
498 | |||
499 | /** |
||
500 | * @dataProvider dataInvalidUrl |
||
501 | */ |
||
502 | public function testInvalidUrl($url) |
||
508 | |||
509 | public static function dataInvalidUrl() |
||
520 | |||
521 | /** |
||
522 | * @dataProvider dataValidUrl |
||
523 | */ |
||
524 | public function testValidUrl($url) |
||
528 | |||
529 | public static function dataValidUrl() |
||
540 | |||
541 | public function testInvalidDigit() |
||
546 | |||
547 | public function testValidDigit() |
||
553 | |||
554 | public function testValidAlnum() |
||
561 | |||
562 | public function testInvalidAlnum() |
||
567 | |||
568 | public function testValidTrue() |
||
572 | |||
573 | public function testInvalidTrue() |
||
578 | |||
579 | public function testValidFalse() |
||
583 | |||
584 | public function testInvalidFalse() |
||
589 | |||
590 | public function testInvalidClass() |
||
595 | |||
596 | public function testValidClass() |
||
600 | |||
601 | public function testSame() |
||
609 | |||
610 | public function testEq() |
||
618 | |||
619 | public function testNotEq() |
||
626 | |||
627 | public function testNotSame() |
||
634 | |||
635 | public function testMin() |
||
644 | |||
645 | public function testMax() |
||
654 | |||
655 | public function testNullOr() |
||
660 | |||
661 | public function testNullOrWithNoValueThrows() |
||
666 | |||
667 | public function testLength() |
||
672 | |||
673 | public static function dataLengthUtf8Characters() |
||
680 | |||
681 | /** |
||
682 | * @dataProvider dataLengthUtf8Characters |
||
683 | */ |
||
684 | public function testLenghtUtf8Characters($value, $expected) |
||
688 | |||
689 | public function testLengthFailed() |
||
694 | |||
695 | public function testLengthFailedForWrongEncoding() |
||
700 | |||
701 | public function testLengthValidForGivenEncoding() |
||
705 | |||
706 | public function testFile() |
||
710 | |||
711 | public function testFileWithEmptyFilename() |
||
716 | |||
717 | public function testFileDoesNotExists() |
||
722 | |||
723 | public function testDirectory() |
||
730 | |||
731 | public function testReadable() |
||
738 | |||
739 | public function testWriteable() |
||
746 | |||
747 | /** |
||
748 | * @expectedException \BadMethodCallException |
||
749 | * @expectedExceptionMessage No assertion |
||
750 | */ |
||
751 | public function testFailedNullOrMethodCall() |
||
755 | |||
756 | public function testImplementsInterface() |
||
769 | |||
770 | public function testImplementsInterfaceWithClassObject() |
||
785 | |||
786 | /** |
||
787 | * @dataProvider isJsonStringDataprovider |
||
788 | */ |
||
789 | public function testIsJsonString($content) |
||
793 | |||
794 | public static function isJsonStringDataprovider() |
||
803 | |||
804 | /** |
||
805 | * @dataProvider isJsonStringInvalidStringDataprovider |
||
806 | */ |
||
807 | public function testIsJsonStringExpectingException($invalidString) |
||
812 | |||
813 | public static function isJsonStringInvalidStringDataprovider() |
||
820 | |||
821 | /** |
||
822 | * @dataProvider providesValidUuids |
||
823 | */ |
||
824 | public function testValidUuids($uuid) |
||
828 | |||
829 | /** |
||
830 | * @dataProvider providesInvalidUuids |
||
831 | */ |
||
832 | public function testInvalidUuids($uuid) |
||
837 | |||
838 | static public function providesValidUuids() |
||
849 | |||
850 | static public function providesInvalidUuids() |
||
860 | |||
861 | public function testValidNotEmptyKey() |
||
865 | |||
866 | /** |
||
867 | * @dataProvider invalidNotEmptyKeyDataprovider |
||
868 | */ |
||
869 | public function testInvalidNotEmptyKey($invalidArray, $key) |
||
874 | |||
875 | public static function invalidNotEmptyKeyDataprovider() |
||
882 | |||
883 | public function testAllWithSimpleAssertion() |
||
887 | |||
888 | public function testAllWithSimpleAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
893 | |||
894 | public function testAllWithComplexAssertion() |
||
898 | |||
899 | public function testAllWithComplexAssertionThrowsExceptionOnElementThatFailsAssertion() |
||
905 | |||
906 | public function testAllWithNoValueThrows() |
||
911 | |||
912 | public function testValidCount() |
||
917 | |||
918 | public static function dataInvalidCount() |
||
925 | |||
926 | /** |
||
927 | * @dataProvider dataInvalidCount |
||
928 | */ |
||
929 | public function testInvalidCount($countable, $count) |
||
934 | |||
935 | public function testChoicesNotEmpty() |
||
942 | |||
943 | /** |
||
944 | * @dataProvider invalidChoicesProvider |
||
945 | */ |
||
946 | public function testChoicesNotEmptyExpectingException($values, $choices, $exceptionCode) |
||
954 | |||
955 | public function invalidChoicesProvider() |
||
963 | |||
964 | public function testIsObject() |
||
968 | |||
969 | public function testIsObjectExpectingException() |
||
974 | |||
975 | public function testMethodExists() |
||
979 | |||
980 | /** |
||
981 | * @test |
||
982 | */ |
||
983 | public function it_passes_values_and_constraints_to_exception() |
||
994 | |||
995 | public function testLessThan() |
||
1002 | |||
1003 | public function invalidLessProvider() |
||
1014 | |||
1015 | /** |
||
1016 | * @dataProvider invalidLessProvider |
||
1017 | */ |
||
1018 | public function testLessThanThrowsException($value, $limit) |
||
1023 | |||
1024 | public function testLessOrEqualThan() |
||
1034 | |||
1035 | public function invalidLessOrEqualProvider() |
||
1043 | |||
1044 | /** |
||
1045 | * @dataProvider invalidLessOrEqualProvider |
||
1046 | */ |
||
1047 | public function testLessOrEqualThanThrowsException($value, $limit) |
||
1052 | |||
1053 | public function testGreaterThan() |
||
1060 | |||
1061 | public function invalidGreaterProvider() |
||
1072 | |||
1073 | /** |
||
1074 | * @dataProvider validDateProvider |
||
1075 | */ |
||
1076 | public function testValidDate($value, $format) |
||
1080 | |||
1081 | public function validDateProvider() |
||
1090 | |||
1091 | /** |
||
1092 | * @dataProvider invalidGreaterProvider |
||
1093 | */ |
||
1094 | public function testGreaterThanThrowsException($value, $limit) |
||
1099 | |||
1100 | public function testGreaterOrEqualThan() |
||
1110 | |||
1111 | public function invalidGreaterOrEqualProvider() |
||
1119 | |||
1120 | /** |
||
1121 | * @dataProvider invalidGreaterOrEqualProvider |
||
1122 | * |
||
1123 | * @param mixed $value |
||
1124 | * @param mixed $limit |
||
1125 | */ |
||
1126 | public function testGreaterOrEqualThanThrowsException($value, $limit) |
||
1131 | |||
1132 | /** |
||
1133 | * @dataProvider invalidDateProvider |
||
1134 | */ |
||
1135 | public function testInvalidDate($value, $format) |
||
1140 | |||
1141 | public function invalidDateProvider() |
||
1149 | } |
||
1150 | |||
1163 |