@@ -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,201 +19,201 @@ 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 testOneEuroString_getsTurnedInto100cents() { |
96 | - $this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() ); |
|
96 | + $this->assertSame(100, Euro::newFromString('1.00')->getEuroCents()); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | public function testOneCentString_getsTurnedInto1cents() { |
100 | - $this->assertSame( 1, Euro::newFromString( '0.01' )->getEuroCents() ); |
|
100 | + $this->assertSame(1, Euro::newFromString('0.01')->getEuroCents()); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | public function testTenCentString_getsTurnedInto10cents() { |
104 | - $this->assertSame( 10, Euro::newFromString( '0.10' )->getEuroCents() ); |
|
104 | + $this->assertSame(10, Euro::newFromString('0.10')->getEuroCents()); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | public function testShortTenCentString_getsTurnedInto10cents() { |
108 | - $this->assertSame( 10, Euro::newFromString( '0.1' )->getEuroCents() ); |
|
108 | + $this->assertSame(10, Euro::newFromString('0.1')->getEuroCents()); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | public function testShortOneEuroString_getsTurnedInto100cents() { |
112 | - $this->assertSame( 100, Euro::newFromString( '1' )->getEuroCents() ); |
|
112 | + $this->assertSame(100, Euro::newFromString('1')->getEuroCents()); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | public function testOneDecimalOneEuroString_getsTurnedInto100cents() { |
116 | - $this->assertSame( 100, Euro::newFromString( '1.0' )->getEuroCents() ); |
|
116 | + $this->assertSame(100, Euro::newFromString('1.0')->getEuroCents()); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | public function testMultiDecimalOneEuroString_getsTurnedInto100cents() { |
120 | - $this->assertSame( 100, Euro::newFromString( '1.00000' )->getEuroCents() ); |
|
120 | + $this->assertSame(100, Euro::newFromString('1.00000')->getEuroCents()); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | public function testHandlingOfLargeEuroString() { |
124 | - $this->assertSame( 3133742, Euro::newFromString( '31337.42' )->getEuroCents() ); |
|
124 | + $this->assertSame(3133742, Euro::newFromString('31337.42')->getEuroCents()); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | public function testEuroStringThatCausedRoundingError_doesNotCauseRoundingError() { |
128 | 128 | // Regression test for https://phabricator.wikimedia.org/T183481 |
129 | - $this->assertSame( 870, Euro::newFromString( '8.70' )->getEuroCents() ); |
|
130 | - $this->assertSame( 920, Euro::newFromString( '9.20' )->getEuroCents() ); |
|
129 | + $this->assertSame(870, Euro::newFromString('8.70')->getEuroCents()); |
|
130 | + $this->assertSame(920, Euro::newFromString('9.20')->getEuroCents()); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | public function testEuroStringWithRoundingError_getsRoundedAppropriately() { |
134 | - $this->assertSame( 101, Euro::newFromString( '1.0100000001' )->getEuroCents() ); |
|
135 | - $this->assertSame( 101, Euro::newFromString( '1.010000009999' )->getEuroCents() ); |
|
136 | - $this->assertSame( 101, Euro::newFromString( '1.011' )->getEuroCents() ); |
|
137 | - $this->assertSame( 101, Euro::newFromString( '1.014' )->getEuroCents() ); |
|
138 | - $this->assertSame( 101, Euro::newFromString( '1.0149' )->getEuroCents() ); |
|
139 | - $this->assertSame( 102, Euro::newFromString( '1.015' )->getEuroCents() ); |
|
140 | - $this->assertSame( 102, Euro::newFromString( '1.019' )->getEuroCents() ); |
|
141 | - $this->assertSame( 102, Euro::newFromString( '1.0199999' )->getEuroCents() ); |
|
142 | - $this->assertSame( 870, Euro::newFromString( '8.701' )->getEuroCents() ); |
|
143 | - $this->assertSame( 870, Euro::newFromString( '8.70499' )->getEuroCents() ); |
|
144 | - $this->assertSame( 871, Euro::newFromString( '8.705' )->getEuroCents() ); |
|
145 | - $this->assertSame( 871, Euro::newFromString( '8.705000' )->getEuroCents() ); |
|
146 | - $this->assertSame( 871, Euro::newFromString( '8.705001' )->getEuroCents() ); |
|
147 | - $this->assertSame( 871, Euro::newFromString( '8.709999' )->getEuroCents() ); |
|
134 | + $this->assertSame(101, Euro::newFromString('1.0100000001')->getEuroCents()); |
|
135 | + $this->assertSame(101, Euro::newFromString('1.010000009999')->getEuroCents()); |
|
136 | + $this->assertSame(101, Euro::newFromString('1.011')->getEuroCents()); |
|
137 | + $this->assertSame(101, Euro::newFromString('1.014')->getEuroCents()); |
|
138 | + $this->assertSame(101, Euro::newFromString('1.0149')->getEuroCents()); |
|
139 | + $this->assertSame(102, Euro::newFromString('1.015')->getEuroCents()); |
|
140 | + $this->assertSame(102, Euro::newFromString('1.019')->getEuroCents()); |
|
141 | + $this->assertSame(102, Euro::newFromString('1.0199999')->getEuroCents()); |
|
142 | + $this->assertSame(870, Euro::newFromString('8.701')->getEuroCents()); |
|
143 | + $this->assertSame(870, Euro::newFromString('8.70499')->getEuroCents()); |
|
144 | + $this->assertSame(871, Euro::newFromString('8.705')->getEuroCents()); |
|
145 | + $this->assertSame(871, Euro::newFromString('8.705000')->getEuroCents()); |
|
146 | + $this->assertSame(871, Euro::newFromString('8.705001')->getEuroCents()); |
|
147 | + $this->assertSame(871, Euro::newFromString('8.709999')->getEuroCents()); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | public function testGivenNegativeAmountString_exceptionIsThrown() { |
151 | - $this->expectException( \InvalidArgumentException::class ); |
|
152 | - Euro::newFromString( '-1.00' ); |
|
151 | + $this->expectException(\InvalidArgumentException::class); |
|
152 | + Euro::newFromString('-1.00'); |
|
153 | 153 | } |
154 | 154 | |
155 | 155 | public function testGivenStringWithComma_exceptionIsThrown() { |
156 | - $this->expectException( \InvalidArgumentException::class ); |
|
157 | - Euro::newFromString( '1,00' ); |
|
156 | + $this->expectException(\InvalidArgumentException::class); |
|
157 | + Euro::newFromString('1,00'); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | public function testGivenStringWithMultipleDots_ExceptionIsThrown() { |
161 | - $this->expectException( \InvalidArgumentException::class ); |
|
162 | - Euro::newFromString( '1.0.0' ); |
|
161 | + $this->expectException(\InvalidArgumentException::class); |
|
162 | + Euro::newFromString('1.0.0'); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | public function testGivenNonNumber_exceptionIsThrown() { |
166 | - $this->expectException( \InvalidArgumentException::class ); |
|
167 | - Euro::newFromString( '1.00abc' ); |
|
166 | + $this->expectException(\InvalidArgumentException::class); |
|
167 | + Euro::newFromString('1.00abc'); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | public function testGivenNegativeFloatAmount_exceptionIsThrown() { |
171 | - $this->expectException( \InvalidArgumentException::class ); |
|
171 | + $this->expectException(\InvalidArgumentException::class); |
|
172 | 172 | Euro::newFromFloat( -1.00 ); |
173 | 173 | } |
174 | 174 | |
175 | 175 | public function testOneEuroFloat_getsTurnedInto100cents() { |
176 | - $this->assertSame( 100, Euro::newFromFloat( 1.0 )->getEuroCents() ); |
|
176 | + $this->assertSame(100, Euro::newFromFloat(1.0)->getEuroCents()); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | public function testOneCentFloat_getsTurnedInto1cent() { |
180 | - $this->assertSame( 1, Euro::newFromFloat( 0.01 )->getEuroCents() ); |
|
180 | + $this->assertSame(1, Euro::newFromFloat(0.01)->getEuroCents()); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | public function testTenCentFloat_getsTurnedInto10cents() { |
184 | - $this->assertSame( 10, Euro::newFromFloat( 0.1 )->getEuroCents() ); |
|
184 | + $this->assertSame(10, Euro::newFromFloat(0.1)->getEuroCents()); |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | public function testHandlingOfLargeEuroFloat() { |
188 | - $this->assertSame( 3133742, Euro::newFromFloat( 31337.42 )->getEuroCents() ); |
|
188 | + $this->assertSame(3133742, Euro::newFromFloat(31337.42)->getEuroCents()); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | public function testFloatWithRoundingError_getsRoundedAppropriately() { |
192 | - $this->assertSame( 101, Euro::newFromFloat( 1.0100000001 )->getEuroCents() ); |
|
193 | - $this->assertSame( 101, Euro::newFromFloat( 1.010000009999 )->getEuroCents() ); |
|
194 | - $this->assertSame( 101, Euro::newFromFloat( 1.011 )->getEuroCents() ); |
|
195 | - $this->assertSame( 101, Euro::newFromFloat( 1.014 )->getEuroCents() ); |
|
196 | - $this->assertSame( 101, Euro::newFromFloat( 1.0149 )->getEuroCents() ); |
|
197 | - $this->assertSame( 102, Euro::newFromFloat( 1.015 )->getEuroCents() ); |
|
198 | - $this->assertSame( 102, Euro::newFromFloat( 1.019 )->getEuroCents() ); |
|
199 | - $this->assertSame( 102, Euro::newFromFloat( 1.0199999 )->getEuroCents() ); |
|
200 | - $this->assertSame( 870, Euro::newFromFloat( 8.70 )->getEuroCents() ); |
|
192 | + $this->assertSame(101, Euro::newFromFloat(1.0100000001)->getEuroCents()); |
|
193 | + $this->assertSame(101, Euro::newFromFloat(1.010000009999)->getEuroCents()); |
|
194 | + $this->assertSame(101, Euro::newFromFloat(1.011)->getEuroCents()); |
|
195 | + $this->assertSame(101, Euro::newFromFloat(1.014)->getEuroCents()); |
|
196 | + $this->assertSame(101, Euro::newFromFloat(1.0149)->getEuroCents()); |
|
197 | + $this->assertSame(102, Euro::newFromFloat(1.015)->getEuroCents()); |
|
198 | + $this->assertSame(102, Euro::newFromFloat(1.019)->getEuroCents()); |
|
199 | + $this->assertSame(102, Euro::newFromFloat(1.0199999)->getEuroCents()); |
|
200 | + $this->assertSame(870, Euro::newFromFloat(8.70)->getEuroCents()); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | public function testZeroEuroIntegers_isZeroCents() { |
204 | - $this->assertSame( 0, Euro::newFromInt( 0 )->getEuroCents() ); |
|
204 | + $this->assertSame(0, Euro::newFromInt(0)->getEuroCents()); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | public function testOneEuroIntegers_is100cents() { |
208 | - $this->assertSame( 100, Euro::newFromInt( 1 )->getEuroCents() ); |
|
208 | + $this->assertSame(100, Euro::newFromInt(1)->getEuroCents()); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | public function test1337EuroIntegers_is133700cents() { |
212 | - $this->assertSame( 133700, Euro::newFromInt( 1337 )->getEuroCents() ); |
|
212 | + $this->assertSame(133700, Euro::newFromInt(1337)->getEuroCents()); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | public function testGivenNegativeIntegerAmount_exceptionIsThrown() { |
216 | - $this->expectException( \InvalidArgumentException::class ); |
|
216 | + $this->expectException(\InvalidArgumentException::class); |
|
217 | 217 | Euro::newFromInt( -1 ); |
218 | 218 | } |
219 | 219 | |
@@ -221,78 +221,78 @@ discard block |
||
221 | 221 | * @dataProvider euroProvider |
222 | 222 | * @param Euro $euro |
223 | 223 | */ |
224 | - public function testEuroEqualsItself( Euro $euro ) { |
|
225 | - $this->assertTrue( $euro->equals( clone $euro ) ); |
|
224 | + public function testEuroEqualsItself(Euro $euro) { |
|
225 | + $this->assertTrue($euro->equals(clone $euro)); |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | public function euroProvider() { |
229 | 229 | return [ |
230 | - [ Euro::newFromCents( 0 ) ], |
|
231 | - [ Euro::newFromCents( 1 ) ], |
|
232 | - [ Euro::newFromCents( 99 ) ], |
|
233 | - [ Euro::newFromCents( 100 ) ], |
|
234 | - [ Euro::newFromCents( 9999 ) ], |
|
230 | + [Euro::newFromCents(0)], |
|
231 | + [Euro::newFromCents(1)], |
|
232 | + [Euro::newFromCents(99)], |
|
233 | + [Euro::newFromCents(100)], |
|
234 | + [Euro::newFromCents(9999)], |
|
235 | 235 | ]; |
236 | 236 | } |
237 | 237 | |
238 | 238 | public function testOneCentDoesNotEqualOneEuro() { |
239 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromInt( 1 ) ) ); |
|
239 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromInt(1))); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | public function testOneCentDoesNotEqualTwoCents() { |
243 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 2 ) ) ); |
|
243 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromCents(2))); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | public function testOneCentDoesNotEqualOneEuroAndOneCent() { |
247 | - $this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 101 ) ) ); |
|
247 | + $this->assertFalse(Euro::newFromCents(1)->equals(Euro::newFromCents(101))); |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | public function test9001centsDoesNotEqual9000cents() { |
251 | - $this->assertFalse( Euro::newFromCents( 9001 )->equals( Euro::newFromCents( 9000 ) ) ); |
|
251 | + $this->assertFalse(Euro::newFromCents(9001)->equals(Euro::newFromCents(9000))); |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | /** |
255 | 255 | * @dataProvider tooLongStringProvider |
256 | 256 | */ |
257 | - public function testNewFromStringThrowsExceptionWhenStringIsTooLong( string $string ) { |
|
258 | - $this->expectException( InvalidArgumentException::class ); |
|
259 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
257 | + public function testNewFromStringThrowsExceptionWhenStringIsTooLong(string $string) { |
|
258 | + $this->expectException(InvalidArgumentException::class); |
|
259 | + $this->expectExceptionMessage('Number is too big'); |
|
260 | 260 | |
261 | - Euro::newFromString( $string ); |
|
261 | + Euro::newFromString($string); |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | public function tooLongStringProvider() { |
265 | - yield [ '1111111111111111111111111111111' ]; |
|
266 | - yield [ (string)PHP_INT_MAX ]; |
|
267 | - yield [ substr( (string)PHP_INT_MAX, 0, -2 ) ]; |
|
265 | + yield ['1111111111111111111111111111111']; |
|
266 | + yield [(string) PHP_INT_MAX]; |
|
267 | + yield [substr((string) PHP_INT_MAX, 0, -2)]; |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | public function testNewFromStringHandlesLongStrings() { |
271 | - Euro::newFromString( substr( (string)PHP_INT_MAX, 0, -3 ) ); |
|
272 | - $this->assertTrue( true ); |
|
271 | + Euro::newFromString(substr((string) PHP_INT_MAX, 0, -3)); |
|
272 | + $this->assertTrue(true); |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | /** |
276 | 276 | * @dataProvider tooHighIntegerProvider |
277 | 277 | */ |
278 | - public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh( int $int ) { |
|
279 | - $this->expectException( InvalidArgumentException::class ); |
|
280 | - $this->expectExceptionMessage( 'Number is too big' ); |
|
281 | - Euro::newFromInt( $int ); |
|
278 | + public function testNewFromIntThrowsExceptionWhenIntegerIsTooHigh(int $int) { |
|
279 | + $this->expectException(InvalidArgumentException::class); |
|
280 | + $this->expectExceptionMessage('Number is too big'); |
|
281 | + Euro::newFromInt($int); |
|
282 | 282 | } |
283 | 283 | |
284 | 284 | public function tooHighIntegerProvider() { |
285 | - yield [ PHP_INT_MAX ]; |
|
286 | - yield [ PHP_INT_MAX / 10 ]; |
|
287 | - yield [ PHP_INT_MAX / 100 ]; |
|
285 | + yield [PHP_INT_MAX]; |
|
286 | + yield [PHP_INT_MAX / 10]; |
|
287 | + yield [PHP_INT_MAX / 100]; |
|
288 | 288 | } |
289 | 289 | |
290 | 290 | public function testNewFromIntHandlesBigIntegers() { |
291 | - $number = (int)floor( PHP_INT_MAX / 101 ); |
|
291 | + $number = (int) floor(PHP_INT_MAX / 101); |
|
292 | 292 | |
293 | 293 | $this->assertSame( |
294 | 294 | $number * 100, |
295 | - Euro::newFromInt( $number )->getEuroCents() |
|
295 | + Euro::newFromInt($number)->getEuroCents() |
|
296 | 296 | ); |
297 | 297 | } |
298 | 298 |
@@ -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,13 +98,13 @@ discard block |
||
98 | 98 | * @return self |
99 | 99 | * @throws InvalidArgumentException |
100 | 100 | */ |
101 | - public static function newFromFloat( float $euroAmount ): self { |
|
102 | - return new self( intval( |
|
101 | + public static function newFromFloat(float $euroAmount): self { |
|
102 | + return new self(intval( |
|
103 | 103 | round( |
104 | - round( $euroAmount, self::DECIMAL_COUNT ) * self::CENTS_PER_EURO, |
|
104 | + round($euroAmount, self::DECIMAL_COUNT) * self::CENTS_PER_EURO, |
|
105 | 105 | 0 |
106 | 106 | ) |
107 | - ) ); |
|
107 | + )); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
@@ -112,12 +112,12 @@ discard block |
||
112 | 112 | * @return self |
113 | 113 | * @throws InvalidArgumentException |
114 | 114 | */ |
115 | - public static function newFromInt( int $euroAmount ): self { |
|
116 | - if ( $euroAmount > floor( PHP_INT_MAX / 101 ) ) { |
|
117 | - throw new InvalidArgumentException( 'Number is too big' ); |
|
115 | + public static function newFromInt(int $euroAmount): self { |
|
116 | + if ($euroAmount > floor(PHP_INT_MAX / 101)) { |
|
117 | + throw new InvalidArgumentException('Number is too big'); |
|
118 | 118 | } |
119 | 119 | |
120 | - return new self( $euroAmount * self::CENTS_PER_EURO ); |
|
120 | + return new self($euroAmount * self::CENTS_PER_EURO); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | public function getEuroCents(): int { |
@@ -132,10 +132,10 @@ discard block |
||
132 | 132 | * Returns the euro amount as string with two decimals always present in format "42.00". |
133 | 133 | */ |
134 | 134 | public function getEuroString(): string { |
135 | - return number_format( $this->getEuroFloat(), self::DECIMAL_COUNT, '.', '' ); |
|
135 | + return number_format($this->getEuroFloat(), self::DECIMAL_COUNT, '.', ''); |
|
136 | 136 | } |
137 | 137 | |
138 | - public function equals( Euro $euro ): bool { |
|
138 | + public function equals(Euro $euro): bool { |
|
139 | 139 | return $this->cents === $euro->cents; |
140 | 140 | } |
141 | 141 |