@@ -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; |
@@ -34,8 +34,8 @@ discard block |
||
34 | 34 | * @return self |
35 | 35 | * @throws InvalidArgumentException |
36 | 36 | */ |
37 | - public static function newFromCents( int $cents ): self { |
|
38 | - return new self( $cents ); |
|
37 | + public static function newFromCents(int $cents): self { |
|
38 | + return new self($cents); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
@@ -49,40 +49,40 @@ discard block |
||
49 | 49 | * @return self |
50 | 50 | * @throws InvalidArgumentException |
51 | 51 | */ |
52 | - public static function newFromString( string $euroAmount ): self { |
|
53 | - if ( !is_numeric( $euroAmount ) ) { |
|
54 | - throw new InvalidArgumentException( 'Not a number' ); |
|
52 | + public static function newFromString(string $euroAmount): self { |
|
53 | + if (!is_numeric($euroAmount)) { |
|
54 | + throw new InvalidArgumentException('Not a number'); |
|
55 | 55 | } |
56 | 56 | |
57 | - if ( self::stringIsTooLong( $euroAmount ) ) { |
|
58 | - throw new InvalidArgumentException( 'Number is too big' ); |
|
57 | + if (self::stringIsTooLong($euroAmount)) { |
|
58 | + throw new InvalidArgumentException('Number is too big'); |
|
59 | 59 | } |
60 | 60 | |
61 | - $parts = explode( '.', $euroAmount, 2 ); |
|
61 | + $parts = explode('.', $euroAmount, 2); |
|
62 | 62 | |
63 | - $euros = (int)$parts[0]; |
|
64 | - $cents = self::centsFromString( $parts[1] ?? '0' ); |
|
63 | + $euros = (int) $parts[0]; |
|
64 | + $cents = self::centsFromString($parts[1] ?? '0'); |
|
65 | 65 | |
66 | - return new self( $euros * self::CENTS_PER_EURO + $cents ); |
|
66 | + return new self($euros * self::CENTS_PER_EURO + $cents); |
|
67 | 67 | } |
68 | 68 | |
69 | - private static function stringIsTooLong( string $euroString ): bool { |
|
70 | - return strlen( $euroString ) + 2 > log10( PHP_INT_MAX ); |
|
69 | + private static function stringIsTooLong(string $euroString): bool { |
|
70 | + return strlen($euroString) + 2 > log10(PHP_INT_MAX); |
|
71 | 71 | } |
72 | 72 | |
73 | - private static function centsFromString( string $cents ): int { |
|
74 | - if ( strlen( $cents ) > self::DECIMAL_COUNT ) { |
|
75 | - return self::roundCentsToInt( $cents ); |
|
73 | + private static function centsFromString(string $cents): int { |
|
74 | + if (strlen($cents) > self::DECIMAL_COUNT) { |
|
75 | + return self::roundCentsToInt($cents); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // Turn .1 into .10, so it ends up as 10 cents |
79 | - return (int)str_pad( $cents, self::DECIMAL_COUNT, '0' ); |
|
79 | + return (int) str_pad($cents, self::DECIMAL_COUNT, '0'); |
|
80 | 80 | } |
81 | 81 | |
82 | - private static function roundCentsToInt( string $cents ): int { |
|
83 | - $centsInt = (int)substr( $cents, 0, self::DECIMAL_COUNT ); |
|
82 | + private static function roundCentsToInt(string $cents): int { |
|
83 | + $centsInt = (int) substr($cents, 0, self::DECIMAL_COUNT); |
|
84 | 84 | |
85 | - if ( (int)$cents[self::DECIMAL_COUNT] >= 5 ) { |
|
85 | + if ((int) $cents[self::DECIMAL_COUNT] >= 5) { |
|
86 | 86 | $centsInt++; |
87 | 87 | } |
88 | 88 | |
@@ -98,26 +98,26 @@ discard block |
||
98 | 98 | * @return self |
99 | 99 | * @throws InvalidArgumentException |
100 | 100 | */ |
101 | - public static function newFromFloat( float $euroAmount ): self { |
|
102 | - self::assertMaximumValueNotExceeded( $euroAmount ); |
|
103 | - return new self( intval( |
|
101 | + public static function newFromFloat(float $euroAmount): self { |
|
102 | + self::assertMaximumValueNotExceeded($euroAmount); |
|
103 | + return new self(intval( |
|
104 | 104 | round( |
105 | - round( $euroAmount, self::DECIMAL_COUNT ) * self::CENTS_PER_EURO, |
|
105 | + round($euroAmount, self::DECIMAL_COUNT) * self::CENTS_PER_EURO, |
|
106 | 106 | 0 |
107 | 107 | ) |
108 | - ) ); |
|
108 | + )); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
112 | 112 | * @param int|float $euroAmount |
113 | 113 | */ |
114 | - private static function assertMaximumValueNotExceeded( $euroAmount ): void { |
|
114 | + private static function assertMaximumValueNotExceeded($euroAmount): void { |
|
115 | 115 | // When $euroAmount == PHP_INT_MAX / self::CENTS_PER_EURO, multiplying it |
116 | 116 | // by self::CENTS_PER_EURO still exceeds PHP_INT_MAX, which leads type errors |
117 | 117 | // due to float conversion. The safest thing to do here is using a safety |
118 | 118 | // margin of 1 with self::CENTS_PER_EURO |
119 | - if ( $euroAmount > floor( PHP_INT_MAX / ( self::CENTS_PER_EURO + 1 ) ) ) { |
|
120 | - throw new InvalidArgumentException( 'Number is too big' ); |
|
119 | + if ($euroAmount > floor(PHP_INT_MAX / (self::CENTS_PER_EURO + 1))) { |
|
120 | + throw new InvalidArgumentException('Number is too big'); |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 | |
@@ -126,9 +126,9 @@ discard block |
||
126 | 126 | * @return self |
127 | 127 | * @throws InvalidArgumentException |
128 | 128 | */ |
129 | - public static function newFromInt( int $euroAmount ): self { |
|
130 | - self::assertMaximumValueNotExceeded( $euroAmount ); |
|
131 | - return new self( $euroAmount * self::CENTS_PER_EURO ); |
|
129 | + public static function newFromInt(int $euroAmount): self { |
|
130 | + self::assertMaximumValueNotExceeded($euroAmount); |
|
131 | + return new self($euroAmount * self::CENTS_PER_EURO); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | public function getEuroCents(): int { |
@@ -143,10 +143,10 @@ discard block |
||
143 | 143 | * Returns the euro amount as string with two decimals always present in format "42.00". |
144 | 144 | */ |
145 | 145 | public function getEuroString(): string { |
146 | - return number_format( $this->getEuroFloat(), self::DECIMAL_COUNT, '.', '' ); |
|
146 | + return number_format($this->getEuroFloat(), self::DECIMAL_COUNT, '.', ''); |
|
147 | 147 | } |
148 | 148 | |
149 | - public function equals( Euro $euro ): bool { |
|
149 | + public function equals(Euro $euro): bool { |
|
150 | 150 | return $this->cents === $euro->cents; |
151 | 151 | } |
152 | 152 |
@@ -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,211 +19,211 @@ 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( float $expected, $actual ) { |
|
41 | - $this->assertInternalType( 'float', $actual ); |
|
42 | - $this->assertEquals( $expected, $actual, '', 0 ); |
|
40 | + private function assertExactFloat(float $expected, $actual) { |
|
41 | + $this->assertInternalType('float', $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 | public function testGivenNegativeAmount_constructorThrowsException() { |
61 | - $this->expectException( \InvalidArgumentException::class ); |
|
61 | + $this->expectException(\InvalidArgumentException::class); |
|
62 | 62 | Euro::newFromCents( -1 ); |
63 | 63 | } |
64 | 64 | |
65 | 65 | public function testGivenZero_getEuroStringReturnsZeroString() { |
66 | - $amount = Euro::newFromCents( 0 ); |
|
67 | - $this->assertSame( '0.00', $amount->getEuroString() ); |
|
66 | + $amount = Euro::newFromCents(0); |
|
67 | + $this->assertSame('0.00', $amount->getEuroString()); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() { |
71 | - $amount = Euro::newFromCents( 100 ); |
|
72 | - $this->assertSame( '1.00', $amount->getEuroString() ); |
|
71 | + $amount = Euro::newFromCents(100); |
|
72 | + $this->assertSame('1.00', $amount->getEuroString()); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() { |
76 | - $amount = Euro::newFromCents( 200 ); |
|
77 | - $this->assertSame( '2.00', $amount->getEuroString() ); |
|
76 | + $amount = Euro::newFromCents(200); |
|
77 | + $this->assertSame('2.00', $amount->getEuroString()); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() { |
81 | - $amount = Euro::newFromCents( 1 ); |
|
82 | - $this->assertSame( '0.01', $amount->getEuroString() ); |
|
81 | + $amount = Euro::newFromCents(1); |
|
82 | + $this->assertSame('0.01', $amount->getEuroString()); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() { |
86 | - $amount = Euro::newFromCents( 10 ); |
|
87 | - $this->assertSame( '0.10', $amount->getEuroString() ); |
|
86 | + $amount = Euro::newFromCents(10); |
|
87 | + $this->assertSame('0.10', $amount->getEuroString()); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | public function testGiven1234Cents_getEuroStringReturns12euro34() { |
91 | - $amount = Euro::newFromCents( 1234 ); |
|
92 | - $this->assertSame( '12.34', $amount->getEuroString() ); |
|
91 | + $amount = Euro::newFromCents(1234); |
|
92 | + $this->assertSame('12.34', $amount->getEuroString()); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | public function testGiven9876Cents_stringCastingReturns98euro76() { |
96 | - $amount = Euro::newFromCents( 9876 ); |
|
97 | - $this->assertSame( '98.76', (string) $amount ); |
|
96 | + $amount = Euro::newFromCents(9876); |
|
97 | + $this->assertSame('98.76', (string) $amount); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | public function testGivenEuroAmount_jsonEncodeWillEncodeProperly() { |
101 | - $amount = Euro::newFromCents( 9876 ); |
|
102 | - $this->assertSame( '"98.76"', json_encode( $amount ) ); |
|
101 | + $amount = Euro::newFromCents(9876); |
|
102 | + $this->assertSame('"98.76"', json_encode($amount)); |
|
103 | 103 | } |
104 | 104 | |
105 | 105 | public function testOneEuroString_getsTurnedInto100cents() { |
106 | - $this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() ); |
|
106 | + $this->assertSame(100, Euro::newFromString('1.00')->getEuroCents()); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | public function testOneCentString_getsTurnedInto1cents() { |
110 | - $this->assertSame( 1, Euro::newFromString( '0.01' )->getEuroCents() ); |
|
110 | + $this->assertSame(1, Euro::newFromString('0.01')->getEuroCents()); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | public function testTenCentString_getsTurnedInto10cents() { |
114 | - $this->assertSame( 10, Euro::newFromString( '0.10' )->getEuroCents() ); |
|
114 | + $this->assertSame(10, Euro::newFromString('0.10')->getEuroCents()); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | public function testShortTenCentString_getsTurnedInto10cents() { |
118 | - $this->assertSame( 10, Euro::newFromString( '0.1' )->getEuroCents() ); |
|
118 | + $this->assertSame(10, Euro::newFromString('0.1')->getEuroCents()); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | public function testShortOneEuroString_getsTurnedInto100cents() { |
122 | - $this->assertSame( 100, Euro::newFromString( '1' )->getEuroCents() ); |
|
122 | + $this->assertSame(100, Euro::newFromString('1')->getEuroCents()); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
126 | - $this->assertSame( 100, Euro::newFromString( '1.0' )->getEuroCents() ); |
|
126 | + $this->assertSame(100, Euro::newFromString('1.0')->getEuroCents()); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
130 | - $this->assertSame( 100, Euro::newFromString( '1.00000' )->getEuroCents() ); |
|
130 | + $this->assertSame(100, Euro::newFromString('1.00000')->getEuroCents()); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | public function testHandlingOfLargeEuroString() { |
134 | - $this->assertSame( 3133742, Euro::newFromString( '31337.42' )->getEuroCents() ); |
|
134 | + $this->assertSame(3133742, Euro::newFromString('31337.42')->getEuroCents()); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
138 | 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() ); |
|
139 | + $this->assertSame(870, Euro::newFromString('8.70')->getEuroCents()); |
|
140 | + $this->assertSame(920, Euro::newFromString('9.20')->getEuroCents()); |
|
141 | 141 | } |
142 | 142 | |
143 | 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() ); |
|
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 | 158 | } |
159 | 159 | |
160 | 160 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
161 | - $this->expectException( \InvalidArgumentException::class ); |
|
162 | - Euro::newFromString( '-1.00' ); |
|
161 | + $this->expectException(\InvalidArgumentException::class); |
|
162 | + Euro::newFromString('-1.00'); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | public function testGivenStringWithComma_exceptionIsThrown() { |
166 | - $this->expectException( \InvalidArgumentException::class ); |
|
167 | - Euro::newFromString( '1,00' ); |
|
166 | + $this->expectException(\InvalidArgumentException::class); |
|
167 | + Euro::newFromString('1,00'); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
171 | - $this->expectException( \InvalidArgumentException::class ); |
|
172 | - Euro::newFromString( '1.0.0' ); |
|
171 | + $this->expectException(\InvalidArgumentException::class); |
|
172 | + Euro::newFromString('1.0.0'); |
|
173 | 173 | } |
174 | 174 | |
175 | 175 | public function testGivenNonNumber_exceptionIsThrown() { |
176 | - $this->expectException( \InvalidArgumentException::class ); |
|
177 | - Euro::newFromString( '1.00abc' ); |
|
176 | + $this->expectException(\InvalidArgumentException::class); |
|
177 | + Euro::newFromString('1.00abc'); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
181 | - $this->expectException( \InvalidArgumentException::class ); |
|
181 | + $this->expectException(\InvalidArgumentException::class); |
|
182 | 182 | Euro::newFromFloat( -1.00 ); |
183 | 183 | } |
184 | 184 | |
185 | 185 | public function testOneEuroFloat_getsTurnedInto100cents() { |
186 | - $this->assertSame( 100, Euro::newFromFloat( 1.0 )->getEuroCents() ); |
|
186 | + $this->assertSame(100, Euro::newFromFloat(1.0)->getEuroCents()); |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | public function testOneCentFloat_getsTurnedInto1cent() { |
190 | - $this->assertSame( 1, Euro::newFromFloat( 0.01 )->getEuroCents() ); |
|
190 | + $this->assertSame(1, Euro::newFromFloat(0.01)->getEuroCents()); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | public function testTenCentFloat_getsTurnedInto10cents() { |
194 | - $this->assertSame( 10, Euro::newFromFloat( 0.1 )->getEuroCents() ); |
|
194 | + $this->assertSame(10, Euro::newFromFloat(0.1)->getEuroCents()); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | public function testHandlingOfLargeEuroFloat() { |
198 | - $this->assertSame( 3133742, Euro::newFromFloat( 31337.42 )->getEuroCents() ); |
|
198 | + $this->assertSame(3133742, Euro::newFromFloat(31337.42)->getEuroCents()); |
|
199 | 199 | } |
200 | 200 | |
201 | 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() ); |
|
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 | 211 | } |
212 | 212 | |
213 | 213 | public function testZeroEuroIntegers_isZeroCents() { |
214 | - $this->assertSame( 0, Euro::newFromInt( 0 )->getEuroCents() ); |
|
214 | + $this->assertSame(0, Euro::newFromInt(0)->getEuroCents()); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | public function testOneEuroIntegers_is100cents() { |
218 | - $this->assertSame( 100, Euro::newFromInt( 1 )->getEuroCents() ); |
|
218 | + $this->assertSame(100, Euro::newFromInt(1)->getEuroCents()); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | public function test1337EuroIntegers_is133700cents() { |
222 | - $this->assertSame( 133700, Euro::newFromInt( 1337 )->getEuroCents() ); |
|
222 | + $this->assertSame(133700, Euro::newFromInt(1337)->getEuroCents()); |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
226 | - $this->expectException( \InvalidArgumentException::class ); |
|
226 | + $this->expectException(\InvalidArgumentException::class); |
|
227 | 227 | Euro::newFromInt( -1 ); |
228 | 228 | } |
229 | 229 | |
@@ -231,89 +231,89 @@ discard block |
||
231 | 231 | * @dataProvider euroProvider |
232 | 232 | * @param Euro $euro |
233 | 233 | */ |
234 | - public function testEuroEqualsItself( Euro $euro ) { |
|
235 | - $this->assertTrue( $euro->equals( clone $euro ) ); |
|
234 | + public function testEuroEqualsItself(Euro $euro) { |
|
235 | + $this->assertTrue($euro->equals(clone $euro)); |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | public function euroProvider() { |
239 | 239 | return [ |
240 | - [ Euro::newFromCents( 0 ) ], |
|
241 | - [ Euro::newFromCents( 1 ) ], |
|
242 | - [ Euro::newFromCents( 99 ) ], |
|
243 | - [ Euro::newFromCents( 100 ) ], |
|
244 | - [ Euro::newFromCents( 9999 ) ], |
|
240 | + [Euro::newFromCents(0)], |
|
241 | + [Euro::newFromCents(1)], |
|
242 | + [Euro::newFromCents(99)], |
|
243 | + [Euro::newFromCents(100)], |
|
244 | + [Euro::newFromCents(9999)], |
|
245 | 245 | ]; |
246 | 246 | } |
247 | 247 | |
248 | 248 | public function testOneCentDoesNotEqualOneEuro() { |
249 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromInt( 1 ) ) ); |
|
249 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromInt(1))); |
|
250 | 250 | } |
251 | 251 | |
252 | 252 | public function testOneCentDoesNotEqualTwoCents() { |
253 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 2 ) ) ); |
|
253 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromCents(2))); |
|
254 | 254 | } |
255 | 255 | |
256 | 256 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
257 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 101 ) ) ); |
|
257 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromCents(101))); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | public function test9001centsDoesNotEqual9000cents() { |
261 | - $this->assertFalse( Euro::newFromCents( 9001 )->equals( Euro::newFromCents( 9000 ) ) ); |
|
261 | + $this->assertFalse(Euro::newFromCents(9001)->equals(Euro::newFromCents(9000))); |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | /** |
265 | 265 | * @dataProvider tooLongStringProvider |
266 | 266 | */ |
267 | - public function testNewFromStringThrowsExceptionWhenStringIsTooLong( string $string ) { |
|
268 | - $this->expectException( InvalidArgumentException::class ); |
|
269 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
267 | + public function testNewFromStringThrowsExceptionWhenStringIsTooLong(string $string) { |
|
268 | + $this->expectException(InvalidArgumentException::class); |
|
269 | + $this->expectExceptionMessage('Number is too big'); |
|
270 | 270 | |
271 | - Euro::newFromString( $string ); |
|
271 | + Euro::newFromString($string); |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | public function tooLongStringProvider() { |
275 | - yield [ '1111111111111111111111111111111' ]; |
|
276 | - yield [ (string)PHP_INT_MAX ]; |
|
277 | - yield [ substr( (string)PHP_INT_MAX, 0, -2 ) ]; |
|
275 | + yield ['1111111111111111111111111111111']; |
|
276 | + yield [(string) PHP_INT_MAX]; |
|
277 | + yield [substr((string) PHP_INT_MAX, 0, -2)]; |
|
278 | 278 | } |
279 | 279 | |
280 | 280 | public function testNewFromStringHandlesLongStrings() { |
281 | - Euro::newFromString( substr( (string)PHP_INT_MAX, 0, -3 ) ); |
|
282 | - $this->assertTrue( true ); |
|
281 | + Euro::newFromString(substr((string) PHP_INT_MAX, 0, -3)); |
|
282 | + $this->assertTrue(true); |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | /** |
286 | 286 | * @dataProvider tooHighNumberProvider |
287 | 287 | */ |
288 | - public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh( int $int ) { |
|
289 | - $this->expectException( InvalidArgumentException::class ); |
|
290 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
291 | - Euro::newFromInt( $int ); |
|
288 | + public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh(int $int) { |
|
289 | + $this->expectException(InvalidArgumentException::class); |
|
290 | + $this->expectExceptionMessage('Number is too big'); |
|
291 | + Euro::newFromInt($int); |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | public function tooHighNumberProvider() { |
295 | - yield [ PHP_INT_MAX ]; |
|
296 | - yield [ PHP_INT_MAX / 10 ]; |
|
297 | - yield [ PHP_INT_MAX / 100 ]; |
|
295 | + yield [PHP_INT_MAX]; |
|
296 | + yield [PHP_INT_MAX / 10]; |
|
297 | + yield [PHP_INT_MAX / 100]; |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
301 | 301 | * @dataProvider tooHighNumberProvider |
302 | 302 | */ |
303 | - public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh( int $int ) { |
|
304 | - $this->expectException( InvalidArgumentException::class ); |
|
305 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
306 | - Euro::newFromFloat( (float)$int ); |
|
303 | + public function testNewFromFloatThrowsExceptionWhenFloatIsTooHigh(int $int) { |
|
304 | + $this->expectException(InvalidArgumentException::class); |
|
305 | + $this->expectExceptionMessage('Number is too big'); |
|
306 | + Euro::newFromFloat((float) $int); |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | public function testNewFromIntHandlesBigIntegers() { |
310 | 310 | // Edge case test for the highest allowed value (Euro::CENTS_PER_EURO +1 ) |
311 | 311 | // 100 (Euro::CENTS_PER_EURO) does not work due to rounding |
312 | - $number = (int)floor( PHP_INT_MAX / 101 ); |
|
312 | + $number = (int) floor(PHP_INT_MAX / 101); |
|
313 | 313 | |
314 | 314 | $this->assertSame( |
315 | 315 | $number * 100, |
316 | - Euro::newFromInt( $number )->getEuroCents() |
|
316 | + Euro::newFromInt($number)->getEuroCents() |
|
317 | 317 | ); |
318 | 318 | } |
319 | 319 |