| Total Complexity | 53 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 0 |
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.
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 ) { |
||
| 23 | $amount = Euro::newFromCents( $unsignedInteger ); |
||
| 24 | $this->assertSame( $unsignedInteger, $amount->getEuroCents() ); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function unsignedIntegerProvider() { |
||
| 28 | return [ |
||
| 29 | [ 0 ], [ 1 ], [ 2 ], [ 9 ], [ 10 ], [ 11 ], |
||
| 30 | [ 99 ], [ 100 ], [ 101 ], [ 999 ], [ 1000 ], [ 1001 ], |
||
| 31 | ]; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function testGivenZero_getEuroFloatReturnsZeroFloat() { |
||
| 35 | $amount = Euro::newFromCents( 0 ); |
||
| 36 | $this->assertExactFloat( 0.0, $amount->getEuroFloat() ); |
||
| 37 | $this->assertNotSame( 0, $amount->getEuroFloat() ); |
||
| 38 | } |
||
| 39 | |||
| 40 | private function assertExactFloat( $expected, $actual ) { |
||
| 41 | $this->assertIsFloat( $actual ); |
||
| 42 | $this->assertEquals( $expected, $actual, '', 0 ); |
||
|
|
|||
| 43 | } |
||
| 44 | |||
| 45 | public function testGivenOneEuro_getEuroFloatReturnsOne() { |
||
| 46 | $amount = Euro::newFromCents( 100 ); |
||
| 47 | $this->assertExactFloat( 1.0, $amount->getEuroFloat() ); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testGivenOneCent_getEuroFloatReturnsPointZeroOne() { |
||
| 51 | $amount = Euro::newFromCents( 1 ); |
||
| 52 | $this->assertExactFloat( 0.01, $amount->getEuroFloat() ); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testGiven33cents_getEuroFloatReturnsPointThreeThree() { |
||
| 56 | $amount = Euro::newFromCents( 33 ); |
||
| 57 | $this->assertExactFloat( 0.33, $amount->getEuroFloat() ); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testGivenNegativeAmount_constructorThrowsException() { |
||
| 63 | } |
||
| 64 | |||
| 65 | public function testGivenZero_getEuroStringReturnsZeroString() { |
||
| 66 | $amount = Euro::newFromCents( 0 ); |
||
| 67 | $this->assertSame( '0.00', $amount->getEuroString() ); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() { |
||
| 71 | $amount = Euro::newFromCents( 100 ); |
||
| 72 | $this->assertSame( '1.00', $amount->getEuroString() ); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() { |
||
| 76 | $amount = Euro::newFromCents( 200 ); |
||
| 77 | $this->assertSame( '2.00', $amount->getEuroString() ); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() { |
||
| 81 | $amount = Euro::newFromCents( 1 ); |
||
| 82 | $this->assertSame( '0.01', $amount->getEuroString() ); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() { |
||
| 86 | $amount = Euro::newFromCents( 10 ); |
||
| 87 | $this->assertSame( '0.10', $amount->getEuroString() ); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testGiven1234Cents_getEuroStringReturns12euro34() { |
||
| 91 | $amount = Euro::newFromCents( 1234 ); |
||
| 92 | $this->assertSame( '12.34', $amount->getEuroString() ); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testGiven9876Cents_stringCastingReturns98euro76() { |
||
| 96 | $amount = Euro::newFromCents( 9876 ); |
||
| 97 | $this->assertSame( '98.76', (string)$amount ); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testGivenEuroAmount_jsonEncodeWillEncodeProperly() { |
||
| 101 | $amount = Euro::newFromCents( 9876 ); |
||
| 102 | $this->assertSame( '"98.76"', json_encode( $amount ) ); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testOneEuroString_getsTurnedInto100cents() { |
||
| 106 | $this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() ); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testOneCentString_getsTurnedInto1cents() { |
||
| 110 | $this->assertSame( 1, Euro::newFromString( '0.01' )->getEuroCents() ); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testTenCentString_getsTurnedInto10cents() { |
||
| 114 | $this->assertSame( 10, Euro::newFromString( '0.10' )->getEuroCents() ); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function testShortTenCentString_getsTurnedInto10cents() { |
||
| 118 | $this->assertSame( 10, Euro::newFromString( '0.1' )->getEuroCents() ); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function testShortOneEuroString_getsTurnedInto100cents() { |
||
| 122 | $this->assertSame( 100, Euro::newFromString( '1' )->getEuroCents() ); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
||
| 126 | $this->assertSame( 100, Euro::newFromString( '1.0' )->getEuroCents() ); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
||
| 130 | $this->assertSame( 100, Euro::newFromString( '1.00000' )->getEuroCents() ); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function testHandlingOfLargeEuroString() { |
||
| 134 | $this->assertSame( 3133742, Euro::newFromString( '31337.42' )->getEuroCents() ); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
||
| 138 | // Regression test for https://phabricator.wikimedia.org/T183481 |
||
| 139 | $this->assertSame( 870, Euro::newFromString( '8.70' )->getEuroCents() ); |
||
| 140 | $this->assertSame( 920, Euro::newFromString( '9.20' )->getEuroCents() ); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
||
| 144 | $this->assertSame( 101, Euro::newFromString( '1.0100000001' )->getEuroCents() ); |
||
| 145 | $this->assertSame( 101, Euro::newFromString( '1.010000009999' )->getEuroCents() ); |
||
| 146 | $this->assertSame( 101, Euro::newFromString( '1.011' )->getEuroCents() ); |
||
| 147 | $this->assertSame( 101, Euro::newFromString( '1.014' )->getEuroCents() ); |
||
| 148 | $this->assertSame( 101, Euro::newFromString( '1.0149' )->getEuroCents() ); |
||
| 149 | $this->assertSame( 102, Euro::newFromString( '1.015' )->getEuroCents() ); |
||
| 150 | $this->assertSame( 102, Euro::newFromString( '1.019' )->getEuroCents() ); |
||
| 151 | $this->assertSame( 102, Euro::newFromString( '1.0199999' )->getEuroCents() ); |
||
| 152 | $this->assertSame( 870, Euro::newFromString( '8.701' )->getEuroCents() ); |
||
| 153 | $this->assertSame( 870, Euro::newFromString( '8.70499' )->getEuroCents() ); |
||
| 154 | $this->assertSame( 871, Euro::newFromString( '8.705' )->getEuroCents() ); |
||
| 155 | $this->assertSame( 871, Euro::newFromString( '8.705000' )->getEuroCents() ); |
||
| 156 | $this->assertSame( 871, Euro::newFromString( '8.705001' )->getEuroCents() ); |
||
| 157 | $this->assertSame( 871, Euro::newFromString( '8.709999' )->getEuroCents() ); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
||
| 161 | $this->expectException( \InvalidArgumentException::class ); |
||
| 162 | Euro::newFromString( '-1.00' ); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function testGivenStringWithComma_exceptionIsThrown() { |
||
| 166 | $this->expectException( \InvalidArgumentException::class ); |
||
| 167 | Euro::newFromString( '1,00' ); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
||
| 171 | $this->expectException( \InvalidArgumentException::class ); |
||
| 172 | Euro::newFromString( '1.0.0' ); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function testGivenNonNumber_exceptionIsThrown() { |
||
| 176 | $this->expectException( \InvalidArgumentException::class ); |
||
| 177 | Euro::newFromString( '1.00abc' ); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
||
| 181 | $this->expectException( \InvalidArgumentException::class ); |
||
| 182 | Euro::newFromFloat( -1.00 ); |
||
| 183 | } |
||
| 184 | |||
| 185 | public function testOneEuroFloat_getsTurnedInto100cents() { |
||
| 186 | $this->assertSame( 100, Euro::newFromFloat( 1.0 )->getEuroCents() ); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testOneCentFloat_getsTurnedInto1cent() { |
||
| 190 | $this->assertSame( 1, Euro::newFromFloat( 0.01 )->getEuroCents() ); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function testTenCentFloat_getsTurnedInto10cents() { |
||
| 195 | } |
||
| 196 | |||
| 197 | public function testHandlingOfLargeEuroFloat() { |
||
| 198 | $this->assertSame( 3133742, Euro::newFromFloat( 31337.42 )->getEuroCents() ); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
||
| 202 | $this->assertSame( 101, Euro::newFromFloat( 1.0100000001 )->getEuroCents() ); |
||
| 203 | $this->assertSame( 101, Euro::newFromFloat( 1.010000009999 )->getEuroCents() ); |
||
| 204 | $this->assertSame( 101, Euro::newFromFloat( 1.011 )->getEuroCents() ); |
||
| 205 | $this->assertSame( 101, Euro::newFromFloat( 1.014 )->getEuroCents() ); |
||
| 206 | $this->assertSame( 101, Euro::newFromFloat( 1.0149 )->getEuroCents() ); |
||
| 207 | $this->assertSame( 102, Euro::newFromFloat( 1.015 )->getEuroCents() ); |
||
| 208 | $this->assertSame( 102, Euro::newFromFloat( 1.019 )->getEuroCents() ); |
||
| 209 | $this->assertSame( 102, Euro::newFromFloat( 1.0199999 )->getEuroCents() ); |
||
| 210 | $this->assertSame( 870, Euro::newFromFloat( 8.70 )->getEuroCents() ); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function testZeroEuroIntegers_isZeroCents() { |
||
| 215 | } |
||
| 216 | |||
| 217 | public function testOneEuroIntegers_is100Cents() { |
||
| 219 | } |
||
| 220 | |||
| 221 | // phpcs:ignore MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName |
||
| 222 | public function test1337EuroIntegers_is133700Cents() { |
||
| 223 | $this->assertSame( 133700, Euro::newFromInt( 1337 )->getEuroCents() ); |
||
| 224 | } |
||
| 225 | |||
| 226 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
||
| 227 | $this->expectException( \InvalidArgumentException::class ); |
||
| 228 | Euro::newFromInt( -1 ); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @dataProvider euroProvider |
||
| 233 | * @param Euro $euro |
||
| 234 | */ |
||
| 235 | public function testEuroEqualsItself( Euro $euro ) { |
||
| 237 | } |
||
| 238 | |||
| 239 | public function euroProvider() { |
||
| 246 | ]; |
||
| 247 | } |
||
| 248 | |||
| 249 | public function testOneCentDoesNotEqualOneEuro() { |
||
| 250 | $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromInt( 1 ) ) ); |
||
| 251 | } |
||
| 252 | |||
| 253 | public function testOneCentDoesNotEqualTwoCents() { |
||
| 254 | $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 2 ) ) ); |
||
| 255 | } |
||
| 256 | |||
| 257 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
||
| 258 | $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 101 ) ) ); |
||
| 259 | } |
||
| 260 | |||
| 261 | public function test9001centsDoesNotEqual9000cents() { |
||
| 262 | $this->assertFalse( Euro::newFromCents( 9001 )->equals( Euro::newFromCents( 9000 ) ) ); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @dataProvider tooLongStringProvider |
||
| 267 | */ |
||
| 268 | public function testNewFromStringThrowsExceptionWhenStringIsTooLong( string $string ) { |
||
| 269 | $this->expectException( InvalidArgumentException::class ); |
||
| 270 | $this->expectExceptionMessage( 'Number is too big' ); |
||
| 271 | |||
| 272 | Euro::newFromString( $string ); |
||
| 273 | } |
||
| 274 | |||
| 275 | public function tooLongStringProvider() { |
||
| 276 | yield [ '1111111111111111111111111111111' ]; |
||
| 277 | yield [ (string)PHP_INT_MAX ]; |
||
| 278 | yield [ substr( (string)PHP_INT_MAX, 0, -2 ) ]; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function testNewFromStringHandlesLongStrings() { |
||
| 282 | Euro::newFromString( substr( (string)PHP_INT_MAX, 0, -3 ) ); |
||
| 283 | $this->assertTrue( true ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @dataProvider tooHighNumberProvider |
||
| 288 | */ |
||
| 289 | public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh( int $int ) { |
||
| 290 | $this->expectException( InvalidArgumentException::class ); |
||
| 291 | $this->expectExceptionMessage( 'Number is too big' ); |
||
| 292 | Euro::newFromInt( $int ); |
||
| 293 | } |
||
| 294 | |||
| 295 | public function tooHighNumberProvider() { |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @dataProvider tooHighNumberProvider |
||
| 303 | */ |
||
| 304 | public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh( int $int ) { |
||
| 308 | } |
||
| 309 | |||
| 310 | public function testNewFromIntHandlesBigIntegers() { |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | } |
||
| 322 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.