Complex classes like EuroTest 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 EuroTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class EuroTest extends TestCase { |
||
18 | |||
19 | /** |
||
20 | * @dataProvider unsignedIntegerProvider |
||
21 | */ |
||
22 | public function testGetCentsReturnsConstructorArgument( int $unsignedInteger ) { |
||
26 | |||
27 | public function unsignedIntegerProvider() { |
||
33 | |||
34 | public function testGivenZero_getEuroFloatReturnsZeroFloat() { |
||
39 | |||
40 | private function assertExactFloat( float $expected, $actual ) { |
||
44 | |||
45 | public function testGivenOneEuro_getEuroFloatReturnsOne() { |
||
49 | |||
50 | public function testGivenOneCent_getEuroFloatReturnsPointZeroOne() { |
||
54 | |||
55 | public function testGiven33cents_getEuroFloatReturnsPointThreeThree() { |
||
59 | |||
60 | public function testGivenNegativeAmount_constructorThrowsException() { |
||
64 | |||
65 | public function testGivenZero_getEuroStringReturnsZeroString() { |
||
69 | |||
70 | public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() { |
||
74 | |||
75 | public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() { |
||
79 | |||
80 | public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() { |
||
84 | |||
85 | public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() { |
||
89 | |||
90 | public function testGiven1234Cents_getEuroStringReturns12euro34() { |
||
94 | |||
95 | public function testGiven9876Cents_stringCastingReturns98euro76() { |
||
99 | |||
100 | public function testGivenEuroAmount_jsonEncodeWillEncodeProperly() { |
||
104 | |||
105 | public function testOneEuroString_getsTurnedInto100cents() { |
||
108 | |||
109 | public function testOneCentString_getsTurnedInto1cents() { |
||
112 | |||
113 | public function testTenCentString_getsTurnedInto10cents() { |
||
116 | |||
117 | public function testShortTenCentString_getsTurnedInto10cents() { |
||
120 | |||
121 | public function testShortOneEuroString_getsTurnedInto100cents() { |
||
124 | |||
125 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
||
128 | |||
129 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
||
132 | |||
133 | public function testHandlingOfLargeEuroString() { |
||
136 | |||
137 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
||
142 | |||
143 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
||
159 | |||
160 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
||
164 | |||
165 | public function testGivenStringWithComma_exceptionIsThrown() { |
||
169 | |||
170 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
||
174 | |||
175 | public function testGivenNonNumber_exceptionIsThrown() { |
||
179 | |||
180 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
||
184 | |||
185 | public function testOneEuroFloat_getsTurnedInto100cents() { |
||
188 | |||
189 | public function testOneCentFloat_getsTurnedInto1cent() { |
||
192 | |||
193 | public function testTenCentFloat_getsTurnedInto10cents() { |
||
196 | |||
197 | public function testHandlingOfLargeEuroFloat() { |
||
200 | |||
201 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
||
212 | |||
213 | public function testZeroEuroIntegers_isZeroCents() { |
||
216 | |||
217 | public function testOneEuroIntegers_is100cents() { |
||
220 | |||
221 | public function test1337EuroIntegers_is133700cents() { |
||
224 | |||
225 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
||
229 | |||
230 | /** |
||
231 | * @dataProvider euroProvider |
||
232 | * @param Euro $euro |
||
233 | */ |
||
234 | public function testEuroEqualsItself( Euro $euro ) { |
||
237 | |||
238 | public function euroProvider() { |
||
247 | |||
248 | public function testOneCentDoesNotEqualOneEuro() { |
||
251 | |||
252 | public function testOneCentDoesNotEqualTwoCents() { |
||
255 | |||
256 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
||
259 | |||
260 | public function test9001centsDoesNotEqual9000cents() { |
||
263 | |||
264 | /** |
||
265 | * @dataProvider tooLongStringProvider |
||
266 | */ |
||
267 | public function testNewFromStringThrowsExceptionWhenStringIsTooLong( string $string ) { |
||
273 | |||
274 | public function tooLongStringProvider() { |
||
279 | |||
280 | public function testNewFromStringHandlesLongStrings() { |
||
284 | |||
285 | /** |
||
286 | * @dataProvider tooHighNumberProvider |
||
287 | */ |
||
288 | public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh( int $int ) { |
||
293 | |||
294 | public function tooHighNumberProvider() { |
||
299 | |||
300 | /** |
||
301 | * @dataProvider tooHighNumberProvider |
||
302 | */ |
||
303 | public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh( int $int ) { |
||
308 | |||
309 | public function testNewFromIntHandlesBigIntegers() { |
||
319 | |||
320 | } |
||
321 |