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 |
||
| 15 | class EuroTest extends \PHPUnit_Framework_TestCase { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @dataProvider unsignedIntegerProvider |
||
| 19 | */ |
||
| 20 | public function testGetCentsReturnsConstructorArgument( int $unsignedInteger ) { |
||
| 24 | |||
| 25 | public function unsignedIntegerProvider() { |
||
| 31 | |||
| 32 | public function testGivenZero_getEuroFloatReturnsZeroFloat() { |
||
| 37 | |||
| 38 | private function assertExactFloat( float $expected, $actual ) { |
||
| 42 | |||
| 43 | public function testGivenOneEuro_getEuroFloatReturnsOne() { |
||
| 47 | |||
| 48 | public function testGivenOneCent_getEuroFloatReturnsPointZeroOne() { |
||
| 52 | |||
| 53 | public function testGiven33cents_getEuroFloatReturnsPointThreeThree() { |
||
| 57 | |||
| 58 | public function testGivenNegativeAmount_constructorThrowsException() { |
||
| 62 | |||
| 63 | public function testGivenZero_getEuroStringReturnsZeroString() { |
||
| 67 | |||
| 68 | public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() { |
||
| 72 | |||
| 73 | public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() { |
||
| 77 | |||
| 78 | public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() { |
||
| 82 | |||
| 83 | public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() { |
||
| 87 | |||
| 88 | public function testGiven1234Cents_getEuroStringReturns12euro34() { |
||
| 92 | |||
| 93 | public function testOneEuroString_getsTurnedInto100cents() { |
||
| 96 | |||
| 97 | public function testOneCentString_getsTurnedInto1cents() { |
||
| 100 | |||
| 101 | public function testTenCentString_getsTurnedInto10cents() { |
||
| 104 | |||
| 105 | public function testShortTenCentString_getsTurnedInto10cents() { |
||
| 108 | |||
| 109 | public function testShortOneEuroString_getsTurnedInto100cents() { |
||
| 112 | |||
| 113 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
||
| 116 | |||
| 117 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
||
| 120 | |||
| 121 | public function testHandlingOfLargeEuroString() { |
||
| 124 | |||
| 125 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
||
| 135 | |||
| 136 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
||
| 140 | |||
| 141 | public function testGivenStringWithComma_exceptionIsThrown() { |
||
| 145 | |||
| 146 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
||
| 150 | |||
| 151 | public function testGivenNonNumber_exceptionIsThrown() { |
||
| 155 | |||
| 156 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
||
| 160 | |||
| 161 | public function testOneEuroFloat_getsTurnedInto100cents() { |
||
| 164 | |||
| 165 | public function testOneCentFloat_getsTurnedInto1cent() { |
||
| 168 | |||
| 169 | public function testTenCentFloat_getsTurnedInto10cents() { |
||
| 172 | |||
| 173 | public function testHandlingOfLargeEuroFloat() { |
||
| 176 | |||
| 177 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
||
| 187 | |||
| 188 | public function testZeroEuroIntegers_isZeroCents() { |
||
| 191 | |||
| 192 | public function testOneEuroIntegers_is100cents() { |
||
| 195 | |||
| 196 | public function test1337EuroIntegers_is133700cents() { |
||
| 199 | |||
| 200 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @dataProvider euroProvider |
||
| 207 | * @param Euro $euro |
||
| 208 | */ |
||
| 209 | public function testEuroEqualsItself( Euro $euro ) { |
||
| 212 | |||
| 213 | public function euroProvider() { |
||
| 222 | |||
| 223 | public function testOneCentDoesNotEqualOneEuro() { |
||
| 226 | |||
| 227 | public function testOneCentDoesNotEqualTwoCents() { |
||
| 230 | |||
| 231 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
||
| 234 | |||
| 235 | public function test9001centsDoesNotEqual9000cents() { |
||
| 238 | |||
| 239 | } |
||
| 240 |