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 testGiven9876Cents_stringCastingReturns98euro76() { |
||
| 95 | $amount = Euro::newFromCents( 9876 ); |
||
| 96 | $this->assertSame( '98.76', (string) $amount ); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function testGivenEuroAmount_jsonEncodeWillEncodeProperly() { |
||
| 103 | |||
| 104 | public function testOneEuroString_getsTurnedInto100cents() { |
||
| 107 | |||
| 108 | public function testOneCentString_getsTurnedInto1cents() { |
||
| 111 | |||
| 112 | public function testTenCentString_getsTurnedInto10cents() { |
||
| 115 | |||
| 116 | public function testShortTenCentString_getsTurnedInto10cents() { |
||
| 119 | |||
| 120 | public function testShortOneEuroString_getsTurnedInto100cents() { |
||
| 123 | |||
| 124 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
||
| 127 | |||
| 128 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
||
| 131 | |||
| 132 | public function testHandlingOfLargeEuroString() { |
||
| 135 | |||
| 136 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
||
| 141 | |||
| 142 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
||
| 158 | |||
| 159 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
||
| 163 | |||
| 164 | public function testGivenStringWithComma_exceptionIsThrown() { |
||
| 168 | |||
| 169 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
||
| 173 | |||
| 174 | public function testGivenNonNumber_exceptionIsThrown() { |
||
| 178 | |||
| 179 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
||
| 183 | |||
| 184 | public function testOneEuroFloat_getsTurnedInto100cents() { |
||
| 187 | |||
| 188 | public function testOneCentFloat_getsTurnedInto1cent() { |
||
| 191 | |||
| 192 | public function testTenCentFloat_getsTurnedInto10cents() { |
||
| 195 | |||
| 196 | public function testHandlingOfLargeEuroFloat() { |
||
| 199 | |||
| 200 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
||
| 211 | |||
| 212 | public function testZeroEuroIntegers_isZeroCents() { |
||
| 215 | |||
| 216 | public function testOneEuroIntegers_is100cents() { |
||
| 219 | |||
| 220 | public function test1337EuroIntegers_is133700cents() { |
||
| 223 | |||
| 224 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @dataProvider euroProvider |
||
| 231 | * @param Euro $euro |
||
| 232 | */ |
||
| 233 | public function testEuroEqualsItself( Euro $euro ) { |
||
| 236 | |||
| 237 | public function euroProvider() { |
||
| 246 | |||
| 247 | public function testOneCentDoesNotEqualOneEuro() { |
||
| 250 | |||
| 251 | public function testOneCentDoesNotEqualTwoCents() { |
||
| 254 | |||
| 255 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
||
| 258 | |||
| 259 | public function test9001centsDoesNotEqual9000cents() { |
||
| 262 | |||
| 263 | } |
||
| 264 |