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 testOneEuroString_getsTurnedInto100cents() { |
||
98 | |||
99 | public function testOneCentString_getsTurnedInto1cents() { |
||
102 | |||
103 | public function testTenCentString_getsTurnedInto10cents() { |
||
106 | |||
107 | public function testShortTenCentString_getsTurnedInto10cents() { |
||
110 | |||
111 | public function testShortOneEuroString_getsTurnedInto100cents() { |
||
114 | |||
115 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
||
118 | |||
119 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
||
122 | |||
123 | public function testHandlingOfLargeEuroString() { |
||
126 | |||
127 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
||
132 | |||
133 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
||
149 | |||
150 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
||
154 | |||
155 | public function testGivenStringWithComma_exceptionIsThrown() { |
||
159 | |||
160 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
||
164 | |||
165 | public function testGivenNonNumber_exceptionIsThrown() { |
||
169 | |||
170 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
||
174 | |||
175 | public function testOneEuroFloat_getsTurnedInto100cents() { |
||
178 | |||
179 | public function testOneCentFloat_getsTurnedInto1cent() { |
||
182 | |||
183 | public function testTenCentFloat_getsTurnedInto10cents() { |
||
186 | |||
187 | public function testHandlingOfLargeEuroFloat() { |
||
190 | |||
191 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
||
202 | |||
203 | public function testZeroEuroIntegers_isZeroCents() { |
||
206 | |||
207 | public function testOneEuroIntegers_is100cents() { |
||
210 | |||
211 | public function test1337EuroIntegers_is133700cents() { |
||
214 | |||
215 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
||
219 | |||
220 | /** |
||
221 | * @dataProvider euroProvider |
||
222 | * @param Euro $euro |
||
223 | */ |
||
224 | public function testEuroEqualsItself( Euro $euro ) { |
||
227 | |||
228 | public function euroProvider() { |
||
237 | |||
238 | public function testOneCentDoesNotEqualOneEuro() { |
||
241 | |||
242 | public function testOneCentDoesNotEqualTwoCents() { |
||
245 | |||
246 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
||
249 | |||
250 | public function test9001centsDoesNotEqual9000cents() { |
||
253 | |||
254 | /** |
||
255 | * @dataProvider tooLongStringProvider |
||
256 | */ |
||
257 | public function testNewFromStringThrowsExceptionWhenStringIsTooLong( string $string ) { |
||
263 | |||
264 | public function tooLongStringProvider() { |
||
269 | |||
270 | public function testNewFromStringHandlesLongStrings() { |
||
274 | |||
275 | /** |
||
276 | * @dataProvider tooHighNumberProvider |
||
277 | */ |
||
278 | public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh( int $int ) { |
||
283 | |||
284 | public function tooHighNumberProvider() { |
||
289 | |||
290 | /** |
||
291 | * @dataProvider tooHighNumberProvider |
||
292 | */ |
||
293 | public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh( int $int ) { |
||
298 | |||
299 | public function testNewFromIntHandlesBigIntegers() { |
||
309 | |||
310 | } |
||
311 |