Completed
Push — master ( 9569f0...7d46f5 )
by Jeroen De
09:50 queued 02:04
created

EuroTest::testGivenNonNumber_exceptionIsThrown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Euro\Tests\Unit;
6
7
use WMDE\Euro\Euro;
8
9
/**
10
 * @covers \WMDE\Euro\Euro
11
 *
12
 * @license GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class EuroTest extends \PHPUnit_Framework_TestCase {
16
17
	/**
18
	 * @dataProvider unsignedIntegerProvider
19
	 */
20
	public function testGetCentsReturnsConstructorArgument( int $unsignedInteger ) {
21
		$amount = Euro::newFromCents( $unsignedInteger );
22
		$this->assertSame( $unsignedInteger, $amount->getEuroCents() );
23
	}
24
25
	public function unsignedIntegerProvider() {
26
		return [
27
			[ 0 ], [ 1 ], [ 2 ], [ 9 ], [ 10 ], [ 11 ],
28
			[ 99 ], [ 100 ], [ 101 ], [ 999 ], [ 1000 ], [ 1001 ],
29
		];
30
	}
31
32
	public function testGivenZero_getEuroFloatReturnsZeroFloat() {
33
		$amount = Euro::newFromCents( 0 );
34
		$this->assertExactFloat( 0.0, $amount->getEuroFloat() );
35
		$this->assertNotSame( 0, $amount->getEuroFloat() );
36
	}
37
38
	private function assertExactFloat( float $expected, $actual ) {
39
		$this->assertInternalType( 'float', $actual );
40
		$this->assertEquals( $expected, $actual, '', 0 );
41
	}
42
43
	public function testGivenOneEuro_getEuroFloatReturnsOne() {
44
		$amount = Euro::newFromCents( 100 );
45
		$this->assertExactFloat( 1.0, $amount->getEuroFloat() );
46
	}
47
48
	public function testGivenOneCent_getEuroFloatReturnsPointZeroOne() {
49
		$amount = Euro::newFromCents( 1 );
50
		$this->assertExactFloat( 0.01, $amount->getEuroFloat() );
51
	}
52
53
	public function testGiven33cents_getEuroFloatReturnsPointThreeThree() {
54
		$amount = Euro::newFromCents( 33 );
55
		$this->assertExactFloat( 0.33, $amount->getEuroFloat() );
56
	}
57
58
	public function testGivenNegativeAmount_constructorThrowsException() {
59
		$this->expectException( \InvalidArgumentException::class );
60
		Euro::newFromCents( -1 );
61
	}
62
63
	public function testGivenZero_getEuroStringReturnsZeroString() {
64
		$amount = Euro::newFromCents( 0 );
65
		$this->assertSame( '0.00', $amount->getEuroString() );
66
	}
67
68
	public function testGivenOneEuro_getEuroStringReturnsOnePointZeroZero() {
69
		$amount = Euro::newFromCents( 100 );
70
		$this->assertSame( '1.00', $amount->getEuroString() );
71
	}
72
73
	public function testGivenTwoEuros_getEuroStringReturnsTwoPointZeroZero() {
74
		$amount = Euro::newFromCents( 200 );
75
		$this->assertSame( '2.00', $amount->getEuroString() );
76
	}
77
78
	public function testGivenOneCent_getEuroStringReturnsZeroPointZeroOne() {
79
		$amount = Euro::newFromCents( 1 );
80
		$this->assertSame( '0.01', $amount->getEuroString() );
81
	}
82
83
	public function testGivenTenCents_getEuroStringReturnsZeroPointOneZero() {
84
		$amount = Euro::newFromCents( 10 );
85
		$this->assertSame( '0.10', $amount->getEuroString() );
86
	}
87
88
	public function testGiven1234Cents_getEuroStringReturns12euro34() {
89
		$amount = Euro::newFromCents( 1234 );
90
		$this->assertSame( '12.34', $amount->getEuroString() );
91
	}
92
93
	public function testOneEuroString_getsTurnedInto100cents() {
94
		$this->assertSame( 100, Euro::newFromString( '1.00' )->getEuroCents() );
95
	}
96
97
	public function testOneCentString_getsTurnedInto1cents() {
98
		$this->assertSame( 1, Euro::newFromString( '0.01' )->getEuroCents() );
99
	}
100
101
	public function testTenCentString_getsTurnedInto10cents() {
102
		$this->assertSame( 10, Euro::newFromString( '0.10' )->getEuroCents() );
103
	}
104
105
	public function testShortTenCentString_getsTurnedInto10cents() {
106
		$this->assertSame( 10, Euro::newFromString( '0.1' )->getEuroCents() );
107
	}
108
109
	public function testShortOneEuroString_getsTurnedInto100cents() {
110
		$this->assertSame( 100, Euro::newFromString( '1' )->getEuroCents() );
111
	}
112
113
	public function testOneDecimalOneEuroString_getsTurnedInto100cents() {
114
		$this->assertSame( 100, Euro::newFromString( '1.0' )->getEuroCents() );
115
	}
116
117
	public function testMultiDecimalOneEuroString_getsTurnedInto100cents() {
118
		$this->assertSame( 100, Euro::newFromString( '1.00000' )->getEuroCents() );
119
	}
120
121
	public function testHandlingOfLargeEuroString() {
122
		$this->assertSame( 3133742, Euro::newFromString( '31337.42' )->getEuroCents() );
123
	}
124
125
	public function testEuroStringWithRoundingError_getsRoundedAppropriately() {
126
		$this->assertSame( 101, Euro::newFromString( '1.0100000001' )->getEuroCents() );
127
		$this->assertSame( 101, Euro::newFromString( '1.010000009999' )->getEuroCents() );
128
		$this->assertSame( 101, Euro::newFromString( '1.011' )->getEuroCents() );
129
		$this->assertSame( 101, Euro::newFromString( '1.014' )->getEuroCents() );
130
		$this->assertSame( 101, Euro::newFromString( '1.0149' )->getEuroCents() );
131
		$this->assertSame( 102, Euro::newFromString( '1.015' )->getEuroCents() );
132
		$this->assertSame( 102, Euro::newFromString( '1.019' )->getEuroCents() );
133
		$this->assertSame( 102, Euro::newFromString( '1.0199999' )->getEuroCents() );
134
	}
135
136
	public function testGivenNegativeAmountString_exceptionIsThrown() {
137
		$this->expectException( \InvalidArgumentException::class );
138
		Euro::newFromString( '-1.00' );
139
	}
140
141
	public function testGivenStringWithComma_exceptionIsThrown() {
142
		$this->expectException( \InvalidArgumentException::class );
143
		Euro::newFromString( '1,00' );
144
	}
145
146
	public function testGivenStringWithMultipleDots_ExceptionIsThrown() {
147
		$this->expectException( \InvalidArgumentException::class );
148
		Euro::newFromString( '1.0.0' );
149
	}
150
151
	public function testGivenNonNumber_exceptionIsThrown() {
152
		$this->expectException( \InvalidArgumentException::class );
153
		Euro::newFromString( '1.00abc' );
154
	}
155
156
	public function testGivenNegativeFloatAmount_exceptionIsThrown() {
157
		$this->expectException( \InvalidArgumentException::class );
158
		Euro::newFromFloat( -1.00 );
159
	}
160
161
	public function testOneEuroFloat_getsTurnedInto100cents() {
162
		$this->assertSame( 100, Euro::newFromFloat( 1.0 )->getEuroCents() );
163
	}
164
165
	public function testOneCentFloat_getsTurnedInto1cent() {
166
		$this->assertSame( 1, Euro::newFromFloat( 0.01 )->getEuroCents() );
167
	}
168
169
	public function testTenCentFloat_getsTurnedInto10cents() {
170
		$this->assertSame( 10, Euro::newFromFloat( 0.1 )->getEuroCents() );
171
	}
172
173
	public function testHandlingOfLargeEuroFloat() {
174
		$this->assertSame( 3133742, Euro::newFromFloat( 31337.42 )->getEuroCents() );
175
	}
176
177
	public function testFloatWithRoundingError_getsRoundedAppropriately() {
178
		$this->assertSame( 101, Euro::newFromFloat( 1.0100000001 )->getEuroCents() );
179
		$this->assertSame( 101, Euro::newFromFloat( 1.010000009999 )->getEuroCents() );
180
		$this->assertSame( 101, Euro::newFromFloat( 1.011 )->getEuroCents() );
181
		$this->assertSame( 101, Euro::newFromFloat( 1.014 )->getEuroCents() );
182
		$this->assertSame( 101, Euro::newFromFloat( 1.0149 )->getEuroCents() );
183
		$this->assertSame( 102, Euro::newFromFloat( 1.015 )->getEuroCents() );
184
		$this->assertSame( 102, Euro::newFromFloat( 1.019 )->getEuroCents() );
185
		$this->assertSame( 102, Euro::newFromFloat( 1.0199999 )->getEuroCents() );
186
	}
187
188
	public function testZeroEuroIntegers_isZeroCents() {
189
		$this->assertSame( 0, Euro::newFromInt( 0 )->getEuroCents() );
190
	}
191
192
	public function testOneEuroIntegers_is100cents() {
193
		$this->assertSame( 100, Euro::newFromInt( 1 )->getEuroCents() );
194
	}
195
196
	public function test1337EuroIntegers_is133700cents() {
197
		$this->assertSame( 133700, Euro::newFromInt( 1337 )->getEuroCents() );
198
	}
199
200
	public function testGivenNegativeIntegerAmount_exceptionIsThrown() {
201
		$this->expectException( \InvalidArgumentException::class );
202
		Euro::newFromInt( -1 );
203
	}
204
205
	/**
206
	 * @dataProvider euroProvider
207
	 * @param Euro $euro
208
	 */
209
	public function testEuroEqualsItself( Euro $euro ) {
210
		$this->assertTrue( $euro->equals( clone $euro ) );
211
	}
212
213
	public function euroProvider() {
214
		return [
215
			[ Euro::newFromCents( 0 ) ],
216
			[ Euro::newFromCents( 1 ) ],
217
			[ Euro::newFromCents( 99 ) ],
218
			[ Euro::newFromCents( 100 ) ],
219
			[ Euro::newFromCents( 9999 ) ],
220
		];
221
	}
222
223
	public function testOneCentDoesNotEqualOneEuro() {
224
		$this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromInt( 1 ) ) );
225
	}
226
227
	public function testOneCentDoesNotEqualTwoCents() {
228
		$this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 2 ) ) );
229
	}
230
231
	public function testOneCentDoesNotEqualOneEuroAndOneCent() {
232
		$this->assertFalse( Euro::newFromCents( 1 )->equals( Euro::newFromCents( 101 ) ) );
233
	}
234
235
	public function test9001centsDoesNotEqual9000cents() {
236
		$this->assertFalse( Euro::newFromCents( 9001 )->equals( Euro::newFromCents( 9000 ) ) );
237
	}
238
239
}
240