@@ -1,16 +1,16 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare( strict_types = 1 ); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | -if ( PHP_SAPI !== 'cli' ) { |
|
| 6 | - die( 'Not an entry point' ); |
|
| 5 | +if (PHP_SAPI !== 'cli') { |
|
| 6 | + die('Not an entry point'); |
|
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | error_reporting( -1 ); |
| 10 | -ini_set( 'display_errors', '1' ); |
|
| 10 | +ini_set('display_errors', '1'); |
|
| 11 | 11 | |
| 12 | -if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
|
| 13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 12 | +if (!is_readable(__DIR__.'/../vendor/autoload.php')) { |
|
| 13 | + die('You need to install this package with Composer before you can run the tests'); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | -require __DIR__ . '/../vendor/autoload.php'; |
|
| 16 | +require __DIR__.'/../vendor/autoload.php'; |
|
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare( strict_types = 1 ); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace WMDE\Euro; |
| 6 | 6 | |
@@ -21,9 +21,9 @@ discard block |
||
| 21 | 21 | * @param int $cents |
| 22 | 22 | * @throws InvalidArgumentException |
| 23 | 23 | */ |
| 24 | - private function __construct( int $cents ) { |
|
| 25 | - if ( $cents < 0 ) { |
|
| 26 | - throw new InvalidArgumentException( 'Amount needs to be positive' ); |
|
| 24 | + private function __construct(int $cents) { |
|
| 25 | + if ($cents < 0) { |
|
| 26 | + throw new InvalidArgumentException('Amount needs to be positive'); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | $this->cents = $cents; |
@@ -48,8 +48,8 @@ discard block |
||
| 48 | 48 | * @return self |
| 49 | 49 | * @throws InvalidArgumentException |
| 50 | 50 | */ |
| 51 | - public static function newFromCents( int $cents ): self { |
|
| 52 | - return new self( $cents ); |
|
| 51 | + public static function newFromCents(int $cents): self { |
|
| 52 | + return new self($cents); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
@@ -63,40 +63,40 @@ discard block |
||
| 63 | 63 | * @return self |
| 64 | 64 | * @throws InvalidArgumentException |
| 65 | 65 | */ |
| 66 | - public static function newFromString( string $euroAmount ): self { |
|
| 67 | - if ( !is_numeric( $euroAmount ) ) { |
|
| 68 | - throw new InvalidArgumentException( 'Not a number' ); |
|
| 66 | + public static function newFromString(string $euroAmount): self { |
|
| 67 | + if (!is_numeric($euroAmount)) { |
|
| 68 | + throw new InvalidArgumentException('Not a number'); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - if ( self::stringIsTooLong( $euroAmount ) ) { |
|
| 72 | - throw new InvalidArgumentException( 'Number is too big' ); |
|
| 71 | + if (self::stringIsTooLong($euroAmount)) { |
|
| 72 | + throw new InvalidArgumentException('Number is too big'); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $parts = explode( '.', $euroAmount, 2 ); |
|
| 75 | + $parts = explode('.', $euroAmount, 2); |
|
| 76 | 76 | |
| 77 | - $euros = (int)$parts[0]; |
|
| 78 | - $cents = self::centsFromString( $parts[1] ?? '0' ); |
|
| 77 | + $euros = (int) $parts[0]; |
|
| 78 | + $cents = self::centsFromString($parts[1] ?? '0'); |
|
| 79 | 79 | |
| 80 | - return new self( $euros * self::CENTS_PER_EURO + $cents ); |
|
| 80 | + return new self($euros * self::CENTS_PER_EURO + $cents); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - private static function stringIsTooLong( string $euroString ): bool { |
|
| 84 | - return strlen( $euroString ) + 2 > log10( PHP_INT_MAX ); |
|
| 83 | + private static function stringIsTooLong(string $euroString): bool { |
|
| 84 | + return strlen($euroString) + 2 > log10(PHP_INT_MAX); |
|
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - private static function centsFromString( string $cents ): int { |
|
| 88 | - if ( strlen( $cents ) > self::DECIMAL_COUNT ) { |
|
| 89 | - return self::roundCentsToInt( $cents ); |
|
| 87 | + private static function centsFromString(string $cents): int { |
|
| 88 | + if (strlen($cents) > self::DECIMAL_COUNT) { |
|
| 89 | + return self::roundCentsToInt($cents); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | // Turn .1 into .10, so it ends up as 10 cents |
| 93 | - return (int)str_pad( $cents, self::DECIMAL_COUNT, '0' ); |
|
| 93 | + return (int) str_pad($cents, self::DECIMAL_COUNT, '0'); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - private static function roundCentsToInt( string $cents ): int { |
|
| 97 | - $centsInt = (int)substr( $cents, 0, self::DECIMAL_COUNT ); |
|
| 96 | + private static function roundCentsToInt(string $cents): int { |
|
| 97 | + $centsInt = (int) substr($cents, 0, self::DECIMAL_COUNT); |
|
| 98 | 98 | |
| 99 | - if ( (int)$cents[self::DECIMAL_COUNT] >= 5 ) { |
|
| 99 | + if ((int) $cents[self::DECIMAL_COUNT] >= 5) { |
|
| 100 | 100 | $centsInt++; |
| 101 | 101 | } |
| 102 | 102 | |
@@ -112,26 +112,26 @@ discard block |
||
| 112 | 112 | * @return self |
| 113 | 113 | * @throws InvalidArgumentException |
| 114 | 114 | */ |
| 115 | - public static function newFromFloat( float $euroAmount ): self { |
|
| 116 | - self::assertMaximumValueNotExceeded( $euroAmount ); |
|
| 117 | - return new self( intval( |
|
| 115 | + public static function newFromFloat(float $euroAmount): self { |
|
| 116 | + self::assertMaximumValueNotExceeded($euroAmount); |
|
| 117 | + return new self(intval( |
|
| 118 | 118 | round( |
| 119 | - round( $euroAmount, self::DECIMAL_COUNT ) * self::CENTS_PER_EURO, |
|
| 119 | + round($euroAmount, self::DECIMAL_COUNT) * self::CENTS_PER_EURO, |
|
| 120 | 120 | 0 |
| 121 | 121 | ) |
| 122 | - ) ); |
|
| 122 | + )); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
| 126 | 126 | * @param int|float $euroAmount |
| 127 | 127 | */ |
| 128 | - private static function assertMaximumValueNotExceeded( $euroAmount ): void { |
|
| 128 | + private static function assertMaximumValueNotExceeded($euroAmount): void { |
|
| 129 | 129 | // When $euroAmount == PHP_INT_MAX / self::CENTS_PER_EURO, multiplying it |
| 130 | 130 | // by self::CENTS_PER_EURO still exceeds PHP_INT_MAX, which leads type errors |
| 131 | 131 | // due to float conversion. The safest thing to do here is using a safety |
| 132 | 132 | // margin of 1 with self::CENTS_PER_EURO |
| 133 | - if ( $euroAmount > floor( PHP_INT_MAX / ( self::CENTS_PER_EURO + 1 ) ) ) { |
|
| 134 | - throw new InvalidArgumentException( 'Number is too big' ); |
|
| 133 | + if ($euroAmount > floor(PHP_INT_MAX / (self::CENTS_PER_EURO + 1))) { |
|
| 134 | + throw new InvalidArgumentException('Number is too big'); |
|
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
@@ -140,9 +140,9 @@ discard block |
||
| 140 | 140 | * @return self |
| 141 | 141 | * @throws InvalidArgumentException |
| 142 | 142 | */ |
| 143 | - public static function newFromInt( int $euroAmount ): self { |
|
| 144 | - self::assertMaximumValueNotExceeded( $euroAmount ); |
|
| 145 | - return new self( $euroAmount * self::CENTS_PER_EURO ); |
|
| 143 | + public static function newFromInt(int $euroAmount): self { |
|
| 144 | + self::assertMaximumValueNotExceeded($euroAmount); |
|
| 145 | + return new self($euroAmount * self::CENTS_PER_EURO); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | public function getEuroCents(): int { |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | public function getEuros(): int { |
| 157 | - return intval( $this->cents / self::CENTS_PER_EURO ); |
|
| 157 | + return intval($this->cents / self::CENTS_PER_EURO); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -163,10 +163,10 @@ discard block |
||
| 163 | 163 | * @return string |
| 164 | 164 | */ |
| 165 | 165 | public function getEuroString(): string { |
| 166 | - return number_format( $this->getEuroFloat(), self::DECIMAL_COUNT, '.', '' ); |
|
| 166 | + return number_format($this->getEuroFloat(), self::DECIMAL_COUNT, '.', ''); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - public function equals( Euro $euro ): bool { |
|
| 169 | + public function equals(Euro $euro): bool { |
|
| 170 | 170 | return $this->cents === $euro->cents; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare( strict_types = 1 ); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace WMDE\Euro\Tests\Unit; |
| 6 | 6 | |
@@ -19,235 +19,235 @@ discard block |
||
| 19 | 19 | /** |
| 20 | 20 | * @dataProvider unsignedIntegerProvider |
| 21 | 21 | */ |
| 22 | - public function testGetCentsReturnsConstructorArgument( int $unsignedInteger ) { |
|
| 23 | - $amount = Euro::newFromCents( $unsignedInteger ); |
|
| 24 | - $this->assertSame( $unsignedInteger, $amount->getEuroCents() ); |
|
| 22 | + public function testGetCentsReturnsConstructorArgument(int $unsignedInteger) { |
|
| 23 | + $amount = Euro::newFromCents($unsignedInteger); |
|
| 24 | + $this->assertSame($unsignedInteger, $amount->getEuroCents()); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | public function unsignedIntegerProvider() { |
| 28 | 28 | return [ |
| 29 | - [ 0 ], [ 1 ], [ 2 ], [ 9 ], [ 10 ], [ 11 ], |
|
| 30 | - [ 99 ], [ 100 ], [ 101 ], [ 999 ], [ 1000 ], [ 1001 ], |
|
| 29 | + [0], [1], [2], [9], [10], [11], |
|
| 30 | + [99], [100], [101], [999], [1000], [1001], |
|
| 31 | 31 | ]; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | public function testGivenZero_getEuroFloatReturnsZeroFloat() { |
| 35 | - $amount = Euro::newFromCents( 0 ); |
|
| 36 | - $this->assertExactFloat( 0.0, $amount->getEuroFloat() ); |
|
| 37 | - $this->assertNotSame( 0, $amount->getEuroFloat() ); |
|
| 35 | + $amount = Euro::newFromCents(0); |
|
| 36 | + $this->assertExactFloat(0.0, $amount->getEuroFloat()); |
|
| 37 | + $this->assertNotSame(0, $amount->getEuroFloat()); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - private function assertExactFloat( $expected, $actual ) { |
|
| 41 | - $this->assertIsFloat( $actual ); |
|
| 42 | - $this->assertEquals( $expected, $actual, '', 0 ); |
|
| 40 | + private function assertExactFloat($expected, $actual) { |
|
| 41 | + $this->assertIsFloat($actual); |
|
| 42 | + $this->assertEquals($expected, $actual, '', 0); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | public function testGivenOneEuro_getEuroFloatReturnsOne() { |
| 46 | - $amount = Euro::newFromCents( 100 ); |
|
| 47 | - $this->assertExactFloat( 1.0, $amount->getEuroFloat() ); |
|
| 46 | + $amount = Euro::newFromCents(100); |
|
| 47 | + $this->assertExactFloat(1.0, $amount->getEuroFloat()); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | public function testGivenOneCent_getEuroFloatReturnsPointZeroOne() { |
| 51 | - $amount = Euro::newFromCents( 1 ); |
|
| 52 | - $this->assertExactFloat( 0.01, $amount->getEuroFloat() ); |
|
| 51 | + $amount = Euro::newFromCents(1); |
|
| 52 | + $this->assertExactFloat(0.01, $amount->getEuroFloat()); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public function testGiven33cents_getEuroFloatReturnsPointThreeThree() { |
| 56 | - $amount = Euro::newFromCents( 33 ); |
|
| 57 | - $this->assertExactFloat( 0.33, $amount->getEuroFloat() ); |
|
| 56 | + $amount = Euro::newFromCents(33); |
|
| 57 | + $this->assertExactFloat(0.33, $amount->getEuroFloat()); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * @dataProvider getEurosDataProvider |
| 62 | 62 | */ |
| 63 | - public function testGetEurosReturnsCorrectValues( int $cents, int $expectedEuros ) { |
|
| 64 | - $amount = Euro::newFromCents( $cents ); |
|
| 65 | - $this->assertEquals( $expectedEuros, $amount->getEuros() ); |
|
| 63 | + public function testGetEurosReturnsCorrectValues(int $cents, int $expectedEuros) { |
|
| 64 | + $amount = Euro::newFromCents($cents); |
|
| 65 | + $this->assertEquals($expectedEuros, $amount->getEuros()); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public function getEurosDataProvider(): array { |
| 69 | 69 | return [ |
| 70 | - [ 0, 0 ], |
|
| 71 | - [ 3, 0 ], |
|
| 72 | - [ 102, 1 ], |
|
| 73 | - [ 149, 1 ], |
|
| 74 | - [ 150, 1 ], |
|
| 75 | - [ 151, 1 ], |
|
| 76 | - [ 199, 1 ], |
|
| 77 | - [ 555, 5 ], |
|
| 78 | - [ 1033, 10 ], |
|
| 79 | - [ 9999, 99 ], |
|
| 70 | + [0, 0], |
|
| 71 | + [3, 0], |
|
| 72 | + [102, 1], |
|
| 73 | + [149, 1], |
|
| 74 | + [150, 1], |
|
| 75 | + [151, 1], |
|
| 76 | + [199, 1], |
|
| 77 | + [555, 5], |
|
| 78 | + [1033, 10], |
|
| 79 | + [9999, 99], |
|
| 80 | 80 | ]; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | public function testGivenNegativeAmount_constructorThrowsException() { |
| 84 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 84 | + $this->expectException(\InvalidArgumentException::class); |
|
| 85 | 85 | Euro::newFromCents( -1 ); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | public function testGivenZero_getEuroStringReturnsZeroString() { |
| 89 | - $amount = Euro::newFromCents( 0 ); |
|
| 90 | - $this->assertSame( '0.00', $amount->getEuroString() ); |
|
| 89 | + $amount = Euro::newFromCents(0); |
|
| 90 | + $this->assertSame('0.00', $amount->getEuroString()); |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() { |
| 94 | - $amount = Euro::newFromCents( 100 ); |
|
| 95 | - $this->assertSame( '1.00', $amount->getEuroString() ); |
|
| 94 | + $amount = Euro::newFromCents(100); |
|
| 95 | + $this->assertSame('1.00', $amount->getEuroString()); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() { |
| 99 | - $amount = Euro::newFromCents( 200 ); |
|
| 100 | - $this->assertSame( '2.00', $amount->getEuroString() ); |
|
| 99 | + $amount = Euro::newFromCents(200); |
|
| 100 | + $this->assertSame('2.00', $amount->getEuroString()); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() { |
| 104 | - $amount = Euro::newFromCents( 1 ); |
|
| 105 | - $this->assertSame( '0.01', $amount->getEuroString() ); |
|
| 104 | + $amount = Euro::newFromCents(1); |
|
| 105 | + $this->assertSame('0.01', $amount->getEuroString()); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() { |
| 109 | - $amount = Euro::newFromCents( 10 ); |
|
| 110 | - $this->assertSame( '0.10', $amount->getEuroString() ); |
|
| 109 | + $amount = Euro::newFromCents(10); |
|
| 110 | + $this->assertSame('0.10', $amount->getEuroString()); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | public function testGiven1234Cents_getEuroStringReturns12euro34() { |
| 114 | - $amount = Euro::newFromCents( 1234 ); |
|
| 115 | - $this->assertSame( '12.34', $amount->getEuroString() ); |
|
| 114 | + $amount = Euro::newFromCents(1234); |
|
| 115 | + $this->assertSame('12.34', $amount->getEuroString()); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | public function testGiven9876Cents_stringCastingReturns98euro76() { |
| 119 | - $amount = Euro::newFromCents( 9876 ); |
|
| 120 | - $this->assertSame( '98.76', (string)$amount ); |
|
| 119 | + $amount = Euro::newFromCents(9876); |
|
| 120 | + $this->assertSame('98.76', (string) $amount); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | public function testGivenEuroAmount_jsonEncodeWillEncodeProperly() { |
| 124 | - $amount = Euro::newFromCents( 9876 ); |
|
| 125 | - $this->assertSame( '"98.76"', json_encode( $amount ) ); |
|
| 124 | + $amount = Euro::newFromCents(9876); |
|
| 125 | + $this->assertSame('"98.76"', json_encode($amount)); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | public function testOneEuroString_getsTurnedInto100cents() { |
| 129 | - $this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() ); |
|
| 129 | + $this->assertSame(100, Euro::newFromString('1.00')->getEuroCents()); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | public function testOneCentString_getsTurnedInto1cents() { |
| 133 | - $this->assertSame( 1, Euro::newFromString( '0.01' )->getEuroCents() ); |
|
| 133 | + $this->assertSame(1, Euro::newFromString('0.01')->getEuroCents()); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | public function testTenCentString_getsTurnedInto10cents() { |
| 137 | - $this->assertSame( 10, Euro::newFromString( '0.10' )->getEuroCents() ); |
|
| 137 | + $this->assertSame(10, Euro::newFromString('0.10')->getEuroCents()); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | public function testShortTenCentString_getsTurnedInto10cents() { |
| 141 | - $this->assertSame( 10, Euro::newFromString( '0.1' )->getEuroCents() ); |
|
| 141 | + $this->assertSame(10, Euro::newFromString('0.1')->getEuroCents()); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | public function testShortOneEuroString_getsTurnedInto100cents() { |
| 145 | - $this->assertSame( 100, Euro::newFromString( '1' )->getEuroCents() ); |
|
| 145 | + $this->assertSame(100, Euro::newFromString('1')->getEuroCents()); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
| 149 | - $this->assertSame( 100, Euro::newFromString( '1.0' )->getEuroCents() ); |
|
| 149 | + $this->assertSame(100, Euro::newFromString('1.0')->getEuroCents()); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
| 153 | - $this->assertSame( 100, Euro::newFromString( '1.00000' )->getEuroCents() ); |
|
| 153 | + $this->assertSame(100, Euro::newFromString('1.00000')->getEuroCents()); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | public function testHandlingOfLargeEuroString() { |
| 157 | - $this->assertSame( 3133742, Euro::newFromString( '31337.42' )->getEuroCents() ); |
|
| 157 | + $this->assertSame(3133742, Euro::newFromString('31337.42')->getEuroCents()); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
| 161 | 161 | // Regression test for https://phabricator.wikimedia.org/T183481 |
| 162 | - $this->assertSame( 870, Euro::newFromString( '8.70' )->getEuroCents() ); |
|
| 163 | - $this->assertSame( 920, Euro::newFromString( '9.20' )->getEuroCents() ); |
|
| 162 | + $this->assertSame(870, Euro::newFromString('8.70')->getEuroCents()); |
|
| 163 | + $this->assertSame(920, Euro::newFromString('9.20')->getEuroCents()); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
| 167 | - $this->assertSame( 101, Euro::newFromString( '1.0100000001' )->getEuroCents() ); |
|
| 168 | - $this->assertSame( 101, Euro::newFromString( '1.010000009999' )->getEuroCents() ); |
|
| 169 | - $this->assertSame( 101, Euro::newFromString( '1.011' )->getEuroCents() ); |
|
| 170 | - $this->assertSame( 101, Euro::newFromString( '1.014' )->getEuroCents() ); |
|
| 171 | - $this->assertSame( 101, Euro::newFromString( '1.0149' )->getEuroCents() ); |
|
| 172 | - $this->assertSame( 102, Euro::newFromString( '1.015' )->getEuroCents() ); |
|
| 173 | - $this->assertSame( 102, Euro::newFromString( '1.019' )->getEuroCents() ); |
|
| 174 | - $this->assertSame( 102, Euro::newFromString( '1.0199999' )->getEuroCents() ); |
|
| 175 | - $this->assertSame( 870, Euro::newFromString( '8.701' )->getEuroCents() ); |
|
| 176 | - $this->assertSame( 870, Euro::newFromString( '8.70499' )->getEuroCents() ); |
|
| 177 | - $this->assertSame( 871, Euro::newFromString( '8.705' )->getEuroCents() ); |
|
| 178 | - $this->assertSame( 871, Euro::newFromString( '8.705000' )->getEuroCents() ); |
|
| 179 | - $this->assertSame( 871, Euro::newFromString( '8.705001' )->getEuroCents() ); |
|
| 180 | - $this->assertSame( 871, Euro::newFromString( '8.709999' )->getEuroCents() ); |
|
| 167 | + $this->assertSame(101, Euro::newFromString('1.0100000001')->getEuroCents()); |
|
| 168 | + $this->assertSame(101, Euro::newFromString('1.010000009999')->getEuroCents()); |
|
| 169 | + $this->assertSame(101, Euro::newFromString('1.011')->getEuroCents()); |
|
| 170 | + $this->assertSame(101, Euro::newFromString('1.014')->getEuroCents()); |
|
| 171 | + $this->assertSame(101, Euro::newFromString('1.0149')->getEuroCents()); |
|
| 172 | + $this->assertSame(102, Euro::newFromString('1.015')->getEuroCents()); |
|
| 173 | + $this->assertSame(102, Euro::newFromString('1.019')->getEuroCents()); |
|
| 174 | + $this->assertSame(102, Euro::newFromString('1.0199999')->getEuroCents()); |
|
| 175 | + $this->assertSame(870, Euro::newFromString('8.701')->getEuroCents()); |
|
| 176 | + $this->assertSame(870, Euro::newFromString('8.70499')->getEuroCents()); |
|
| 177 | + $this->assertSame(871, Euro::newFromString('8.705')->getEuroCents()); |
|
| 178 | + $this->assertSame(871, Euro::newFromString('8.705000')->getEuroCents()); |
|
| 179 | + $this->assertSame(871, Euro::newFromString('8.705001')->getEuroCents()); |
|
| 180 | + $this->assertSame(871, Euro::newFromString('8.709999')->getEuroCents()); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
| 184 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 185 | - Euro::newFromString( '-1.00' ); |
|
| 184 | + $this->expectException(\InvalidArgumentException::class); |
|
| 185 | + Euro::newFromString('-1.00'); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | public function testGivenStringWithComma_exceptionIsThrown() { |
| 189 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 190 | - Euro::newFromString( '1,00' ); |
|
| 189 | + $this->expectException(\InvalidArgumentException::class); |
|
| 190 | + Euro::newFromString('1,00'); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
| 194 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 195 | - Euro::newFromString( '1.0.0' ); |
|
| 194 | + $this->expectException(\InvalidArgumentException::class); |
|
| 195 | + Euro::newFromString('1.0.0'); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | public function testGivenNonNumber_exceptionIsThrown() { |
| 199 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 200 | - Euro::newFromString( '1.00abc' ); |
|
| 199 | + $this->expectException(\InvalidArgumentException::class); |
|
| 200 | + Euro::newFromString('1.00abc'); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
| 204 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 204 | + $this->expectException(\InvalidArgumentException::class); |
|
| 205 | 205 | Euro::newFromFloat( -1.00 ); |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | public function testOneEuroFloat_getsTurnedInto100cents() { |
| 209 | - $this->assertSame( 100, Euro::newFromFloat( 1.0 )->getEuroCents() ); |
|
| 209 | + $this->assertSame(100, Euro::newFromFloat(1.0)->getEuroCents()); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | public function testOneCentFloat_getsTurnedInto1cent() { |
| 213 | - $this->assertSame( 1, Euro::newFromFloat( 0.01 )->getEuroCents() ); |
|
| 213 | + $this->assertSame(1, Euro::newFromFloat(0.01)->getEuroCents()); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | public function testTenCentFloat_getsTurnedInto10cents() { |
| 217 | - $this->assertSame( 10, Euro::newFromFloat( 0.1 )->getEuroCents() ); |
|
| 217 | + $this->assertSame(10, Euro::newFromFloat(0.1)->getEuroCents()); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | public function testHandlingOfLargeEuroFloat() { |
| 221 | - $this->assertSame( 3133742, Euro::newFromFloat( 31337.42 )->getEuroCents() ); |
|
| 221 | + $this->assertSame(3133742, Euro::newFromFloat(31337.42)->getEuroCents()); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
| 225 | - $this->assertSame( 101, Euro::newFromFloat( 1.0100000001 )->getEuroCents() ); |
|
| 226 | - $this->assertSame( 101, Euro::newFromFloat( 1.010000009999 )->getEuroCents() ); |
|
| 227 | - $this->assertSame( 101, Euro::newFromFloat( 1.011 )->getEuroCents() ); |
|
| 228 | - $this->assertSame( 101, Euro::newFromFloat( 1.014 )->getEuroCents() ); |
|
| 229 | - $this->assertSame( 101, Euro::newFromFloat( 1.0149 )->getEuroCents() ); |
|
| 230 | - $this->assertSame( 102, Euro::newFromFloat( 1.015 )->getEuroCents() ); |
|
| 231 | - $this->assertSame( 102, Euro::newFromFloat( 1.019 )->getEuroCents() ); |
|
| 232 | - $this->assertSame( 102, Euro::newFromFloat( 1.0199999 )->getEuroCents() ); |
|
| 233 | - $this->assertSame( 870, Euro::newFromFloat( 8.70 )->getEuroCents() ); |
|
| 225 | + $this->assertSame(101, Euro::newFromFloat(1.0100000001)->getEuroCents()); |
|
| 226 | + $this->assertSame(101, Euro::newFromFloat(1.010000009999)->getEuroCents()); |
|
| 227 | + $this->assertSame(101, Euro::newFromFloat(1.011)->getEuroCents()); |
|
| 228 | + $this->assertSame(101, Euro::newFromFloat(1.014)->getEuroCents()); |
|
| 229 | + $this->assertSame(101, Euro::newFromFloat(1.0149)->getEuroCents()); |
|
| 230 | + $this->assertSame(102, Euro::newFromFloat(1.015)->getEuroCents()); |
|
| 231 | + $this->assertSame(102, Euro::newFromFloat(1.019)->getEuroCents()); |
|
| 232 | + $this->assertSame(102, Euro::newFromFloat(1.0199999)->getEuroCents()); |
|
| 233 | + $this->assertSame(870, Euro::newFromFloat(8.70)->getEuroCents()); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | public function testZeroEuroIntegers_isZeroCents() { |
| 237 | - $this->assertSame( 0, Euro::newFromInt( 0 )->getEuroCents() ); |
|
| 237 | + $this->assertSame(0, Euro::newFromInt(0)->getEuroCents()); |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | public function testOneEuroIntegers_is100Cents() { |
| 241 | - $this->assertSame( 100, Euro::newFromInt( 1 )->getEuroCents() ); |
|
| 241 | + $this->assertSame(100, Euro::newFromInt(1)->getEuroCents()); |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | // phpcs:ignore MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName |
| 245 | 245 | public function test1337EuroIntegers_is133700Cents() { |
| 246 | - $this->assertSame( 133700, Euro::newFromInt( 1337 )->getEuroCents() ); |
|
| 246 | + $this->assertSame(133700, Euro::newFromInt(1337)->getEuroCents()); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
| 250 | - $this->expectException( \InvalidArgumentException::class ); |
|
| 250 | + $this->expectException(\InvalidArgumentException::class); |
|
| 251 | 251 | Euro::newFromInt( -1 ); |
| 252 | 252 | } |
| 253 | 253 | |
@@ -255,89 +255,89 @@ discard block |
||
| 255 | 255 | * @dataProvider euroProvider |
| 256 | 256 | * @param Euro $euro |
| 257 | 257 | */ |
| 258 | - public function testEuroEqualsItself( Euro $euro ) { |
|
| 259 | - $this->assertTrue( $euro->equals( clone $euro ) ); |
|
| 258 | + public function testEuroEqualsItself(Euro $euro) { |
|
| 259 | + $this->assertTrue($euro->equals(clone $euro)); |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | public function euroProvider() { |
| 263 | 263 | return [ |
| 264 | - [ Euro::newFromCents( 0 ) ], |
|
| 265 | - [ Euro::newFromCents( 1 ) ], |
|
| 266 | - [ Euro::newFromCents( 99 ) ], |
|
| 267 | - [ Euro::newFromCents( 100 ) ], |
|
| 268 | - [ Euro::newFromCents( 9999 ) ], |
|
| 264 | + [Euro::newFromCents(0)], |
|
| 265 | + [Euro::newFromCents(1)], |
|
| 266 | + [Euro::newFromCents(99)], |
|
| 267 | + [Euro::newFromCents(100)], |
|
| 268 | + [Euro::newFromCents(9999)], |
|
| 269 | 269 | ]; |
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | public function testOneCentDoesNotEqualOneEuro() { |
| 273 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromInt( 1 ) ) ); |
|
| 273 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromInt(1))); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | public function testOneCentDoesNotEqualTwoCents() { |
| 277 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 2 ) ) ); |
|
| 277 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromCents(2))); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
| 281 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 101 ) ) ); |
|
| 281 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromCents(101))); |
|
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | public function test9001centsDoesNotEqual9000cents() { |
| 285 | - $this->assertFalse( Euro::newFromCents( 9001 )->equals( Euro::newFromCents( 9000 ) ) ); |
|
| 285 | + $this->assertFalse(Euro::newFromCents(9001)->equals(Euro::newFromCents(9000))); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | /** |
| 289 | 289 | * @dataProvider tooLongStringProvider |
| 290 | 290 | */ |
| 291 | - public function testNewFromStringThrowsExceptionWhenStringIsTooLong( string $string ) { |
|
| 292 | - $this->expectException( InvalidArgumentException::class ); |
|
| 293 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
| 291 | + public function testNewFromStringThrowsExceptionWhenStringIsTooLong(string $string) { |
|
| 292 | + $this->expectException(InvalidArgumentException::class); |
|
| 293 | + $this->expectExceptionMessage('Number is too big'); |
|
| 294 | 294 | |
| 295 | - Euro::newFromString( $string ); |
|
| 295 | + Euro::newFromString($string); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | public function tooLongStringProvider() { |
| 299 | - yield [ '1111111111111111111111111111111' ]; |
|
| 300 | - yield [ (string)PHP_INT_MAX ]; |
|
| 301 | - yield [ substr( (string)PHP_INT_MAX, 0, -2 ) ]; |
|
| 299 | + yield ['1111111111111111111111111111111']; |
|
| 300 | + yield [(string) PHP_INT_MAX]; |
|
| 301 | + yield [substr((string) PHP_INT_MAX, 0, -2)]; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | public function testNewFromStringHandlesLongStrings() { |
| 305 | - Euro::newFromString( substr( (string)PHP_INT_MAX, 0, -3 ) ); |
|
| 306 | - $this->assertTrue( true ); |
|
| 305 | + Euro::newFromString(substr((string) PHP_INT_MAX, 0, -3)); |
|
| 306 | + $this->assertTrue(true); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | /** |
| 310 | 310 | * @dataProvider tooHighNumberProvider |
| 311 | 311 | */ |
| 312 | - public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh( int $int ) { |
|
| 313 | - $this->expectException( InvalidArgumentException::class ); |
|
| 314 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
| 315 | - Euro::newFromInt( $int ); |
|
| 312 | + public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh(int $int) { |
|
| 313 | + $this->expectException(InvalidArgumentException::class); |
|
| 314 | + $this->expectExceptionMessage('Number is too big'); |
|
| 315 | + Euro::newFromInt($int); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | 318 | public function tooHighNumberProvider() { |
| 319 | - yield [ PHP_INT_MAX ]; |
|
| 320 | - yield [ (int)floor( PHP_INT_MAX / 10 ) ]; |
|
| 321 | - yield [ (int)floor( PHP_INT_MAX / 100 ) ]; |
|
| 319 | + yield [PHP_INT_MAX]; |
|
| 320 | + yield [(int) floor(PHP_INT_MAX / 10)]; |
|
| 321 | + yield [(int) floor(PHP_INT_MAX / 100)]; |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | /** |
| 325 | 325 | * @dataProvider tooHighNumberProvider |
| 326 | 326 | */ |
| 327 | - public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh( int $int ) { |
|
| 328 | - $this->expectException( InvalidArgumentException::class ); |
|
| 329 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
| 330 | - Euro::newFromFloat( (float)$int ); |
|
| 327 | + public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh(int $int) { |
|
| 328 | + $this->expectException(InvalidArgumentException::class); |
|
| 329 | + $this->expectExceptionMessage('Number is too big'); |
|
| 330 | + Euro::newFromFloat((float) $int); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | public function testNewFromIntHandlesBigIntegers() { |
| 334 | 334 | // Edge case test for the highest allowed value (Euro::CENTS_PER_EURO +1 ) |
| 335 | 335 | // 100 (Euro::CENTS_PER_EURO) does not work due to rounding |
| 336 | - $number = (int)floor( PHP_INT_MAX / 101 ); |
|
| 336 | + $number = (int) floor(PHP_INT_MAX / 101); |
|
| 337 | 337 | |
| 338 | 338 | $this->assertSame( |
| 339 | 339 | $number * 100, |
| 340 | - Euro::newFromInt( $number )->getEuroCents() |
|
| 340 | + Euro::newFromInt($number)->getEuroCents() |
|
| 341 | 341 | ); |
| 342 | 342 | } |
| 343 | 343 | |