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 |
||
16 | class EuroTest extends TestCase { |
||
17 | |||
18 | /** |
||
19 | * @dataProvider unsignedIntegerProvider |
||
20 | */ |
||
21 | public function testGetCentsReturnsConstructorArgument( int $unsignedInteger ) { |
||
25 | |||
26 | public function unsignedIntegerProvider() { |
||
32 | |||
33 | public function testGivenZero_getEuroFloatReturnsZeroFloat() { |
||
38 | |||
39 | private function assertExactFloat( float $expected, $actual ) { |
||
43 | |||
44 | public function testGivenOneEuro_getEuroFloatReturnsOne() { |
||
48 | |||
49 | public function testGivenOneCent_getEuroFloatReturnsPointZeroOne() { |
||
53 | |||
54 | public function testGiven33cents_getEuroFloatReturnsPointThreeThree() { |
||
58 | |||
59 | public function testGivenNegativeAmount_constructorThrowsException() { |
||
63 | |||
64 | public function testGivenZero_getEuroStringReturnsZeroString() { |
||
68 | |||
69 | public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() { |
||
73 | |||
74 | public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() { |
||
78 | |||
79 | public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() { |
||
83 | |||
84 | public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() { |
||
88 | |||
89 | public function testGiven1234Cents_getEuroStringReturns12euro34() { |
||
93 | |||
94 | public function testOneEuroString_getsTurnedInto100cents() { |
||
97 | |||
98 | public function testOneCentString_getsTurnedInto1cents() { |
||
101 | |||
102 | public function testTenCentString_getsTurnedInto10cents() { |
||
105 | |||
106 | public function testShortTenCentString_getsTurnedInto10cents() { |
||
109 | |||
110 | public function testShortOneEuroString_getsTurnedInto100cents() { |
||
113 | |||
114 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
||
117 | |||
118 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
||
121 | |||
122 | public function testHandlingOfLargeEuroString() { |
||
125 | |||
126 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
||
131 | |||
132 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
||
148 | |||
149 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
||
153 | |||
154 | public function testGivenStringWithComma_exceptionIsThrown() { |
||
158 | |||
159 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
||
163 | |||
164 | public function testGivenNonNumber_exceptionIsThrown() { |
||
168 | |||
169 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
||
173 | |||
174 | public function testOneEuroFloat_getsTurnedInto100cents() { |
||
177 | |||
178 | public function testOneCentFloat_getsTurnedInto1cent() { |
||
181 | |||
182 | public function testTenCentFloat_getsTurnedInto10cents() { |
||
185 | |||
186 | public function testHandlingOfLargeEuroFloat() { |
||
189 | |||
190 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
||
201 | |||
202 | public function testZeroEuroIntegers_isZeroCents() { |
||
205 | |||
206 | public function testOneEuroIntegers_is100cents() { |
||
209 | |||
210 | public function test1337EuroIntegers_is133700cents() { |
||
213 | |||
214 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
||
218 | |||
219 | /** |
||
220 | * @dataProvider euroProvider |
||
221 | * @param Euro $euro |
||
222 | */ |
||
223 | public function testEuroEqualsItself( Euro $euro ) { |
||
226 | |||
227 | public function euroProvider() { |
||
236 | |||
237 | public function testOneCentDoesNotEqualOneEuro() { |
||
240 | |||
241 | public function testOneCentDoesNotEqualTwoCents() { |
||
244 | |||
245 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
||
248 | |||
249 | public function test9001centsDoesNotEqual9000cents() { |
||
252 | |||
253 | } |
||
254 |